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

I want to know if there is some way for me to grab more than one varaible using the param method. I know I can grab all of them like this
@values = $q->param;
but there are specific param values I want to grab I know I can grab them singly with
$value = $q->param('foo');
I need something that will something like this for me
$values = $q->param('foo','foo2');
OR let me just show you my code and what I'm trying to do;
# configuration variables my $path = '/saw3/cgi-bin/BXJOH27/'; my $job_file = 'job.txt'; my $location_file = 'Inv_CLLI.txt'; # assign form data to variables my $Job = $q->param('Job_ID'); my $Location = $q->param('Inv_CLLI'); # print our data to a file print_to_file("$path/$job_file", $Job); print_to_file("$path/$location_file", $Location); # print data to file sub print_to_file { my $file = shift; my $data = shift; open (FILE, ">>$file") || die "Unable to open file: $!"; # flock (FILE, LOCK_EX) || # die "Can't get an exclusive lock on $file: $!"; print FILE "$data\n"; close FILE; }
I need $Job to hold a few different form variables so that when I print to file they print on the same line separated by colons or something. Help me please!! Thanks

Replies are listed 'Best First'.
map is your friend
by marcink (Monk) on Jun 08, 2001 at 00:29 UTC
    ... and don't you forget it ;-)

    You can load many form variables to an array like this:
    my @array = map { $q->param($_) } ( 'Job_ID', 'Inv_CLLI' );

    Or into a hash:
    my %h = map { $_ => $q->param($_) } ( 'Job_ID', 'Inv_CLLI' );

    Then print a set of values in one line using join:
    print join ',', @array;


    -mk
Re: cgi.pm param method help
by arturo (Vicar) on Jun 08, 2001 at 00:26 UTC

    With no error checking or untainting,and assuming you know the names you want ahead of time (and heck, even if you don't, you can construct the list, it's just a little more work):

    my @params_to_join = qw(foo bar baz); my $Job = join ":", map {$q->param($_)} @params_to_join;

    Or, even more concisely:

    my $Job = join ":", map {$q->param($_)} qw(foo bar baz);

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: cgi.pm param method help
by tachyon (Chancellor) on Jun 08, 2001 at 00:30 UTC

    You can do it this way

    tachyon

    my @params = qw(foo too you); my @values; push @values,$q->param($_) for @params; my $line = join ":",@values; print FILE "$line\n";

    You can avoid the @params array and the join by doing it this way:

    my @values; { push @values,$q->param($_) for qw(foo too you); #set print seperator to ":" just for this block local $"=":"; print FILE "@values\n"; }

    Note we use a bare block and local to scope the change of the value of $" to just this block to avoid any nasty suprises elsewhere.

Re: cgi.pm param method help
by da (Friar) on Jun 08, 2001 at 00:29 UTC
    For a very brief list of parameters, I'd go for string concatenation - simple is good too.
    my $job = $q->param('Job_ID') . ':' . $q->param('Job_param2') . ':' . $q->param('Job_param3');

    ___
    -DA

Re: cgi.pm param method help
by bikeNomad (Priest) on Jun 08, 2001 at 00:32 UTC
    warning: I am not now, nor have I ever been a CGI programmer

    Looking at the CGI manual page, I see the import_names method.

    It looks like you could go:

    $q->import_names('Job'); # now $Job::Job_ID and $Job::Inv_CLLI should be set to whatever param( +) would have returned.
Re: cgi.pm param method help
by new_2_perl (Acolyte) on Jun 08, 2001 at 02:45 UTC
    ok thanks guys!