blahblah has asked for the wisdom of the Perl Monks concerning the following question:

I kind of asked this question at the beginning of the week or so, but received no answers, so I'm asking a different way in the hopes that someone will answer. Sometimes the question is wrong...

So, back in 2000 someone asked how to make variably named packages.

My question is:
When you got back into main:: how would you call $Foo::a?

If you called it with:
print $Foo::a;
That would work... BUT, its likely you wouldn't know the name of the package since it was variable. So you might try:
my $package = $input{somepackage}; # Foo was passed to input print $$package::a;
but that wouldn't work. So you might try:
$pleasework = eval{$$package::a}; print $pleasework;
but that wouldn't work either, so you might go insane for a week and then ask the monks again! ;)

thanks, alex

Replies are listed 'Best First'.
Re: Calling variable variables
by chromatic (Archbishop) on May 26, 2002 at 06:48 UTC
    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.

      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.