Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Grep not working?

Expand Messages
  • Paul Archer
    While there s nothing inherently wrong with Charles answer, I would take another look at Randal s email, and the module he suggested (HTML::Parser). Two
    Message 1 of 9 , Feb 19, 2005
    • 0 Attachment
      While there's nothing inherently wrong with Charles' answer, I would take
      another look at Randal's email, and the module he suggested (HTML::Parser).
      Two strong suggestions when it comes to Perl:
      1) Don't reinvent the wheel. Somewhere out there is a module that does what
      you want. Leverage the work of those who have come before you.
      2) Listen to Randal. Several books and about a zillion articles have given
      him a lot of cred. He keeps that cred by continuing to give good answers and
      the right advice. (But don't get him started on mySQL vs Postgres!)

      Paul



      6:37pm, zeshanism wrote:

      >
      >
      > Thanks Charles, you always remained helpful for me. I learned $/ for
      > the first time, $INPUT_RECORD_SEPARATOR.
      >
      > "The current input record separator. This is the new line by
      > default, but it can be set to any string to enable you to read in
      > delimited text files that use one or more special characters to
      > separate the records. You can also undefine the variable, which will
      > allow you to read in an entire file, although this is best done
      > using local within a block"
      >
      > {
      > local $/;
      > $file = <FH>;
      > }
      >
      > Once again thanks.
      >
      > Zeshan.
      >
      >
      >
      >
      >
      > --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
      > <cclarkson@h...> wrote:
      >> zeshanism <zeshanism@y...> wrote:
      >>
      >> : Any ideas as why it's not working? Using perl 5.8.0 on
      >> : my XP machine.
      >> :
      >> : open (TEMPLATE, "file.txt") ;
      >>
      >> How do you know it opened? Always verify I/O
      >> operations.
      >>
      >>
      >> : my @lines = <TEMPLATE>;
      >>
      >> Each line in the file is now an element of @lines.
      >>
      >>
      >> : close TEMPLATE;
      >> : my @greped = grep {/<body>.*?<\/body>/si} @lines;
      >>
      >> This regex never matches. It tests against each
      >> line in @lines. No single line has the starting and
      >> ending <body></body> tags.
      >>
      >> Perhaps you could slurp the file into a scalar.
      >> Read perlfaq5 for details.
      >>
      >>
      >> #!/usr/bin/perl
      >>
      >> use strict;
      >> use warnings;
      >>
      >> my $template;
      >> {
      >> my $file = 'file.txt';
      >> open FH, $file or die qq(Cannot open "$file": $!);
      >> local $/ = undef;
      >> $template = <DATA>;
      >> close FH;
      >> }
      >>
      >> print $1 if $template =~ m!(<body>.*?</body>)!si;
      >>
      >> __END__
      >
      >
      >
      >
      >
      > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
      >

      --------------------------------------------
      Never ascribe to malice what can perfectly
      well be explained by stupidity. -Anonymous
      --------------------------------------------
    Your message has been successfully submitted and would be delivered to recipients shortly.