Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

http://www.url.com/cgi-bin/file.pl?node=how

by surrealistfashion (Acolyte)
on Sep 09, 2003 at 13:01 UTC ( [id://290016]=perlquestion: print w/replies, xml ) Need Help??

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

respected monks, I have been trying to call a sub from the url using CGI.pm, but my attempts have come up with nothing. I have tried using
http://myserver/cgi-bin/file.pl?node=Main
my $node; if ($node eq "Main") { &Main_page; } elsif ($node eq "Check") { &Check_page; } else { &Error_page; }
but I can't seem to call the correct sub from the url.
This scripts works well with info from a standard html form, but not form a link.
my $Page = param('Main'); if ($Page eq "Main") { &Main_Page; } else ...
How do I fix this problem?

in a surrealistFashion

Replies are listed 'Best First'.
Re: http://www.url.com/cgi-bin/file.pl?node=how
by smitz (Chaplain) on Sep 09, 2003 at 13:30 UTC
    Hi,
    Your first code snippet contains
    my $node; if ($node eq...
    Is this a typo? $node will always eq undef in this case. Did you mean
    my $node = param('node')
    You could do it something like this: (untested code):
    #!/usr/bin/perl use CGI; my $node = param('node'); my %lookup_table = (how => 'how_sub', foo => 'foo_sub'); if ($lookup_table{$node} ne '') { &{$lookup_table{$node}}; } else { print "Unknown sub called\n"; } sub how_sub {} sub foo_sub {}
    The %lookup_table and if routine makes sure users cant choose any old sub to run,
    just the ones you define in the lookup_table.

    Smitz

      I was under the impression that having it $node undef was ok, if I declared it in the Url.
      I will try the lookup_table when I am off work tonight.
      With your code does the param('node') come from the Url?
      I have more reading to do :D
      Thanks
        Hi surrealistfashion,

        Yes you do have some more reading to do :-) Check out the following:


        Although you have the sub name in your URI, and the CGI.pm module has access to it, you have not 'imported' it into your program.
        Just by declaring it with my doesn't automatically give it the value you woul like. This isn't PHP you know ;-)
        Also, I ran the code I posted, it should work fine.

        Smitz
Re: http://www.url.com/cgi-bin/file.pl?node=how
by Abigail-II (Bishop) on Sep 09, 2003 at 13:24 UTC
    Shouldn't that be:
    my $Page = param ("node");

    Abigail

      Yes, typo. In the script it is acually
      my $UserName = param ('UserNameBlock');
      I have a tendency to type too fast and not sleep enough. thanks for your help :)
Re: http://www.url.com/cgi-bin/file.pl?node=how
by naChoZ (Curate) on Sep 09, 2003 at 13:31 UTC
Re: http://www.url.com/cgi-bin/file.pl?node=how
by bear0053 (Hermit) on Sep 09, 2003 at 14:18 UTC
    Try this:
    use CGI; use CGI::Validate qw(:standard); my $Page = $cgi->param('node'); #$Page will now be assigned the value of the parameter node that was p +assed in the url
    There is also a way to ensure the param you are grabbing came from the url...i believe it is:
    $cgi->url_param('node'); or $cgi->param_url('node');
    This is useful if you want to ensure the param you are grabbing was posted to the script
Re: http://www.url.com/cgi-bin/file.pl?node=how
by bbfu (Curate) on Sep 09, 2003 at 22:06 UTC

    You might also want to check out CGI::Application.

    bbfu
    Black flowers blossom
    Fearless on my breath

      Thanks for all the help.
      Your help, alows me to manage professional and personell time in a very efficeint way.
      thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://290016]
Approved by smitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found