[Cialug] perl problem
David Champion
dchampion at visionary.com
Wed Feb 8 19:19:28 CST 2006
I'm a total perl n00b, but here's something to consider...
If you add this:
my $test3 = join("\n", "foo => ${foo}", "bar => ${bar}");
... then print $test3, it displays the local scoped $foo and $bar. I'm
guessing it has something to do with the way it evaluates the ${$_}
thingy that's causing your scope issue, even though it looks like your
$test1 line with the map... in it should evaluate to the same thing as
my $test3.
Could you stuff the "foo bar" values into an array, then iterate that
and have it evalute the array contents, instead of using the $_
variable? Perl probably has a nice cryptic one-liner for that too.
-dc
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