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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.