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

Hi, I know that this must be simple, but I have never done it and can't get it to work. I need to be able to let someone click on a link, that will take them to a form with the value of the link put into a text box for them. This is what I have:
The link: http://www.whatever.com/cgi-bin/variabletest.pl?animal=dog and the script: #!/usr/bin/perl $pet = $formdata{'animal'}; print "Content-type: text/html\n\n"; print <<STUFF; <html> <head></head> <body> <center> <form><input type="text" value="$pet" size="25"></form> </body> </html> STUFF
what am I doing wrong? :)

Replies are listed 'Best First'.
Re: passing a variable from a link
by Rich36 (Chaplain) on Nov 15, 2001 at 03:51 UTC
    Use the CGI module
    Use the CGI module. The problem that you have here is that you have to interface with the URL that's being passed to the script. One way to do that with CGI is
    #!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; $pet = $q->param{ "animal" }; # Instead of # print "Content-type: text/html\n\n"; # use... print $q->header( "text/html" ); print <<STUFF; <html> <head></head> <body> <center> <form><input type="text" value="$pet" size="25"></form> </body> </html> STUFF
    The code $q->param{ "animal" }; finds the part of the URL passed to the script that contains the value of the form element "animal".
    It's suggested that you use strict and the -T switch to check for tainted variables perlsec.
    Run a Super Search for plenty of CGI info or check out the O'Reilly book CGI Programming with Perl
    Rich36
    There's more than one way to screw it up...

    Updated with $q->header( "text/html: ); - thanks jeffa
Re: passing a variable from a link
by Zaxo (Archbishop) on Nov 15, 2001 at 03:51 UTC
    use strict; use CGI; my $query = new CGI; my $pet = $query->param{'animal'}; $| = 1; print $query->header();

    There was nothing populating the %formdata hash.

    Update: added the canonical header and autoflush, thanks for the suggestion to jeffa.

    After Compline,
    Zaxo

Re: passing a variable from a link
by Flame (Deacon) on Nov 15, 2001 at 03:56 UTC
    Unlike PHP variables rescieved that way are not automatically parsed. If you are only passing one variable, you can try this:

    The Link: http://whatever.com/cgi-bin/variable.pl?dog

    The code:
    #!/usr/bin/perl use strict; my $pet = $ENV{'QUERY_STRING'}; print "Content-type: text/html\n\n"; print qq~<html> <head></head> <body> <center> <form><input type="text" value="$pet" size="25"></form> </body> </html>~;


    I won't bother to mention CGI.pm, as I see others have already done so.


    -----BEGIN GEEK CODE BLOCK-----
    Version: 3.12
    GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
    PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
    ------END GEEK CODE BLOCK------
    Translate

    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

      A slight correction: the above code is going to print animal=pet. If you want to not use CGI.pm (bad, evil, you've been warned, etc.), then $pet is going to contain:
      variable1=value1&variable2=value2...
      Some fairly rudimentary parsing can get you what you need. However, then you might need to worry about how HTML encodes special characters (what if the user enters "<lephant" or something else silly?) which isn't too bad but you're quickly seeing why CGI.pm is probably a better choice.
      Cheers,
      Ardenstone

      Update:
      Here is some ugly not use strict-able code (it creates variables on the fly) that can parse out the environment string:

      $myTmp = $ENV{'QUERY_STRING'}; @words = split '&',$myTmp; my $counter = 0; while ($words[$counter]) { my @tmpWords = split '=',$words[$counter]; $tmpWords[1] =~ tr/+/ /; $tmpWords[1] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; my $tmpName = $tmpWords[0]; $$tmpName = $tmpWords[1]; $counter++; }
      Now that's ugly. But it should work.
        Umm, did you notice that I had changed the way the link was used?

        It said "http://whatever.com/cgi-bin/variable.pl?dog"

        So the query string would be "dog" not "animal=dog"

        And, as I said, that method only really works when you are using just one variable.


        -----BEGIN GEEK CODE BLOCK-----
        Version: 3.12
        GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
        PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
        ------END GEEK CODE BLOCK------
        Translate

        "Weird things happen, get used to it."

        Flame ~ Lead Programmer: GMS