Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Object property not legal as direct object?

by hippo (Bishop)
on Nov 09, 2016 at 11:25 UTC ( [id://1175590]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1175590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-19 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found