Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Variable Interpolation Interpolation

by dpatrick (Scribe)
on Mar 20, 2002 at 19:13 UTC ( [id://153088]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks, I humbly bring before you a piece of code that I have been looking at for about the past hour:

foreach my $f (@fields) { push(@where, qq($f='$fromQuery::{ $f }')) if $fromQuery::{ $f }; push(@searchString, qq($f=).escape($fromQuery::{ $f })) if $fromQuery::{ $f }; }


What I'm trying to do is interpolate $f into $fromQuery::$f so that we end up with $fromQuery:foo if $fromQuery::foo exists. Does anyone know how this could best be handled?

As an aside, the fromQuery namespace comes from the CGI::import_names object method but that's not really that important.

dpatrick
- I think scsh is cool.
Open Sourceror, Perlmonk
http://perlmonk.org/~dpatrick

Replies are listed 'Best First'.
Re: Variable Interpolation Interpolation
by broquaint (Abbot) on Mar 20, 2002 at 19:28 UTC
    Ack, symbolic references! Perhaps a better way would be to look in the symbol table of $fromQuery:: like so
    print "found $f!\n" if grep($_ eq $f, keys %fromQuery::);
    For further info on why symbolic references are generally a no-no check out Dominus' enlightening articles here, here and here.
    HTH

    broquaint

Re: Variable Interpolation Interpolation
by dragonchild (Archbishop) on Mar 20, 2002 at 19:15 UTC
    It's naughty to use symbolic refrences, but the way to do it is:
    if ${"fromQuery::$f"};

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Dragonchild, why is it naughty to use symbolic references?

      dpatrick
      - I think scsh is cool.
      Open Sourceror, Perlmonk
      http://perlmonk.org/~dpatrick
        Doing a search (which I'm too lazy to do) would yield some dozen or so threads on the matter.

        A quick answer is this - symbolic references directly impact the symbol table. This could result in clobbering a variable you already have. As this is completely dependent on run-time activity, debugging this could be complex.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Variable Interpolation Interpolation
by knobunc (Pilgrim) on Mar 20, 2002 at 19:42 UTC

    You can just eval the bit where you want to get the variable's value:

    foreach my $f (@fields) { my $val = eval qq{ \$fromQuery::$f }; next unless $val; push(\@where, $val); push(\@searchString, $val); }

    But I am sure that dragonchild's solution is faster. The only gotcha is that it won't run under strict but no strict 'refs'; in the correct scope will turn off that check:

    foreach my $f (@fields) { no strict 'refs'; # This will only last in this scope my $val = ${ "fromQuery::$f" }; next unless $val; push(\@where, $val); push(\@searchString, $val); }

    -ben

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-10-06 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (43 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.