[Cialug] perl problem

Jerry Weida jweida at gmail.com
Wed Feb 8 21:19:31 CST 2006


I'm not sure why it's this way (I'm far from a Perl expert), but the  
syntax of ${foo} will yield the local variable while ${"foo"} (note  
the quoting around foo) will yield the global variable.  I'm not sure  
why this may be,  but I'm sure someone with my Perl experience on the  
list may be able to shed some light on this mystery.

Jerry


On Feb 8, 2006, at 5:58 PM, Renegade Muskrat wrote:

> I have a question regarding Perl. I am having trouble figuring out the
> variable scoping in a script. I've reduced the problem to as simple of
> a code snippet as i can in order to demonstrate it. In the script  
> below
> i believe $test1 and $test2 should be the same. But they are not. Any
> ideas why not? And how to make both of them display the local  
> variables? Or a better place to ask Perl questions?
>
>
> The script:
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> our $foo = "global foo";
>
> {
>     my $foo = "local foo";
>     my $bar = "local bar";
>
>     no strict 'refs';
>     my $test1 = join("\n", map { "$_ => ${$_}"; } qw(foo bar));
>     my $test2 = join("\n", "foo => $foo", "bar => $bar");
>
>     print "$test1\n----------\n$test2\n";
> }
>
>
> The output:
>
>
> Use of uninitialized value in concatenation (.) or string at
> ./scratch line 12.
> foo => global foo
> bar =>
> ----------
> foo => local foo
> bar => local bar
>
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug



More information about the Cialug mailing list