develooper Front page | perl.perl5.porters | Postings from June 2011

Why doesn't do-given pass back values in lexical variables declared in given?

Thread Next
From:
brian d foy
Date:
June 23, 2011 17:04
Subject:
Why doesn't do-given pass back values in lexical variables declared in given?
Message ID:
230620111904581022%brian.d.foy@gmail.com
Perl 5.14 added a return value to `given` if you put it inside a do.
However, it looks like that return value can't be a value in a lexical
variable the you define inside the `given`:

   #!/usr/local/perls/perl-5.14.1/bin/perl
   
   use 5.014;
   
   my $foo = 'foo!';
   
   foreach my $string ( qw(foo bar baz quux hoo) ) {
      my $got = do {
         my $bar = 'bar!';
         given( $string ) {
            my $baz = 'baz!';
   
            when( 'quux' )    { my $quux = 'quux!' }
            when( 'foo' )     { $foo               }
            when( 'bar' )     { $bar               }
            when( 'baz' )     { $baz               }
            default           { 'default'          }
            }
         };
   
      say "$string got: $got";
      }

This gives me:

   foo got: foo!
   bar got: bar!
   baz got: 
   quux got: 
   hoo got: default

The two values that don't come back are the ones declared inside the
`given`.

What am I doing wrong there?

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