Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Passing variable to another program

by deathmetalscottie (Initiate)
on Nov 27, 2012 at 15:24 UTC ( #1005875=perlquestion: print w/replies, xml ) Need Help??
deathmetalscottie has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am having a heck of a time trying to figure out how to pass a variable from index.cgi to sum.cgi. I have an online form that displays each student from /etc/passwd on the school server. I can pass "Last Month" and "This Month" but I am not sure how to pass the student name to the other file. Any help would be greatly appreciated

#!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; my $count = 3; my $line; my @list; my $ifile = "/etc/passwd"; my %user; my %name; my $key; open(my $IN, "<", $ifile); while(<$IN>) { chomp; my @data = split(/:/); $user{$data[0]} = $data[2]; my @tmp = split(/,/, $data[4]); $name{$data[0]} = $tmp[0]; } close($IN); print header(), start_html( -title=>'Account Summary'); print<<EOHTML; <!DOCTYPE html> <head> <title>Account Summary</title> </head> <body> <h1>Account Summary Gateway</h1> <p>Select which month(s)you would like to summarize: <form action="sum.cgi" method="get"> <input type="checkbox" name="ThisMonth" value="current +">This Month</br> <input type="checkbox" name="LastMonth" value="last">L +ast Month <p>Select one of the $count accounts to summarize:</p> <select name="$key" size="15" multiple="multiple"> EOHTML foreach my $key(sort keys %user) { if($user{$key} > 1001) { print"<option value=\"$key\">$key \($name{ +\"$key\"}\)</option><br />"; #print "$key $name{$key}\n"; } } print<<EOHTML; </select></br> <input type ="submit" name="sbutton" value="Go!"> </form> </body> EOHTML

Replies are listed 'Best First'.
Re: Passing variable to another program
by greengaroo (Hermit) on Nov 27, 2012 at 15:38 UTC

    It has been a while since I've done that but I think when you put the "multiple" option on a select box, you will receive an array of values on the other side. Try a print Dumper of the CGI params and you should be able to find the selected value(s).

    I think you would be able to do this: my @names = $cgi->param('name_of_select_box'); If $cgi is a CGI object. But try the Dumper first to make sure.

    Testing never proves the absence of faults, it only shows their presence.
Re: Passing variable to another program
by blue_cowdawg (Monsignor) on Nov 27, 2012 at 16:24 UTC

    Why pass the name? Use the UID that you are going to glean from /etc/password.

    Better yet why not just put the functionality of both CGI scripts into one CGI? This method is a good example of a justification to create modules.

    Update: I'm not a big fan of "pipe-fitting" processes together. I remember inheriting a project that suffered from performance issues what consisted of scriptA --pipe--> scriptB --pipe--> scriptC --> and nausuem and back to scriptA types of chains. First thing I did was create modules with the working "guts" of all the scripts and replaced all the scripts. Now if scriptA needed some functionality in scriptB it could access that functionality locally rather than piping.

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Passing variable to another program
by golux (Hermit) on Nov 27, 2012 at 17:28 UTC
    Hi deathmetalscottie,

    Make sure that you have a name defined for the <select> tag. In your code,you're using "$key", which you never define:

    my $key; ... <select name="$key" size="15" multiple="multiple">
    So if you fix that, for example, calling it "multi":
    my $name = "multi"; # Renamed from $key to be less confusing # Sorry, I don't want to show my /etc/password :) my %user = ( 'fred' => 101, 'barney' => 102, 'wilma' => 103, 'betty' => 104, 'pebbles' => 105, 'bambam' => 106, ); ... <select name="$name" size="15" multiple="multiple"> foreach my $key(sort keys %user) { my $val = $user{$key}; if ($val > 100) { print qq{<option value="$key">$key ($val)</option><br />}; } }
    Then you can see with a quick test which names were chosen, in sum.cgi, like this:
    #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; use Data::Dumper; my @users = param('multi'); die sprintf "Users selected: %s\n", Dumper(\@users);
    For example, selecting the 2 values "bambam (106)" and "pebbles (105)":
    *** bambam (106) **** barney (102) betty (104) fred (101) *** pebbles (105) *** wilma (103)
    and clicking "Go!" to call sum.cgi should show:
    Software error: Users selected: $VAR1 = [ 'betty', 'wilma' ];
    say  substr+lc crypt(qw $i3 SI$),4,5
Re: Passing variable to another program
by deathmetalscottie (Initiate) on Nov 27, 2012 at 19:56 UTC

    I actually figured this out and it was something rather small. In the line of code:

    <select name="$key" size="15" multiple="multiple">

    I replaced $key for just the name who

    <select name="who" size="15" multiple="multiple">

    That actually worked as I was able to pass over the variable. Although yes this would have been easier with one index.cgi file, this was a homework assignment so obviously my teacher is going to make us jump through hoops on the assignment and then go over best practice in class. Thanks guys.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005875]
Approved by toolic
[ovedpo15]: it isn't homework. its a module I use at work. The process to add additional module isn't worth it. p.s never heard a university course which teach perl
[marto]: there are many, to this day.
[ovedpo15]: @Corion Yes I use the regex you showed me. its very good regex. although now i need to know that substring unit that comma
marto mad corrections to one around 18 months ago
[ovedpo15]: my code checks the value after that comma, if it isn't valid it will remove it. so I would like to remove the substring after that comma meaning getting the string before comma.
[hippo]: Are you sure that Text::CSV_XS isn't already installed at your work? It's such a useful module that it might well be there.
[marto]: pointing out the advantages of cpan modules is well worth in, as both developer and $client/$company benefit greatly
[Tux]: $src =~ m{^(.*),(.*)$/ and $2 !~ $valid and $src = $1;
[Tux]: s,/,},

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-05-27 10:51 GMT
Find Nodes?
    Voting Booth?