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;


In reply to Re: Calling a sub from a button by gryphon
in thread Calling a sub from a button by cal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.