Closed Thread
Results 1 to 13 of 13

Thread: help with perl

  1. #1
    im writing a simple script and i need to know how u make it so when u put like http://mydomain/cgi/script.cgi?something
    that it would go to the subroutine in the script that corrosponds with that u type in after the ?
    any and all help is apreciated

  2. #2
    Junior Member rpalmer16 is an unknown quantity at this point
    Join Date
    Jan 2001
    Posts
    1
    The stuff after the ? is stored in an environment variable called query_string. To store what's in the query_string to a variable, you can do this:

    $variable=$ENV{'QUERY_STRING'};

    By using an IF statement along with the query_string, you can go to a corresponding subroutine. Something like this:

    $query=$ENV{'QUERY_STRING'};
    if ($query eq "main") {
    &main;
    }
    if ($query eq "contact") {
    &contact;
    }

    -Ryan Palmer



  3. #3
    Junior Member perlboy is an unknown quantity at this point
    Join Date
    Nov 2000
    Location
    Gladstone, QLD, Australia
    Posts
    15
    Is it possible that you could use &{$ENV{'QUERY_STRING'})??

    Don't know I have never tried it but I do know that you are able to define names of variables by simply doing ${$nam_value}

    That way you may find you can implement it in callings subroutines etc. Probably a pretty bad idea though considering that it will Internal 500 if you call a non existant sub.


    Anyways.

    See ya

    STuart Low
    thedude@perlboy.org
    --------
    Perlboy.org - Professional Perl programming
    --------

  4. #4
    if u do it with

    $query=$ENV{'QUERY_STRING'};
    if ($query eq "main") {
    &main;
    }
    if ($query eq "contact") {
    &contact;
    }

    how would u make it so if u called sumthing that wasn't there u could redirect it to anotha sub routine like an error subroutine

    dont mind me im a moron when it comes to this

  5. #5
    Pro Member cds is an unknown quantity at this point
    Join Date
    Nov 2000
    Posts
    260
    A simple way is to do a series of elseif statements such as:

    $query=$ENV{'QUERY_STRING'};
    if ($query eq "main")
    &main;
    elsif ($query eq "contact")
    &contact;
    elsif ($query eq "bob")
    &something;
    else
    &default_error_message;

    You can even do a case statement if that makes you feel better

    If you want to just check if its empty..easy...

    if($query eq "") ...

    Hope this helps...

  6. #6
    i keep gettin 500 errors
    why?

  7. #7
    Pro Member cds is an unknown quantity at this point
    Join Date
    Nov 2000
    Posts
    260
    I think the best way to learn is to learn by example...

    So here is a fully working piece of code that works by itself..just put it in a file of its own... :


    ------start code------

    #!/usr/local/bin/perl

    print "Content-type: text/html\n\n";

    $query = $ENV{'QUERY_STRING'};

    if ($query eq "test") {
    print "test";
    }
    elsif ($query eq "tester") {
    print "oknow";
    }
    elsif ($query eq "main") {
    print "main";
    }
    elsif ($query eq "contact") {
    print "contact";
    }
    elsif ($query eq "bob") {
    print "bob";
    }
    else {
    print "error page";
    }

    ------end code------

    **** Make sure that you CHMOD the file to be 755 and that the extension is either .cgi or .pl and it should work. You can then modify it to fit your needs...

  8. #8
    NLC Nick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to beholdNick is a splendid one to behold Nick's Avatar
    Join Date
    Oct 2000
    Location
    United States
    Posts
    3,493

    Exclamation Tip of advice

    LawTown Junky:

    I'm not sure what you're using this script for, but keep in mind that if you're doing this to display html pages do not put all the HTML code in the perl script because it has to completely read the script to execute. So use require to open the file and print it.

    I made this mistake before and it slowed the site down really bad.
    Nick

  9. #9
    Member Jerry is an unknown quantity at this point
    Join Date
    Nov 2000
    Location
    CA, USA
    Posts
    72
    the first example given by cds looks more like java/javascript than perl.. all perl if-else statements use brackets unless trailing a single statement.. thats why it gave 500 errors

    #!/usr/bin/perl

    $go = $ENV{QUERY_STRING};

    print "Content-type: text/html\n\n";
    if ($go eq "contact") {
    &contact ();
    }
    elsif ($go eq "downloads") {
    &downloads ();
    }
    else {
    &main ();
    }

    sub main {
    print qq~<html>
    <body>
    this is the main page
    </body>
    </html>~;
    }

    sub contact {
    print qq~<html>
    <body>
    this is the contact page
    </body>
    </html>~;
    }

    sub downloads {
    print qq~<html>
    <body>
    this is the download page
    </body>
    </html>~;
    }

    and just in case you put a @ sign or a ~ in between the qq~'s and ~;'s .. remember to put a \ in front of it.. there are a few more.. if you add some weird characters in there.. and get an error.. just put a backslash in front of it

  10. #10
    Member Jerry is an unknown quantity at this point
    Join Date
    Nov 2000
    Location
    CA, USA
    Posts
    72
    and if you want to do it nick's way..

    #!/usr/bin/perl

    $pwd = `pwd`;
    chomp $pwd;
    $file = $ENV{QUERY_STRING};

    unless (-e "$pwd/$file.txt") { $file = "main"; }

    print "Content-type: text/html\n\n";
    open FILE, "<$pwd/$file.txt";
    while (<FILE>) { print; }
    close FILE;

    put the html of any file you want in "file.txt"...

    ie: if they type ........blah.cgi?home

    make a home.txt with the html content for the home page

    of course you'll be doing more with whatever program you got in mind.. but as far as this code is.. it is extremely pointless.. because its like instead of making someone type "http://yourdomain.com/home.html", you make them type.. "http://yourdomain.com/file.cgi?home"

    if they type a page that doesn't exist.. it'll go to the home

  11. #11
    im jus messin around tryin to learn some stuff.
    and the otha day i wondered how you did that so i decided to find out. thanx for u guys help

  12. #12
    ok i got it to work with this :
    #!/usr/bin/perl

    $go = $ENV{QUERY_STRING};

    print "Content-type: text/html\n\n";
    if ($go eq "contact") {
    &contact ();
    }
    elsif ($go eq "downloads") {
    &downloads ();
    }
    else {
    &main ();
    }

    sub main {
    print qq~<html>
    <body>
    this is the main page
    </body>
    </html>~;
    }

    sub contact {
    print qq~<html>
    <body>
    this is the contact page
    </body>
    </html>~;
    }

    sub downloads {
    print qq~<html>
    <body>
    this is the download page
    </body>
    </html>~;
    }

    and yes it is extremly slow but now i know how to do it so i can add it in my stupid things i attempt to make.

  13. #13
    Senior Member Zef Hemel is an unknown quantity at this point
    Join Date
    Oct 2000
    Posts
    127
    Try this line:
    Code:
    eval(\&$ENV{'QUERY_STRING'}\;);
    That might do the trick

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts