A friend of mine uses a shareware utility for Windows that mirrors web sites so that you can browse them offline. This utility (unfortunately) adds some sort of banner to each downloaded page; the banner is delimited by two html comments. Here is an example:
<!-- inserted banner --> <TABLE> blah blah </TABLE> <!-- end of inserted banner -->
My friend wanted to remove this banner ... so I wrote a nice perl script for him. The script traverses the whole directory tree of the downloaded site and removes the html lines that create the banner from each page.
The first version of my script used a recursive procedure to go through all the files, read each line skipping those unwanted, and than wrote the file back.
Than I realized that I could use File::Find, so I started rewriting the script. In the meanwhile I was sort of enlightened: the .. operator appeared in my mind, and I realized I could use that too. So here is the second version of my script
use strict; use File::Find; die "usage $0 <dir>" unless (@ARGV == 1 && -d $ARGV[0]); find ( sub { return unless -f $_; open (FILE, "< $_") or warn "cannot read $_\n" and return; my @lines = grep {!(/inserted banner/i .. /end of inserted ban +ner/i)} <FILE>; close (FILE); open (FILE, "> $_") or warn "cannot write $_\n" and return; print FILE @lines; close (FILE); }, $ARGV[0] );
It works pretty well, and I'm quite happy with that ... and my friend is happy too :). Anyway in the process of continuous learning I started thinking of rewriting again the script to take advantage of -p command switch, and here is what I ended up with:
perl -MFile::Find -pi.bak -e "BEGIN {find sub {-f && push @ARGV, $File +::Find::name}, \".\"} if (/inserted banner/i) {while (<>) {s/.*// and last if /end of insert +ed banner/i;}}"
Moral: there is more than one way to do it :)
marcos

In reply to Marcos' Meditation by marcos

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.