in reply to Re^2: Reading and producing variable lists (w/o hashes!)
in thread Reading and producing variable lists (w/o hashes!)
Maybe something like this?
my @one = ('$apple', '$pear'); my $two = '$orange'; eval "$one[0] = 'hello'"; # evaluates $apple = 'hello' eval "$two = 'goodbye'"; # evaluates $orange = 'goodbye' print "$apple $orange"; # hello goodbye
(Note the single quotes in '$apple' etc. With double quotes, the (empty) global variable $apple would be interpolated.)
Update: Another way would be to use symbolic references:
use strict; use warnings; my @one = qw($apple $pear); my $two = '$orange'; our ($apple, $pear, $orange); { no strict 'refs'; ${substr $one[0],1} = 'hello'; # ${'apple'} = 'hello'; ${substr $two,1} = 'goodbye'; # ${'orange'} = 'goodbye'; } print "$apple $orange"; # hello goodbye
(You'd need package variables and no strict 'refs' in this case, though)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Reading and producing variable lists (w/o hashes!)
by why_bird (Pilgrim) on May 01, 2009 at 10:31 UTC | |
by almut (Canon) on May 01, 2009 at 10:37 UTC |