Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Calling a sub from a button

by cal (Beadle)
on Oct 25, 2002 at 21:26 UTC ( [id://208145]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings to all Monks,

Right now I am calling sub store_event(); just from a line &store_event; in the script. I want to use a button to call it instead. Not to sound stupid I am still new to perl and I dont really have any code. I just thought I could use a <FORM> tag scheme from within the script. Is this premise possible? If so, what would the code look like? Thanks
<form action='&store_event' method='post'> <input type='submit' value='submit'> <input type='button' value='Back'onClick='history.go(-1)'> </form>
Thanks Cal

Replies are listed 'Best First'.
Re: Calling a sub from a button
by gjb (Vicar) on Oct 25, 2002 at 21:37 UTC

    While it is possible to execute embedded Perl scripts in the HTTP client on the Windows platform using Microsoft's Scripting Host facility in conjunction with ActiveState's Perl, it something I'd advise strongly against. It implies that someone smart enough to embed some nasty Perl code in his HTML might get it executed on your PC.

    I've created a number of programs with an HTML GUI, the logic residing on the server as a Perl CGI script (which happened to run on my own computer). It's a nice way to very quickly implement simple user interfaces. But yes, you should run a (local) HTTP server to do this.

    Another option is to use the "prefered" Perl GUI framework Tk (others exist) to make a simple GUI app, it isn't hard either.

    Just my 2 cents, -gjb-

Re: Calling a sub from a button
by gryphon (Abbot) on Oct 25, 2002 at 21:48 UTC

    Greetings cal,

    Fortunately (from a security perspective) you can't just run Perl subs via HTML. You'll need to pass your action request via HTTP to your CGI that will interpret it and run the function that you desire.

    The HTML might look something like:

    <FORM action="yourscript.cgi" method=post> <INPUT type=hidden name=action value="storeevent"> ...

    Then you're Perl might something look like:

    #!/usr/bin/perl -wT use strict; use CGI; $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 1024; my $cgi = new CGI; my $action = $1 if $cgi->param('action') =~ /^(\w+)+/; &store_event if ($action eq 'storeevent');

    Whatever you do, don't be tempted to do something like:

    my $action = $cgi->param('action'); &$action;

    UPDATE: The above assumes two things: first that you'll print out the HTTP header elsewhere in your script before sending HTML, and second that the "action" CGI parameter is always defined. If it's not, then you'll get a warning. In such cases, you could use:

    my $action = (defined $cgi->param('action')) ? ($cgi->param('action') =~ /^(\w+)+/) ? $1 : '' : '';

    This just makes sure that the parameter is defined. If it isn't or if it is and contains badness, then $action results in an empty string.

    gryphon
    code('Perl') || die;

      I'm sorry, I thought I mentioned that the call was from a script. I should have said a CGI script.
        In that case you would need the form action to be the script, and submit another field which the script uses to find the right subroutine. You could use a hidden field. But a nice way to do it IMHO is with the submit button. Since the value field in a Submit button only gets submitted with the form when that particular button is pressed, you have the option of different buttons within the same form. For example, in the html:
        <FORM ACTION="yourscript.pl" METHOD="POST"> <!-- insert other form inputs here --> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Run Foo"> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Run Bar"> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Store Event"> </FORM>
        Then in the script (in this example saved as yourscript.pl) you could have something like
        use CGI qw/:standard/; if (param('Action') and param('Action') eq 'Run Foo') { foo(); } elsif (param('Action') and param('Action') eq 'Run Bar') { bar(); } elsif (param('Action') and param('Action') eq 'Store Event') { store_event(); } else { print header, start_html('Ack!'), h1 'you have to call this from the form!', end_html; }


        § George Sherston

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-19 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found