in reply to using a subroutine
One thing you do which strict may call attention to is set a global variable in one subroutine and use it in another. It's better to just pass those values as arguments. You'll wind up with cleaner code that way.
You can also save yourself some trouble by realizing that unlink can take a list as argument, and that glob will produce a list of the kind you want. Here is a sub that illustrates a more economical approach.
The map applies the code in its first argument to each element of the remaining arguments - here, globby versions of your regexen. The result is a list of matching filenames. unlink then acts on each, returning the number of files deleted in that pass.my ($user, $lib) = qw(/home/john /lib); sub directs { my $count; for my $dir (@_) { $count += unlink map {glob "$dir/$_"} qw(*~ core.*); } $count; } print directs( $user, $lib), ' files deleted.', $/;
Update: $/ is the input record seperator, by default your system's newline. You could say that as well with "\n". Using $/ is handy if you might want to change the line end, because then you can say, for instance,
and the strings foo() prints will be terminated with html break tags. $\ could also be used (with even better justification) but it does not have the newline default value.{ local $/ = '<br />'; foo(@args); }
After Compline,
Zaxo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: using a subroutine
by xjlittle (Beadle) on Aug 10, 2004 at 00:50 UTC | |
Re^2: using a subroutine
by xjlittle (Beadle) on Aug 10, 2004 at 00:28 UTC |
In Section
Seekers of Perl Wisdom