Search code examples
perlhreffilehandle

How to read a line from a filehandle in a hashref


I have a handle to a socket in an hashref: $self->{socket}.
I want to read from it using $line = <$self->{socket}>, but I get a syntax error.

Now, I know that

print {$self->{socket}} "Hello";

Will take care of printing, but

$line = < {$self->{socket}} >;

Doesn't work.

How do I do this without cluttering up my code like this:

$fh = $self->{socket};
$line = < $fh >;

Thanks.


Solution

    • <fh> is short for readline(fh)
    • <$fh> is short for readline($fh)
    • <...> is short for glob(qq<...>)

    The shortcut is out of the question, so use the long form.

    readline($self->{socket})