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 davido (Cardinal) on Jun 27, 2004 at 03: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

Re: How to reference variables in another package via keyword names
by gmpassos (Priest) on Jun 26, 2004 at 21:51 UTC
    This,
    $in::$_
    is not a valid syntax!

    You should write:

    ${"in::$_"}

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

      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
      Yep ... I wasn't thinking properly. THANK YOU! Seriously. To the first poster: incorrect ... that list is simply the keywords, not the vars themselves.
Re: How to reference variables in another package via keyword names
by keszler (Priest) on Jun 26, 2004 at 21:40 UTC
    foreach(@in::keywords) { $_ =~s/asdf/fdsa/g; }
    foreach sets $_
Re: How to reference variables in another package via keyword names
by BUU (Prior) on Jun 27, 2004 at 02: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?