For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > How do I scope a variable if the variable name contains a variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I scope a variable if the variable name contains a variable?
David Filmer

2004-05-18, 9:31 pm

I can do this:

my @bar; #explicit name - fine

but not this:

my $foo = 'bar'; #fine
my @{$foo}; #oops

Perl complains, "Can't declare array dereference..."

Does anybody know the syntax to scope a variable name that includes a variable?

Thanks!
Jim Cochrane

2004-05-18, 10:31 pm

In article <e4c916dd.0405181656.5d92e96e@posting.google.com>, David Filmer wrote:
> I can do this:
>
> my @bar; #explicit name - fine
>
> but not this:
>
> my $foo = 'bar'; #fine



> my @{$foo}; #oops


The above is how you deference a reference, which you can define with,
e.g.:

my $f = [1, 2, 3];

and use, e.g.:

print "@{$f}\n";

or, in this case:

print "@$f\n";

Is this what you're looking for?

>
> Perl complains, "Can't declare array dereference..."
>
> Does anybody know the syntax to scope a variable name that includes a variable?
>
> Thanks!



--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com