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
From:
Ronald J Kimball
Date:
September 26, 2000 18:48
Subject:
Re: [ID 20000926.002] substr with undef values is not consistant
Message ID:
20000926214724.A559553@linguist.dartmouth.edu
On Tue, Sep 26, 2000 at 08:12:39PM -0400, Clinton A. Pierce wrote:
> This short program:
> 
> 	no warnings qw(uninitialized);
> 
> 	if (defined substr(undef, 0)) {
> 		print "At 0, defined!\n";
> 	   } else {
> 		print "At 0, not defined!\n";
> 	}
> 
> 	if (defined substr(undef, 1)) {
> 		print "At 1, defined!\n";
> 	   } else {
> 		print "At 1, not defined!\n";
> 	}
> 
> Results in:
> 
> 	At 0, defined!
> 	At 1, not defined!
> 
> So, substr(undef, 0) results in "", which is defined but
> substr(undef, 1) results in undef.  

That is the correct behavior.  substr(undef, 0) is equivalent to
substr('', 0), and at the 0th position of '' there is a null string, which
is defined.

substr(undef, 1) is equivalent to substr('', 1), and the 1st position is
outside the string, so the result is undefined.

If you'd left warnings on you would have received a use of uninitialized
value warning, where undef is being converted to the null string.

Ronald

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About