http://qs321.pair.com?node_id=1136225


in reply to RFC: newscript.pl , my very first script!

You use a global variable @modules to pass what modules you want to add to your Perl script. It's better practice to pass in an array to the _makeperl() function with what you want to add. Change:
if ($addmodule =~ /yes/i) { print "\nThis script does NOT add a ';' for you!\nSay 'done' w +hen you done..\nModules: "; while (<STDIN>) { last if ($_ =~ /done(;)?/i); push @modules, $_; } _makeperl(); } elsif ($addmodule =~ /no/i) { _makeperl();} else { print "I assume no.\n"; _makeperl(); }
to
if ($addmodule =~ /yes/i) { my @modules; print "\nThis script does NOT add a ';' for you!\nSay 'done' w +hen you done..\nModules: "; while (<STDIN>) { last if ($_ =~ /done(;)?/i); push @modules, $_; } _makeperl(@modules); } elsif ($addmodule =~ /no/i) { _makeperl();} else { print "I assume no.\n"; _makeperl(); }
and remove the  my @modules; from the top of the script. Then in your _makeperl() change:
sub _makeperl { open (NEWSCRIPT, '>', $name); print NEWSCRIPT "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n"; if (defined($modules[0])){ # if module is defined +, include them to the template for my $mods (@modules) { print NEWSCRIPT "use $mods"; } }
to
sub _makeperl { my @mods = @_ open (NEWSCRIPT, '>', $name); print NEWSCRIPT "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n"; if (@mods){ # if module is defined, include them +to the template for my $mod (@mods) { print NEWSCRIPT "use $mod"; } }
Note, I also changed the if (@mods){ which is the usual way of checking if an array has any elements in it.

Similarly you could pass the $name of the script into all the functions as well.