Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How can I run only part of a script determined by a posted variable?

by gt8073a (Hermit)
on Jan 15, 2002 at 13:47 UTC ( [id://138863]=note: print w/replies, xml ) Need Help??


in reply to How can I run only part of a script determined by a posted variable?

is there a way to do the same sort of thing without if then statements?

You can use references to subroutines in a hash, then check if the key exists.
The following example does not do any real error checking, security, or check any return values, but it should help point you in the correct direction.

#!/usr/bin/perl -w use strict; print "Starting.\n\n"; my %actions = ( 'open' => \&fileOpen, 'create' => \&fileCreate, 'delete' => \&Nono::fileDelete, 'test' => \&fileTest, ); while ( <DATA> ) { chomp; next if /^\s*$/; my ( $function, $file ) = split( /\s+/, $_, 2); print "Testing '$function' -> '$file' . . ."; exists( $actions{ $function } ) ? &{$actions{ $function }}( $file ) : print "\t'$function' does not exist\n"; } print "Done.\n\n"; exit(0); sub fileOpen { my $open = shift; ## open( BLAH, $open ) or die; ## actions ## close( BLAH ); print "\tGot file '$open' to open.\n"; return; } sub fileCreate { my $create = shift; ## open( BLAH, ">$create" ) or die; ## actions ## close( BLAH ); print "\tGot file '$create' to create.\n"; return; } package Nono; sub fileDelete { my $delete = shift; ## actions print "\tGot file '$delete' to delete.\n"; return; } package main; sub fileTest { my $test = shift; ## actions print "\tGot file '$test' to test.\n"; return; } __DATA__ open line1 test line2 create line3 test line 4 blah blah delete line5 open line 6 nothing line 7 fails xxx line8 fails too fail This will fail
  • Comment on Re: How can I run only part of a script determined by a posted variable?
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found