in reply to passing a variable from a link

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

Replies are listed 'Best First'.
Re: Re: passing a variable from a link
by Ardenstone (Acolyte) on Nov 15, 2001 at 07:10 UTC
    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