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.

Option one:

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>);

Option two

# Using a reference: sub remove_cruft { my $lines = shift; # do stuff to @$lines; } # use as: remove_cruft(\@lines); # Note that the following won't work: # remove_cruft(<FH>); # you need to use a temporary variable </code>

Option Three

# 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>)

Option Four

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... :-)


In reply to Re: Filters within a filter... by Tanktalus
in thread Filters within a filter... by mw

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.