http://www.perlmonks.org?node_id=476453


in reply to Regular expressions

Two problems:

  • Missing space in <!--Start_of_revision-->
  • if rather than while

    A working version of your code in a form that is better for stand alone testing is:

    use strict; use warnings; my $text = join "", <DATA>; while($text =~ /<!-- Start_of_revision-->(.*?)<!-- End_of_revision-->/ +sg) { print $1; } __DATA__ <!-- Start_of_revision--> revision1 <!-- End_of_revision--> <!-- Start_of_revision--> revision2 <!-- End_of_revision--> <!-- Start_of_revision--> revision3 <!-- End_of_revision-->
    revision1 revision2 revision3

    Perl is Huffman encoded by design.
  • Replies are listed 'Best First'.
    Re^2: Regular expressions
    by ikegami (Patriarch) on Jul 20, 2005 at 14:56 UTC

      That's an aweful way of reading a file. It means every line must be pushed onto the stack. The OP's method was better, and the following is even better because it avoids a call to stat and works will all kinds of IO handles:

      my $text; { local $/; $text = <DATA>; }

      Visually, I prefer
      my $text = do { local $/; <DATA> };
      but I think I determined the above is equivalent to
      my $text; { local $/; my $temp = <DATA>; $text = $temp; }

        The reason I am a monk is to learn from the masters. I keep forgetting about $/! I hope I have learned :). Thank you.


        Perl is Huffman encoded by design.