arunkumar.rk141 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to do the pattern match and extract the contents based on the back references on a file handler. But this doesn't seem to be working.Can someone explain me why the printing of file handler when used like print <FH> works & not the extraction part as i explained previously? Also suggest how this can be done in a single line of code instead of looping it in the while condition

open my $a,"<file"; print <$a>;# prints the file contents as expected print $1 if <$a> =~ m/(error.*)/; # Nothing printed though the files h +as the line containing error key word. my @b=<$a>;grep {$_ =~ /error/} @b;# This does the trick and wonderin +g why the above line doesn't work

Replies are listed 'Best First'.
Re: Grepping the filehandler
by haukex (Archbishop) on Jul 23, 2020 at 15:45 UTC

    print and the array assignment provide list context to the <> operator (readline), which causes it to return all (remaining) lines from the handle, while =~ provides scalar context, which causes the operator to only return the next line - unless you set $/ to undef for slurp mode, but in this case I recommend local $/; in a new block to limit the scope of the change.

    Update: grep also provides list context, so you can say e.g. my @matching_lines = grep {/.../} <$fh>;. Also added note on local. Added another link.

Re: Grepping the filehandler
by choroba (Cardinal) on Jul 23, 2020 at 15:47 UTC
    You probably don't run all the lines together in one script, but you include the first line and then just one of the second, third, or fourth one. It would have helped us to help you if you described in more detail what your code section meant.

    The main problem is context. Both print on line 2 and list assignment on line 4 force list context, which means the whole contents of the file is returned from the readline. Line 3 uses readline in the binding operator which forces scalar context, which means it only reads one line from the file. If the very first line doesn't contain the error, it won't be printed.

    Inserted: To shorten the code, you can use

    print grep /error/, <$f>;
    as grep also forces list context to its non-first argument(s).

    Some other comments:

    1. Don't use my $a. $a is a special variable used in sort, lexicalising it can lead to sort not working.
    2. Check the return value of open and use the 3 argument variant:
      open my $f, '<', 'file' or die $!;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks for your explanation.

      I tried to reference the file handler with a readline which gave the last line when it is dereferenced. Bit confused here as in the previously mentioned code it considers the first line of a file in a scalar context but when it comes to referencing last line is considered.

      open $f,'<','file' or die $!; my $r=\<$f>; print ref($f);#SCALAR print "$$r";# prints the last line of the code

        The \ operator in this case "puts its operand in list context, and creates a list of references to the scalars in the list provided by the operand", so the readline is in list context here and reading all the lines from the file. So in other words it's the same as my $r = \("Line1","Line2","...","LineN");, and a list in scalar context returns its last element.

Re: Grepping the filehandler
by hippo (Archbishop) on Jul 23, 2020 at 15:52 UTC

    There will always be a loop whether expressed or implied. Here's an SSCCE:

    #!/usr/bin/env perl use strict; use warnings; print /(error.*)/ ? $1 : '' for <DATA>; __DATA__ foo error bar baz

    And the output is one line with error bar.


    🦛

Re: Grepping the filehandler
by jwkrahn (Abbot) on Jul 23, 2020 at 18:45 UTC
    print $1 if <$a> =~ m/(error.*)/; # Nothing printed though the files h +as the line containing error key word.
    print map /(error.*\n)/, <$a>;