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
|