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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Call Subs from external Form or url

 



abockover
Deleted

Jul 12, 2000, 9:41 PM

Post #1 of 7 (2016 views)
Call Subs from external Form or url Can't Post

I need to be able to execute a certian portion of a program by executing a sub from an html form or url.

For example, my script is called script.cgi, if I go to http://www.domain.com/cgi-bin/script.cgi, the message:

MAIN CONTENT

is displayed.

But if I go to http://www.domain.com/cgi-bin/script.cgi?action=CallSub,

the sub CallSub is executed and the message

SUB CONTENT

is displayed.

-- Code examples welcome!

Thanks,

Aaron


abockover
Deleted

Jul 12, 2000, 9:48 PM

Post #2 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

Testing a weird idea i had... ignore this reply.

-- Code samples still welcome... my question is still a question (I have not found the answer)

Aaron


monocle
User

Jul 12, 2000, 10:48 PM

Post #3 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

I think it should work.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@pairs = split(/&/,$ENV{'QUERY_STRING'});

foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$FORM{$name} = $value;
}

if ($FORM{'action'} == "CallSub"){
#do ehat you want here like &CallSub;
} else {
#do what you want here like print MAIN CONTENT
}</pre><HR></BLOCKQUOTE>
Good luck



------------------
Monocle
Hear great techno music by Monocle at http://www.mp3.com/monocle. CD now on sale!



Kanji
User

Jul 13, 2000, 12:19 PM

Post #4 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w


use strict;
use 5.004; # Must have perl 5.004 or above
use CGI qw( :standard );


print header, start_html;


# You could insert the name of a default
# sub here instead of "". ie, "default"
# would run &default; if the requested
# action wasn't a valid sub.
my $action = param('action') &#0124; &#0124; 'main_content';


# Return a pointer to ourselves. This
# is essentially what MODULE->new() does
# in other modules.
my $self = bless { };


# can() is a method we inherit from the
# UNIVERSAL namespace, and if the string
# we pass it -- matches the name of a
# subroutine in the class ( main:: ) of
# the object it's looking at ($self),
# returns a code_ref to that sub.
#
# ie, $sub_ref = \&#8834;
if ( my $sub_ref = $self->can( $action ) ) {
&$sub_ref;
} else {
print qq("$action" isn't a valid sub.);
exit;
}


print end_html;


sub main_content { print "Main content" }
sub sub_content { print "Sub content" }</pre><HR></BLOCKQUOTE>

Documentation
perldoc UNIVERSAL
perldoc perlref

Side Note
UBB could suck rocks through a blackhole.

[This message has been edited by Kanji (edited 07-13-2000).]


abockover
Deleted

Jul 13, 2000, 2:56 PM

Post #5 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

Replying to reply #3 [monocle]

It works half way, I mean, the output is reversed. Below is the exact code that is in test.pl on my server.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

if ($FORM{'action'} eq "CallSub"){
&CallSub;
} else {
print "Content-type: text/html\n\n";
print "This is the main program.<br>Click <a href=\"test.pl?action=CallSub\">here</a> to enter the sub <address>CallSub</address>.\n";
}

sub CallSub {
print "Content-type: text/html\n\n";
print "This is the sub <address>CallSub</address>. If you are looking at this, it works!";
}

@pairs = split(/&/,$ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$FORM{$name} = $value;
}
</pre><HR></BLOCKQUOTE>

Visit http://www.crevasoft.com/cgi-bin/test.pl to see what I mean.

Then, go to http://www.crevasoft.com/cgi-bin/test.pl?action=CallSub

The output is reversed! What is wrong?

Thanks

Aaron


Kanji
User

Jul 13, 2000, 3:14 PM

Post #6 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

Everything from and including @pairs needs to happen before the if: how else can $FORM{'action'} exist in time?



abockover
Deleted

Jul 13, 2000, 6:43 PM

Post #7 of 7 (2016 views)
Re: Call Subs from external Form or url [In reply to] Can't Post

Oh...

I cannot belive I missed that! Please excuse my brain fart!

Thanks,

Aaron

 
 


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

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