Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Dynamic Symbol Table access

by eak (Monk)
on Sep 22, 2003 at 02:34 UTC ( #293068=perlquestion: print w/replies, xml ) Need Help??

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

If have a a package named Foo::Test with a symbol table entry $URL="", is it possible to print out $URL if you have just a string my $x = "Foo::Test"?

I know $Foo::Test::URL will do this, but I want something dynamic.


Replies are listed 'Best First'.
Re: Dynamic Symbol Table access
by PodMaster (Abbot) on Sep 22, 2003 at 02:54 UTC
Re: Dynamic Symbol Table access
by tachyon (Chancellor) on Sep 22, 2003 at 02:47 UTC

    No. How on earth is Perl expected to go from "Foo::Test" which only specifies a package to finding your $URL var within that package. There is simply not enough infomation. Why should it select $URL? Why not %ENV @ISA $DWIM ....? Perl is good but it is not psychic....but more seriously.....

    Perhaps you might like to explain what it is you are trying to do (besides use symbolic references which is almost certainly a bad idea and generally unnecessary). There are probably a lot of better ways to do what you want than what you seem to be planning. Anyway this is how to do it (if you must).....

    package Foo::Test; $URL = " +htm"; $x = "Foo::Test"; print ${"${x}::URL"};

    Also $x must be a GLOBAL for you to be able to access it outside the package. You can't get at a package lexical* (delared with my) from outside the package it is declared in. Globals are another thing best avoided, although there are of course exceptions to every rule.

    * Technically you can get at them but certainly not easily.




Re: Dynamic Symbol Table access
by Zaxo (Archbishop) on Sep 22, 2003 at 03:14 UTC

    A couple of remarks. You can use symbolic references if you must, and still keep stricture on most of your program by calling { no strict 'refs'; ... } in as small a scope as you can manage.

    The symbol table is a hash, and you can examine its keys with keys %Foo::Test::. To get that hash from the string you have, you must again indulge in symrefs,

    use Foo::Test; my $x = 'Foo::Test' my @syms = do { no strict 'refs'; keys %{$x . '::'}; };

    After Compline,

      To get that hash from the string you have, you must again indulge in symrefs

      You can do it with real hash keys, if you know the secret:

      package Foo::Bar::Baz; use vars '$quux'; print "What should \$quux hold?\n: "; chomp( $quux = <STDIN> ); package main; use strict; my $hash = \%main::; for my $key (qw( Foo Bar Baz )) { $hash = $hash->{ $key . '::' }; } print "Quux is: ${ $hash->{quux} }\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://293068]
Approved by jonnyfolk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-01-21 00:14 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.