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

The main problem is getting param('blah') (using cgipm) to work in a print qq since this is not working I am changing all the params to vars outside of the print qq. I have tons of code asking for a key to a hash that no longer exists like $I{'blah'} and I need to change all of them to param('blah') but those do not work for me inside print qq. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Changing a variable name
by epoptai (Curate) on Jan 25, 2001 at 01:11 UTC
    I used to use arturo's method until merlyn hipped me to CGI.pm's import_names function, the great power of which is well summarized here.
      Careful how you paint that. My preferred means is just calling param as needed. If you insist on accessing them as variables, the above-referenced method is available, but wasteful.

      -- Randal L. Schwartz, Perl hacker

Re: Changing a variable name
by arturo (Vicar) on Jan 24, 2001 at 23:12 UTC

    Going with your clarification, you can do things with minimal mutilation (to your existing code) with the following trick:

    my %param; foreach ( param ) { $param{$_} = param($_); }

    %param is a hash whose keys are the parameter names and whose values are the corresponding values. Just change every param("foo") within the qq[] to $param{foo}.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Changing a variable name
by Blue (Hermit) on Jan 24, 2001 at 22:46 UTC
    We need more details to actually help. If you post the code and the errors we would have a much better chance to figure out what is happening.

    =Blue
    ...you might be eaten by a grue...

    P.S. If you do post code, put <code> and <\code> tags around it.

      I cant get params to print inside of a qq thats the first issue. Using cgipm
      print REG qq[ ABOUT YOU Last Name : param('Last_Name') First Name : param('First_Name') ];

        You may want to check out a smiliar thread, which would have pointed you at import_names() method mentioned amongst other things, but there are more tricks you may want to try ...

        ... @{[ ]}-voodoo to embed arbritrary expressions inside strings.

        print REG qq[; Last Name : @{[ param('Last_Name') ]} First Name : @{[ param('First_Name') ]} ]

        ... use printf() to move the param()'s outside your string.

        printf REG qq[Last Name : %s\nFirst Name : %s\n], param('Last_Name'), param('First_Name'); ## ... or ... printf REG <<__TEXT__, param('Last_Name'), param('First_Name'); Last Name : %s First Name : %s __TEXT__

        ... resort to the much underused (?) formats if you like to see a little more clearly what goes where.

        format REG = Last Name : @<<<<<<<<<<<<<<<<<<<<<<<< param('Last_Name') First Name : @<<<<<<<<<<<<<<<<<<<<<<<< param('First_Name') . write REG;

        The downside to these are you need to be mindful of what param() is returning as a multi-value param() could throw things out of whack if you're only expecting a single value, something import_names() neatly sidesteps by providing both scalars and arrays of the same param() to make context explicit.

            --k.


        Since CGI.pm is a module, not built in, Perl has no way of knowing that those params are variables! Perl will only interpolate a scalar variable when it starts with $. Try one of these:
        way one: $Last = param('Last_Name'); print "Your last name is: $last\n"; way two, using the . (concentation operator): print "Your last name is: ".param('Last_Name')."\n";


        When's the last time you used duct tape on a duct? --Larry Wall