Warning: This code is hazardous to your health. This is an example how not to do it.

Right, now that's said, how's this for fun and giggles:

#!/usr/local/bin/perl -w use strict; sub a { print "Hi from a\n"; } sub b { print "Hi from b\n"; } doit('a'); doit('b'); $main::c=1; $main::c++; doit('c'); sub doit { my $func=shift; unless (exists($::{$func}) && defined(&{$::{$func}})) { print "Invalid function called: $func\n"; return 1; }; no strict 'refs'; &{$func}; } __END__ Hi from a Hi from b Invalid function called: c
This is ugly code. It uses symbolic references, it hacks the symbol table, and the sheer amount of curlies and parentheses induces headaches in anybody having to maintain it. But it works.

For educational purposes only. (And I admit, I did it to play around with symbol tables a bit, to get a feel for the things).

Update: Not only is this ugly, if called as a CGI (and the sub to call is provided as a parameter), it is also terribly insecure in this form: it's letting the CGI user call any function that is defined in your script. Bad programmer.

CU
Robartes-


In reply to Re: Event Handling in CGI by robartes
in thread Event Handling in CGI by Tanalis

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.