develooper Front page | perl.perl5.porters | Postings from September 2000

Re: [ID 20000926.002] substr with undef values is not consistant

Thread Previous | Thread Next
Ronald J Kimball
September 27, 2000 05:48
Re: [ID 20000926.002] substr with undef values is not consistant
Message ID:
On Tue, Sep 26, 2000 at 11:59:53PM -0400, Clinton A. Pierce wrote:
> Ronald J Kimball wrote:
> > C<undef + 4>, C<"hi!" . undef>, and C< undef << 2 > could all result in
> > undef, and that would be a reasonable implementation, but it's not how Perl
> > works.
> That's not what I'm arguing here.  I'm not arguing that substr(undef, 0)
> should return undef necessarily.  I'm arguing that substr(undef, 1)
> should return whatever substr(undef, 0).  I don't CARE which.  undef or
> "", take your pick, but don't flip-flop based on positional logic. 

Position 0 is within the bounds of the null string.  Positions 1..MAXINT
are outside the bounds of the null string.  Similarly, positions 0..1 are
within the bounds of a one-character string, and 2..MAXINT are outside the
bounds.  And so on.  It is both consistent and logical.

> Seeing substr behave this way gives me the creepy feeling that I'm
> looking at C code which reads:
>         char s[]="Hello, world";
>         s[0]=(char)NULL;
>         printf("%s", &s[1]);   /* See! It's still there! */
> That's my position, and I'm sticking to it.  At least until I've had a
> good night's sleep and still think that substr beyond-the-end-of-undef
> should behave that way.

Good morning!  :)

There is no such thing as "substr beyond-the-end-of-undef".  The "str" in
substr is short for string, and the first argument to substr must be a
string.  In a string context, undef is treated as the empty string, so
substr(undef, 0) and substr(undef, 1) are the same as substr('', 0) and
substr('', 1), respectively.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About