in reply to Calling variable variables

Symbol tables are very much like hashes. You can get by without symbolic references if you know the package name at run-time:
use strict; package Foo; use vars qw( $foo ); $foo = 100; package Bar; use vars qw( $foo ); $foo = 200; package Baz; use vars qw( $foo ); $foo = 300; package main; my $package = (qw( Foo Bar Baz ))[ int rand 3 ]; my $foo = $main::{"${package}::"}{foo}; $foo = ${ *{$foo}{SCALAR} }; print "($foo)\n";
Of course, you'll have to do a little homework on glob slots (the SCALAR thingie), and you'll have more work to do if you have multi-level package names, but that should get you started.

Update: Technically, this is a symbolic reference. I should have said that you can use a symbolic reference that doesn't set off strict 'refs' alarms.

Replies are listed 'Best First'.
Re: Re: Calling variable variables
by erikharrison (Deacon) on May 26, 2002 at 07:33 UTC

    And if you DON'T know the package name at runtime . . .

    my $package; print ${$package . '::a' };

    But you shouldn't do that unless your TheDamian. ;-)

    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

    Update: You see, Perl, and computers in general, are a hobby for me - my favortive hobby, but a hobby. And when you take a little break from a hobby, sometimes your mind stales . . . and then if you get back to at some ridiculous hour of the morning, you can say stupid things. Moral? Ignore me, pay attention to chromatic.

      But you shouldn't do that unless your TheDamian. ;-)

      Why not? It's often the only way to get to those variables. In scriptinfo.pl, an Irssi script, I use symbolic references all the time, and I know of no other way to do this. Symbolic references are to be avoided, but you can't always.

      my $name = ${ "Irssi::Script::${data}::IRSSI" }{name}; my $url = ${ "Irssi::Script::${data}::IRSSI" }{url}; my $version = ${ "Irssi::Script::${data}::VERSION" };

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

      Um, runtime is the point at which your code executes, if you don't know the package then, it is impossible to do anything sensible.

      Read chromatic's code more carefully and try running it a few times. He did answer the question.