Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Short answer: you can't do that; you shouldn't be doing that.

Long answer: you can do it, sometimes; but it's not really a good idea.

For variables in the symbol table, you can find this information, by recursing through the symbol table, with something like this (this example only works on scalars, extending it should be relatively simple, see also perlref):

# This code is not tested. Sorry. sub _find_var_loop { my($hash,$prefix,$var) = @_; my @results; # go through each entry in our symbol table hash... while (my($name,$glob) = each %$hash ) { if ($var eq \${ $glob }) { push @results, $perfix.$var; # add $var to our list } # is it another symbol table hash? # (ignore main:: to not infinite loop) if ($name =~ /::\z/ and $name ne 'main::') { # recurse... push @results, _find_var_loop(\%{ $glob }, $prefix.$name, $var); } } return @results; } sub find_var { # usage: my @names = find_var($var); return _find_var_loop(\%main::, "", \$_[0]) }

This function will find all variables with a certain values in the symbol table, by comparing the "SCALAR(0xDEADBEEF)"-like strings you get when you stringify references. (See also perlref). Note that I say all; there can be multiple copies of a variable in the symbol table when Exporter or the *glob = \$variable or *glob1 = *glob2 syntaxes are used. (This function returns the variable names from the prespective of the main package, since that's where it starts searching from, and I didn't want to have all the results have :: or main:: on the front.)

However, this all won't work for my'd variables, which are generally preferred in perl code. You could probably do my'd variables with some C code, but I wouldn't count on it not breaking in future versions of perl. (There is nothing in the public API to do that.)

Using variables as variable names is, in general, a very bad idea. See this set of articles. So, please, don't do that. You can, with a few rare exceptions, always use (possibly nested) data structures instead of variables as variable names.

update: I realize (impossiblerobot's very closely spaced post reminded me) I didn't emphasize enough that this is going to inefficient. Searching through the symbol table is that way, and that's another reason why it's best left avoided.


In reply to Re: getting at the text of an icon by wog
in thread getting at the text of an icon by moo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found