CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
call subroutine from url



Dec 24, 2000, 8:35 PM

Post #1 of 5 (557 views)
call subroutine from url Can't Post

is there any way to call a subroutine from a url?

User / Moderator

Dec 25, 2000, 11:46 AM

Post #2 of 5 (551 views)
Re: call subroutine from url [In reply to] Can't Post

It is not pretty, but you could try something like this...


my %cgi_data = &parse_form (); #using the form parser of your choice

if ($cgi_data{'subroutine'} eq 'Fred') { &Fred (); }
elsif ($cgi_data{'subroutine'} eq 'Wilma') { &Wilma (); }

sub Fred
#some stuff here

sub Wilma
#some stuff here

And then call it as...


The line with &parse_form will probably have to change depending on how you choose to do such things and your views on, but that is left as an exercise for the reader.


Dec 26, 2000, 5:02 AM

Post #3 of 5 (545 views)
Re: call subroutine from url [In reply to] Can't Post

what is () for when you let it what sub to use "&Fred ();" it looks like a javascript function??

also how would you call two sub's?
I'm thinking &Fred && &Wilma; but then I don't think so??
I know I can just call &Wilma from the &Fred sub but can they be called togeather?
if ($in{'Community'} eq "Ev90") { &EV90; }

and is calling the parse sub like that better then just &Parse; but still whats the () for??

Sorry I'm learning too so I got a lot of questions! :)


Dec 26, 2000, 6:15 AM

Post #4 of 5 (544 views)
Re: call subroutine from url [In reply to] Can't Post

just put a variable in the url, and put something like this in the cgi file
if ($FORM{'action} eq "something") { &something; }


Dec 26, 2000, 7:19 AM

Post #5 of 5 (543 views)
Re: call subroutine from url [In reply to] Can't Post

User-defined functions (also called "subroutines") used to require '&' characters in front of them when being called -- this is from Perl 4 and before. Now, the '&' is optional (except in two special cases). Here is a basic rundown of the rules for calling functions.

1. if the function has been declared before it is being used, you can leave out the parentheses:

sub foo { ... } 
$x = foo; # this is like $x = foo();

Otherwise, Perl won't know that foo is a function call, and will instead thing it's a bareword, so it will quote it for you, and $x will end up holding the string "foo". Unless use strict 'subs' is on, in which case you will be barked at during compilation.

2. do not put parentheses after the function name when declaring a function:

sub foo () { ... }  # not what you think

That starts a function prototype, and that specific prototype means that your function NEVER takes any arguments, which is probably not what you were hoping for.

3. avoid Perl 4-style function calls unless you're using the two special cases of &func:

sub foo { 
# ...
&bar; # identical to bar(@_)
# ...

$AUTOLOAD =~ s/.*:://;
goto &$AUTOLOAD;

The first case is showing how a naked &func call is the same as calling the function with the current function's @_ array (the arguments to the function). The second case is called the "magical goto", and it does for function calls what exec() does for processes -- that is, it replaces the current function call with the one specified, so that in Perl's call stack, you never called the original function, but went straight to this goto-ed one.

Please read the documentation! I point you specifically to perldoc perlsub.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives