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

Hello Monks

I'm tyring ot create some web-based resources to allow the data produce by the group that I'm working in to be more easily avalible to the rest of the scientific community. Been working on this particular tool for a couple of weeks. I have many of the subroutiines running in UINX using the key board. BUT I want to get them running from a webpage.

The code below is tested and runs as it should on UNIX with STDIN. I have become confused about How do I pass this variable as $user to the code below.... and how to pass the array back (but given the first bit of infromation I should be less confussed about the second.

Cheers for any help - the first part of the code is just back end stuff.... but it might help in getting to overall picture.

#!/usr/bin/perl use strict; use warnings; my $file = "p2iextact.txt"; my %p2i; my @user_array; my $sp; my $ipr; my $key; my $user; # creates hash of arrays -okay works open(FILE, "p2iextact.txt") || die "can't open file"; while(<FILE>) { chomp; ($sp, $ipr) = split; if (exists $p2i{$sp}) { push @{$p2i{$sp}}, $ipr; # bareword {sp} hours of error }else { $p2i{$sp}= [$ipr]; } } close FILE; # test to see if hash created correctly - works also foreach $key (sort keys %p2i) { print "$key\t @{$p2i{$key}}\n"; #print "$key\t$p2i{$key}\n"; } } #### code that needs work ###### ### get user to enter info $user an id #### print "Please enter an ID: "; chomp( $user = <STDIN>); $user = uc($user); if (exists $p2i{$user}) { print "$user is in the hash. \n"; print "$user: @{ $p2i{$user} }\n"; }else { print "$user is not in the hash.\n"; } exit; # Now I'm passing the $user id and their preference # about data managment and printing them back to the # screen for the user like this... print $query->header; print $query->start_html (-title=>'results', -style=>{'src'=>'/~campus1jle/JoGOCSS.css'} ); print "<p>The swissprot id you entered was: ", "<br />"; print $query->param('user'), "<br />";"<br />"; print "You have chosen to sort by", "<br />"; print $query->param('sort'), "<br />";"<br />"; # BUT for some reason I don't seem to be able to get # passing the $user to the code and passing back the # array working.

I have the feeling that this is a really generic question - but I can't quite see the wood for the trees at present having tried a couple of things that I was sure would work -then making random alterations to these thought though ideas in the hope that things might magical start working.

Cheers
Stalky

Edit by BazB: retitle from "CGI/PERL problem".

Replies are listed 'Best First'.
Re: Getting CGI parameters
by PodMaster (Abbot) on Jun 25, 2004 at 13:41 UTC
    use CGI;
    use CGI qw/:standard/; print header, start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ", textfield('name'), p, "What's the combination?", p, checkbox_group( -name => 'words', -values => [ 'eenie', 'meenie', 'minie', 'moe' ], -defaults => [ 'eenie', 'minie' ] ), p, "What's your favorite color? ", popup_menu( -name => 'color', -values => [ 'red', 'green', 'blue', 'chartreuse' ] ), p, submit, end_form, hr; if ( param() ) { print "Your name is", em( param('name') ), p, "The keywords are: " +, em( join( ", ", param('words') ) ), p, "Your favorite color is " +, em( param('color') ), hr; }
    See also Tutorials and Ovids Web Programming with Perl course.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Getting CGI parameters
by zentara (Cardinal) on Jun 25, 2004 at 14:16 UTC
    Ovid's tutorial is pretty good. The thing to remember with CGI is that the apache webserver(usually) is acting as an interface between your application and the remote user's browser. This replaces the STDIN-STDOUT which you are used to.

    So you need to set up forms which the remote user can fill in and send to the web server, which will relay it to your app. Now you can manually make your forms, or you can generate them dynamically with CGI.pm or with here-documents. I prefer to use here-docs myself. Here is a little cgi program which will display your form variables for testing.

    #!/usr/bin/perl use warnings; use strict; use CGI; my $cgi=new CGI; my %in = $cgi->Vars(); print "Content-type: text/html\n\n"; foreach my $key (keys %in){ print "$key -> $in{$key}<br>"; }

    Now you takes those form variables, process them with your application, and send them back out to apache, which relays them to the user's browser. You can display them anyways you like, from a simple text output, to a fancy html table. The output format can be generated manually( with here-docs) or you can use CGI.pm's features to generate output. The important thing is to observe the protocols for generating the right type of "header" so that apache will send it, and it will look like you want. The more advanced users will recommend using a templating system for producing your output, like HTML::Template.

    Generally, apache will accept your output if you start your script with the standard header:

    print "Content-type: text/html\n\n";
    then
    print 'here is all your output<br>'; print $output;
    Remember that the browser expects <br> instead of \n.

    I'm not really a human, but I play one on earth. flash japh