in reply to Object property not legal as direct object?

From perldoc 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

So, this works fine:

#!/usr/bin/env perl use strict; use warnings; my $hr = { logfh => \*STDOUT }; print { $hr->{'logfh'} } "Hello Hashref\n"; my $obj = bless ($hr, 'UNIVERSAL'); print { $obj->{'logfh'} } "Hello Object\n";

Replies are listed 'Best First'.
Re^2: Object property not legal as direct object?
by Daniel Beardsmore (Initiate) on Nov 09, 2016 at 11:46 UTC

    Braces? How odd. Over eight years on and Perl still has so many oddities I've yet to uncover!

    Silly me, I was overthinking it — I forgot that Perl documentation is actually decent :) (I did actually verify that print has the same problem, funnily enough.)

    Thank you.

Re^2: Object property not legal as direct object?
by BillKSmith (Monsignor) on Nov 09, 2016 at 20:51 UTC
    The book "Perl Best Practices" recommends putting braces around the filehandle in every print statement. Even when they are not needed, they make it clear that you did not forget a comma.
    Bill