[Cialug] perl problem
Jerry Weida
jweida at gmail.com
Thu Feb 9 07:29:31 CST 2006
No, it's not the map call. It's definitely the quoting within the
braces around the variable name that seems to be causing this
behavior. I'll research it a bit more in my Perl books (maybe even
check out perlmonks.org) and see if I can find an explaination
besides my "it's that way because it was designed that way" one.
The following code will produce the same problems:
#!/usr/bin/perl
use strict;
use warnings;
our $foo = "global foo";
{
my $foo = "local foo";
print '${foo} = ' . ${foo} . "\n"; # Produces "local foo"
print '$("foo"} = ' . ${"foo"} . "\n"; # Produces "global foo"
}
On Feb 8, 2006, at 9:51 PM, Tim Wilson wrote:
> My guess is because of the map call using a block. Since you have
> another block, you're referencing the global vars instead of the
> local vars, because the local vars were in a different block. Just
> a guess.
>
> On 2/8/06, Renegade Muskrat <dramaley at spatulacity.cx> 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
>
>
>
> --
> Tim
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cialug.org/pipermail/cialug/attachments/20060209/50330218/attachment.htm
More information about the Cialug
mailing list