Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Avoiding user-input in sub calls.

by bobn (Chaplain)
on Nov 01, 2003 at 08:08 UTC ( [id://303804]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Avoiding user-input in sub calls.

Slightly better to use a hash of coderefs:

%func = ( one => sub { print "Sub one says: $_[0]" }, two => sub { print "Sub two says: $_[0]" }, three => sub { print "Sub three says: $_[0]" }, four => sub { print "Sub four says: $_[0]" }, ); my $sub = CGI::param('type'); my $data = CGI::param('info'); if ( exists $func{$sub} ) { $func{$sub}->($data) } else { $func{one}->($data) }

--Bob Niederman, http://bob-n.com

All code given here is UNTESTED unless otherwise stated.

Replies are listed 'Best First'.
Re: Re: Avoiding user-input in sub calls.
by liz (Monsignor) on Nov 02, 2003 at 05:30 UTC
    Since when dealing with code refs, there is no difference between not existing and undefined, I always prefer to write:
    if ( exists $func{$sub} ) { $func{$sub}->($data) } else { $func{one}->($data) }
    as:
    ($func{$sub} || $func{one})->( $data );

    which at least ensures that the same parameters are passed to (and possibly returned from ) the default routine. More compact and fewer things to worry about from a maintenance point of view!

    Liz

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://303804]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.