develooper Front page | perl.perl5.porters | Postings from October 2005

Re: [perl #37315] 'my' with 'if 0' retains old value

Thread Previous
From:
Steven Schubiger
Date:
October 1, 2005 02:18
Subject:
Re: [perl #37315] 'my' with 'if 0' retains old value
Message ID:
20050930183815.GC7113@accognoscere.homeunix.org
On Fri, Sep 30, 2005 at 09:45:27AM -0700, herington, dean wrote:
> Running the following program outputs:
>   Perl 5.008004
>   undef
>   undef
>   undef
>   2
> but it should output:
>   Perl 5.008004
>   undef
>   undef
>   undef
>   undef
>  
> 
> #!/usr/bin/perl -w
>  
> print "Perl $]\n";
>  
> sub show {
>     my ($val) = @_;
>     if (! defined $val) {
>         print "undef\n";
>     } else {
>         print "$val\n";
>     }
> }
>  
> sub sb {
>  
>     my $v1;
>     $v1 = -1 if 0;
>  
>     my $v2 = -2 if 0;
>  
>     show $v1;
>     show $v2;
>  
>     $v1 = 1;
>     $v2 = 2;
>  
> }
>  
> sb;
> sb;

MJD (Mark-Jason Dominus) gave an explanation which is located at:
http://www.perl.com/pub/a/2000/05/p5pdigest/THISWEEK-20000521.html#my_x_if_0%3B_Trick

The according thread on p5p:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-05/msg00621.html

Thread Previous


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