in reply to Re: Matching Regular expression at EOF
in thread Matching Regular expression at EOF

No... its not on a single variable... i am opening the file and reading it line by line...
  • Comment on Re^2: Matching Regular expression at EOF

Replies are listed 'Best First'.
Re^3: Matching Regular expression at EOF
by ikegami (Patriarch) on Feb 20, 2010 at 19:44 UTC
    Then I assume the match doesn't span more than one line?
    my @match; while (<>)) { my @caps = /pat/ or next; @match = @caps; } if (@match) { print("Captured @match\n"); } else { die("No match\n"); }
    or
    my $match; while (<>)) { $match = $_ if /pat/; } if (defined($match)) { print("Matched $match"); } else { die("No match\n"); }

    All this guessing is leading to suboptimal solutions and wated work. If this is still not good, please provide more info about your problem.

Re^3: Matching Regular expression at EOF
by shawnhcorey (Friar) on Feb 20, 2010 at 19:42 UTC
    No... its not on a single variable... i am opening the file and reading it line by line...

    Try:

    my $last_line = undef;
    while( <$fh> ){
      $last_line = $_;
      # process file contents, if needed
    }
    if( defined $last_line ){
      $last_line =~ m/$pat\z/;
    }else{
      warn "file is empty\n";
    }
    
      That will erroneously claim the file is empty if the last occurrence of the pattern in on the second-last line (for example).