in reply to Re: Filters within a filter...
in thread Filters within a filter...

* mw nods

Well, there are two reasons why I'm porting these scripts over: First, as an exercise to learn perl, and second because I'm hoping for increased speed. I suppose most of the HTML files I'm likely to see here, will fit in memory without too many problems, so I think I'll standardise on arrays of lines slurped therefrom.

Thanks!

Replies are listed 'Best First'.
Re^3: Filters within a filter...
by Tanktalus (Canon) on Feb 04, 2005 at 17:07 UTC

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