Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl: the Markov chain saw
 
PerlMonks  

How to reference variables in another package via keyword names

by theAcolyte (Pilgrim)
on Jun 26, 2004 at 17:26 UTC ( [id://369877]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Okay ... I'm positive that this is likely simple and my brain is just not working today ...

I'm using CGI.pm ... and calling import_names('in') which creates a package namespace for incoming CGI vals so you don't have to worry about name space collisions.

You get a list of keywords that came in via @in::keywords, and can access the variables via $in::keyword.

so ... why does the following produce an error:

foreach(@in::keywords) { $in::$_ =~s/asdf/fdsa/g; }

I can't imagine how else you'd access the vars created by @in::keywords ... and if this doesn't work, why provide @in::keywords at all? I'm perplexed ... anyone help me out?

- Erik

Replies are listed 'Best First'.
Re: How to reference variables in another package via keyword names
by keszler (Priest) on Jun 26, 2004 at 17:40 UTC
    foreach(@in::keywords) { $_ =~s/asdf/fdsa/g; }
    foreach sets $_
Re: How to reference variables in another package via keyword names
by gmpassos (Priest) on Jun 26, 2004 at 17:51 UTC
    This,
    $in::$_
    is not a valid syntax!

    You should write:

    ${"in::$_"}

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      Yep ... I wasn't thinking properly. THANK YOU! Seriously. To the first poster: incorrect ... that list is simply the keywords, not the vars themselves.
      If you're using strict (which you ought to be), then you'll also have to wrap that with no strict refs, since that uses symbolic references.

      You could also just use an eval if you wanted to avoid symbolic references all together.

      ------------ :Wq Not an editor command: Wq
Re: How to reference variables in another package via keyword names
by BUU (Prior) on Jun 26, 2004 at 22:40 UTC
    Is there any particular reason you don't just use "CGI::params()" or something? Use a hash! Why bother with extra namespace, requiring name space hackery and such like?
Re: How to reference variables in another package via keyword names
by davido (Cardinal) on Jun 26, 2004 at 23:17 UTC

    While using a package for an additional name-space is a good use of packages, I disagree with the practice of ever using symbolic references to create variable names from input that came from the outside world (ie, CGI).

    You're doing two of the most dangerous possible things in one fell swoop: using symbolic refs, and taking user input. The latter cannot be avoided, but striking matches in the vicinity of gasolene really is not the safest way to illuminate the inside of the gas can.

    You will save yourself a lot of headaches by taking a few minutes to read the following three eye-opening links:

    And for a quick reminder of just how bad things can get if you botch web-security: Company hacks through my Perl's Website Security hole.

    One of the best ways to handle CGI parameters is to use the CGI.pm module's param() method, and store your parameters in a hash.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://369877]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.