in reply to Filters within a filter...
As a former ksh-(ab)user, who has many utilities in perl ... let's just say that, in and of itself, there is little reason to move working programs over ... the only reasons to do this, IMO, are either to add new features which are getting prohibitively expensive (in time) to add in shell, or for the sake of learning perl. I'm going to assume one of these is the case. I still have many shell scripts floating around aside my perl scripts - and perl scripts that generate shell code which I can eval - and I still write new shell code from time to time. Right tool for the right job.
Personally, I would do one of the following - depending on the rest of the code. Remember: TMTOWTDI. Not all of them are great, but often more than one is sufficient or even desirable.
sub remove_cruft { my @lines = @_; # do stuff to @lines; return @lines; } # use as: my @no_cruft = remove_cruft(@lines); # or: my @no_cruft = remove_cruft(<FH>);
# Using prototypes (which are considered "evil" by some) sub remove_cruft(@) { my $lines = shift; # do stuff to @$lines } # use as: remove_cruft(@lines) # this still doesn't work: # remove_cruft(<FH>)
sub remove_cruft { my @lines; if (@_ > 1) { # lines were passed in. @lines = @_; } # we either got a filename or a filehandle. elsif (ref $_[0]) # assume object is a filehandle. { @lines = <$_[0]>; } else # must be a filename. { my $fh = IO::File->new(shift, 'r'); @lines = <$fh>; } # do stuff to @lines; @lines; }
I realise that's not the most efficient way to do that last one, but I'm too lazy to do all of the work in this little textarea box... :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Filters within a filter...
by mw (Sexton) on Feb 04, 2005 at 16:54 UTC | |
by Tanktalus (Canon) on Feb 04, 2005 at 17:07 UTC |