ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
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
}
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
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
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")?
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
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.
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.