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

All right, (a) I'm new at this. Nuff said. (b) I've looked in the CGI pod but didn't see an explaination. So, the question. I'm trying to expand a variable inside of a "param" call.
sub Handle_Media { my $name = shift; my @Files = $q->param("$name_files"); my @Delete = $q->param("$name_DEL"); more code here...
Thus, if I pass "LIBRARY" to the subroutine, I want to get all the values that are in the "LIBRARY_files" param and in the "LIBRARY_DEL" param. If I simply put    my @Files = $q->param("LIBRARY_files"); I get the list like I expected, so I know the param exists and has data in it. However, I need for this routine to be variable instead of fixed. I'm sure I must be missing something simple Thanks in advance. -- Filmo the Klown

Replies are listed 'Best First'.
Re: Expanding variables inside the CGI module
by mirod (Canon) on Mar 03, 2001 at 13:40 UTC

    I think your problem is that $name_files is interpreted as the name of a variable. You need to use either "$name\_files" or "${name}_files" or $name . '_files' so that the Perl interpreter knows what's part of the variable name and what's a string.

    If you had used use strict as you should _ALWAYS_ do, you would have gotten an error as the $name_files variable does not exist.

Re: Expanding variables inside the CGI module
by mr.nick (Chaplain) on Mar 03, 2001 at 20:53 UTC
    A simpler answer to your question is best describe with this example :):
    sub Handle_Media { my $name = shift; my @Files = $q->param($name."_files"); my @Delete = $q->param($name."_DEL"); ## more }
    Which is probably what you wanted, yes?

    still ... asleep ... must ... coffee ...

      Many thanks, mr.nick's was exactly what I'm looking for. Seems so obvious now, but isn't that always the case. And yes, I'm learning to use STRICT... -- Filmo the Klown
(boo) Re: Expanding variables inside the CGI module
by boo_radley (Parson) on Mar 03, 2001 at 20:37 UTC
    I suggest using the import_names feature of CGI, rather than trying to hack your way to something similar, but not quite as good. from CGI's POD :
    IMPORTING ALL PARAMETERS INTO A NAMESPACE: $query->import_names('R'); This creates a series of variables in the 'R' namespace. For example, $R::foo, @R:foo. For keyword lists, a variable @R::keywords will appear. If no namespace is given, this method will assume 'Q'. WARNING: don't import anything into 'main'; this is a major security risk!!!!
    pretty simple, huh? Just by putting $q->import_names just before you need to query them, you now have access to @Q::LIBRARY_files, @Q::BAKERY_files, @Q::HOSPITAL_files, or whatever else your CGI generates. If the 'Q' namespace isn't descriptive enough, you can $q->import_names("All_my_Parameters") and you'll have @All_my_Parameters::LIBRARY_files, and so on.
    Hope that helps.