in reply to file handle var for print command

See print:

If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead, in which case the LIST may not be omitted:

So, if you're storing the filehandle in a complex structure, you need the block:

print { $self->{fh} } encode( ... )

Replies are listed 'Best First'.
Re^2: file handle var for print command
by Arunbear (Prior) on Sep 24, 2025 at 11:52 UTC
    This also works as expected, but I couldn't find a documented explanation for it:
    $self->{fh}->print("test with print\n");

      Filehandle GLOBs behave like IO::Handle instances (but aren't actually for purposes of inheritance according to the BUGS entry in the IO::Handle docs) so that's just a normal method call to the print method and doesn't get into weird indirect object parsing corners.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Not quite.

        They behave like IO::File objects (since 5.12).

        $ perl -e'STDOUT->foo' Can't locate object method "foo" via package "IO::File" at -e line 1.

        They do obey inheritance.

        $ perl -e'use IO::File; IO::File::say( *STDOUT, "ok" )' Undefined subroutine &IO::File::say called at -e line 1. $ perl -e'use IO::File; IO::Handle::say( *STDOUT, "ok" )' ok $ perl -e'STDOUT->say( "ok" )' ok

        (IO::Handle's docs is merely saying IO::File isn't a derivable class since My::IO::File->new will create an IO::File "object" instead of a My::IO::File object.)

        And the issue raised wasn't why it works; it was about where it's documented. I could not find it either.