How can I read from a method that returns a file descriptor in Perl?

I have an object with a method that returns a file descriptor, and I want to read it. The following does not work because the rectangular bracket of the method call is interpreted as the bracket of the closing angle of the input reader:

my $input = <$object->get_handle()>;

This is analyzed as:

my $input = ( < $object- > ) get_handle() >;

which is obviously a syntax error. Is there a way I can make a method call inside an angle operator, or do I need to break it into two steps?

my $handle = $object->get_handle();
my $input = <$handle>;
+3
source share
4 answers

; < > , glob, <STDIN> , , glob, < $fh > , glob(), < *. c > . glob ('$ object-').

< > readline(), , , my $input = readline( $object->get_handle() );, , , .

. http://perldoc.perl.org/perlop.html#I%2fO-Operators.

+3

< ... > readline( ... ), . . .

+7
my $input = readline($object->get_handle());

use IO::Handle;

my $input = $object->get_handle()->getline();
+1

<...> , , , <bareword> <$scalar>, glob(...), . <HANDLE> readline HANDLE, :

my $input = readline $object->get_handle;

, , . <...> , :

my $handle = $object->get_handle;
while (my $input = <$handle>) {
    ...
}
0

Source: https://habr.com/ru/post/1741876/


All Articles