develooper Front page | perl.perl5.porters | Postings from July 2009

Re: [perl #67694] List::Util attaching to the wrong $_ when usedinside given/when construct

Thread Previous | Thread Next
From:
Bram
Date:
July 21, 2009 02:54
Subject:
Re: [perl #67694] List::Util attaching to the wrong $_ when usedinside given/when construct
Message ID:
20090721115444.r9amzzwzhco0g0cc@horde.wizbit.be
>
> -----------------------------------------------------------------
> [Please enter your report here]
>
> Please behold the following:

[code snipped]

> Note that when commenting out the lexical declaration of $_, the code behaves
> as expected.

No it doesn't. Or at least not for me. I suggest you check your test  
case again.


This seem to happen because given creates a lexical $_. It also  
happens when for/foreach is used with a lexical $_:


#!/usr/bin/perl -l

use strict;
use warnings;
use feature qw/switch/;

sub s1 (&) {
   my $code = shift;
   for ("a") {
     print "\ts1:\t \$_ = " . \$_ . " (value: $_)";
     $code->();
   }
}

print "Given/when block:";
my $val = 1;
given ($val) {
   when (1) {
     print "\twhen-1:\t \$_ = " . \$_ . " (value: $_)";
     s1 { print "\tblock:\t \$_ = " . \$_ . " (value: $_)"; };
     print "\twhen-2:\t \$_ = " . \$_ . " (value: $_)";
   }
}

print "";
print "For loop";
for (1) {
   print "\tfor-1:\t \$_ = " . \$_ . " (value: $_)";
   s1 { print "\tblock:\t \$_ = " . \$_ . " (value: $_)"; };
   print "\tfor-2:\t \$_ = " . \$_ . " (value: $_)";
}


print "";
print "For loop with lexical \$_";
for my $_ (1) {
   print "\tfor-1:\t \$_ = " . \$_ . " (value: $_)";
   s1 { print "\tblock:\t \$_ = " . \$_ . " (value: $_)"; };
   print "\tfor-2:\t \$_ = " . \$_ . " (value: $_)";
}
__END__

$ perl-5.10.0 rt-67694.pl
Given/when block:
         when-1:  $_ = SCALAR(0x994f768) (value: 1)
         s1:      $_ = SCALAR(0x994f478) (value: a)
         block:   $_ = SCALAR(0x994f768) (value: 1)
         when-2:  $_ = SCALAR(0x994f768) (value: 1)

For loop
         for-1:   $_ = SCALAR(0x994ed08) (value: 1)
         s1:      $_ = SCALAR(0x994f478) (value: a)
         block:   $_ = SCALAR(0x994f478) (value: a)
         for-2:   $_ = SCALAR(0x994ed08) (value: 1)

For loop with lexical $_
         for-1:   $_ = SCALAR(0x994e848) (value: 1)
         s1:      $_ = SCALAR(0x994f478) (value: a)
         block:   $_ = SCALAR(0x994e848) (value: 1)
         for-2:   $_ = SCALAR(0x994e848) (value: 1)



Best regards,

Bram



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