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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|