LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2000, 12:27 AM   #1
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Rep: Reputation: 15
Question


I was writing some Perl code to interact with an SQL database and I found myself at a bit of a loss when I wanted to generate a response when a user clicks on a link or button. What is the best way to call a sub from within the same file that the code for the link or button is in?
In other words, say i have a sub foo() {;} which does some interaction with an SQL database when executed. In this program, in the same file as the sub foo(), I have an <INPUT TYPE=button VALUE="bar"> that I want to have execute the sub foo() when clicked. How do I do this? Normally I would <FORM ACTION=/cgi-bin/foo.pl> but this requires the foo() code to be in its own file. Thanks!
 
Old 08-11-2000, 12:41 PM   #2
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 10,316

Rep: Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612
You could use something like the following.

Code:
require "cgi-lib.pl";
&ReadParse; # Reads in GET or POST data, converts it to unescaped text, and puts key/value pairs in %in.

if ($in{action} eq "foo") {
     foo();
}
else { 
     # print the main page including an <input type=button name=foo value=whatever_you_want_foo_to_be>
}

sub foo {
     whatever you want sub foo to do
}
 
Old 08-11-2000, 01:18 PM   #3
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
Thumbs up nifty =)

Cool thanks! I never would have guessed the &ReadParse; to generate the POST data into $in.
 
Old 08-12-2000, 03:20 PM   #4
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
slight snag

Umm.. hypthetical question... Lets say I didn't have cgi-lib.pl, where might I get it?
 
Old 08-12-2000, 05:32 PM   #5
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 10,316

Rep: Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612
If you are just starting out you may want to use CGI.pm (which is kind of teh cgi-lib.pl replacement). It can be found at CPAN. Instead of require cgi-lib.pl you would
Code:
 use CGI qw(:cgi-lib);
If you want to use cgi-lib it can be found at http://cgi-lib.berkeley.edu/.
 
Old 08-12-2000, 09:47 PM   #6
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
Somethings I don't understand:
1) Doesn't &ReadParse just call a sub readparse?
2) What's the difference between <INPUT TYPE=button> and <INPUT TYPE=submit>?
3) Lastly, what do I need in the <FORM> declaration? (i.e. ACTION= METHOD=)

Also, for this to work shouldn't ACTION= just call the exact same file thus this next time the if statement is parsed it will in fact execute it? Or is the if statement somehow always being checked to see if ($in eq "foo")?

Thanks again. =-)
 
Old 08-13-2000, 09:12 AM   #7
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 10,316

Rep: Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612
1) Yes.
2) Button places a button on an HTML form and uses scripting code to make the button perform an action you define (this is aside from the information that it sends in the Name/Value pair upon form submittal.) Note that this INPUT type does not actually initiate a form submission. Also note, after looking at my example it is not very good I started with the example you gave and it did not turn out clear.
3) The action would be the script. Method would be GET or POST. Here is a better example.
Code:
use CGI qw(:cgi-lib); 
&ReadParse; # Reads in GET or POST data, converts it to unescaped text, and puts key/value pairs in %in.

if ($in{foo} eq "linux") {
     foo();
}
else { 
     # print the main page including :
 <FORM ACTION=index.cgi METHOD=POST> 
 <input type=hidden name=foo value=linux>
 <input type=submit Name=Click>
}

sub foo {
     whatever you want sub foo to do
}
So when you browse to index.cgi you will see what is in the else block since foo does not equal linux. When you submit the form you will get what is in the if. Hope this is a little clearer.
 
Old 08-13-2000, 04:12 PM   #8
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
Thumbs up works great

Thanks! It works like a charm! I was <i>almost</i> there, but I didnt think to use the hidden input type. Thanks again.
 
Old 08-13-2000, 07:10 PM   #9
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 10,316

Rep: Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612Reputation: 2612
Exclamation

Actually it doesn't matter what TYPE the input is. You could have a text box, a check box, etc as long as it was NAMED appropriately (in this case foo).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling KDE/C++ functions from python/perl and vice versa yogeshm02 Programming 1 04-30-2005 11:54 AM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04:59 PM
Issues calling sort from perl using pipe as delimiter amytys Programming 1 10-20-2004 09:35 PM
calling a c++ binary inside a perl script Blue_muppet Programming 3 08-28-2004 11:31 PM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM


All times are GMT -5. The time now is 04:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration