You could use a dispatch table:
my %filetype = ( 'c' => \&create_c, 'bash' => \&create_bash, 'perl' => \&create_perl_script, 'pl' => \&create_perl_script, 'pm' => \&create_perl_module, ); sub create_c { my $filename = shift; # stuff for creating a c file } sub create_bash { my $filename = shift; # stuff for creating a bash script } ... print "What kind of file do you want to create?\n"; printf "Supported file types: %s\n", join ', ', sort keys %filetype; # ask user for desired filetype -> $type # ask user for desired filename -> $name if (exists $filetype{$type}) { # this calls the function that is stored in the hash # e.g. create_bash($filename) $filetype{$type}->($name); } else { die "Sorry, file type '$type' is not supported.\n"; }
If you want to support some other filetype (e.g. SQL) at some point then you only need to write a new function 'create_sql' and update the %filetype hash.
In reply to Re: RFC: newscript.pl , my very first script!
by Monk::Thomas
in thread RFC: newscript.pl , my very first script!
by Darfoune
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |