[ale] perl question

Calvin Harrigan charriglists at bellsouth.net
Mon Jan 16 16:20:51 EST 2012


On 1/16/2012 3:59 PM, Ed Cashin wrote:
> On Mon, Jan 16, 2012 at 2:45 PM, Geoffrey Myers
> <lists at serioustechnology.com <mailto:lists at serioustechnology.com>> wrote:
>
>     Okay, so why is it that $foo is empty inside the sub bar() ??
>
>
> I think the key is "its own copy" in this paragraph from the perlfunc
> manpage:
>
>         Unlike dynamic variables created by the "local" operator, lexical
>         variables declared with "my" are totally hidden from the outside
> world,
>         including any called subroutines.  This is true if itâs the same
>         subroutine called from itself or elsewhere--every call gets its own
>         copy.
>
> Keep in mind that the my $foo inside of the bar function is created when
> the file is parsed, before the for loop runs.  Setting the values
> initially helps me see what's going on in your example, as does "perl
> -w".  If I understand the question completely, I think if you play with
> closures some you will feel completely comfortable with this.

Doesn't the 'my' in this case make the $foo, etc local to the entire 
file?  I played with it, just for kicks and seems like the problem is 
indeed with the foreach.

Try the following to see what I mean.  "dog" is printed in both calls to 
bar().


#!/usr/bin/perl -w

my $bar;
my $foo = "dog";


foreach my $foo ("test", "another") {

      $bar = $foo;
      print "before: bar <$bar> foo <$foo>\n";
      bar();
}


sub bar
{
      print "in bar(): bar <$bar> foo <$foo>\n";
}



>
> --
>    Ed Cashin <ecashin at noserose.net <mailto:ecashin at noserose.net>>
> http://noserose.net/e/
> http://www.coraid.com/
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo



More information about the Ale mailing list