no prob -- here's the promptFilename() disected.. note this was a replacement for both func1() and func2() :
sub promptFilename {
my $prompt = shift;
First, declare the function name with 'sub' .. then store the first argument in $prompt. Since in your original you always displayed a string to user before reading in the filename, I thought it made sense to pull it into the function.
print $prompt . "\n";
Use the given string to display a prompt to the user.
my $file;
while(! $file){
$file = <>;
NOTE: reading through this, i realized an error and updated my code here and above.
Declare a $file to hold the result. Start a loop that goes until we have something in $file. Each iteration will start by reading a line from STDIN.
chomp($file);
Strip the newline.
$file =~ s/^"(.+)"$/$1/; # unquote
This a regex substituion (see perlre) -- tries to match a quote at the beginning, anything in the middle, and a quote at the end. If it matches, it replaces everything with the middle part (i.e. it strips the quotes). If it doesn't match, it does nothing (cause the string wasn't quoted).
}
return $file;
}
Close the loop, return the $file value, and close the function.
| [reply] [d/l] [select] |
Alright, final question and i'll be done with this bit for now. In the search and replace bit, what is the $1?
| [reply] |
$file =~ s/^"(.+)"$/$1/; # unquote
The parens in the regex capture text into numbered variables. The first set of parens grab into $1, the second into $2 and so forth. For more info check out perlre.
It may be a bit off topic, but I'd really like to recommend that you pick up a copy of Learning Perl from a bookstore or your local library. It is the best introductory perl material I have seen. You'll be glad you did.
| [reply] [d/l] |
Thank you, it works like a charm now.
| [reply] |