in reply to Re: Reading and producing variable lists (w/o hashes!)
in thread Reading and producing variable lists (w/o hashes!)

ok, massively simplified example:
my $one = ("$apple", "$pear"); my $two = "$orange"; $$one->[0]= "hello"; $$two = "goodbye"; print "$apple $orange";
Neither of these work. I know it is something simple I am not doing.

Cheers!

Replies are listed 'Best First'.
Re^3: Reading and producing variable lists (w/o hashes!)
by almut (Canon) on May 01, 2009 at 10:25 UTC

    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)

      oo crazy cool.. Two questions though: what might be a 'good' place to use this? and does it run under 'strict'?
      ........
      Those are my principles. If you don't like them I have others.
      -- Groucho Marx
      .......
        and does it run under 'strict'?

        It would run under use strict, if you declare the variables, e.g.

        my ($apple, $pear, $orange); # lexicals # or our ($apple, $pear, $orange); # lexically scoped package variabl +es # or use vars qw($apple $pear $orange); # global package variables
        what might be a 'good' place to use this?

        There are almost certainly better ways to approach virtually any typical problem you might have... :)