I knew I missed an option or so ...

Option Five

sub remove_cruft { my $line = shift; # do stuff to one $line here. $line; } # use as: while (my $l = <$fh>) { $l = remove_cruft($l); $l = remove_other_cruft($l); $l = remove_yet_more_cruft($l); # or ... $l = $_->($l) foreach (\&remove_cruft, \&remove_other_cruft, \&remov +e_yet_more_cruft); # or ... $l = remove_yet_more_cruft(remove_other_cruft(remove_cruft($l))); # on second thought, don't do that last one :-) }

Option Six

sub remove_cruft { # do stuff to single line $_[0]; } # use as: while (my $l = <$fh>) { remove_cruft($l); remove_other_cruft($l); remove_yet_more_cruft($l); # or ... $_->($l) foreach (\&remove_cruft, \&remove_other_cruft, \&remove_yet +_more_cruft); }

The options are endless. What I highly discourage you from doing is writing shell script in perl. I've seen that so many times that it makes me cringe each time. Whether that is to write my $data = `grep blah $filename` rather than open my $fh, $filename; my $data = join '', grep { /blah/ } <$fh>; (and this is just the least perlish of the not-shell-script options), or it's system("mkdir $dir"); rather than mkdir $dir ... there are some really nifty perl idioms that take care of these things for you. They say you can write ForTran in any language. Same is true of shell scripts :-)


In reply to Re^3: 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.