Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

print complains about a file handle variable

by SirBones (Friar)
on Oct 15, 2006 at 03:00 UTC ( #578341=perlquestion: print w/ replies, xml ) Need Help??
SirBones has asked for the wisdom of the Perl Monks concerning the following question:

I've started playing with Perl objects. I've got a nice little class that establishes a TCP/IP socket and supports some communications functions along that socket. The hash key for the socket name is 'S', which was created and assigned in my constructor, and which is referred to in my methods as $me->{S}, i.e.:

sub readSocket { my $me = shift; . . my $readNum = read($me->{S}, $inData, 4); . }

This works fine.

But if I try to use it here:

sub socketSend { my $me = shift; . . . my $outData; . . # <Do stuff that builds $outData> . . print $me->{S} $outData; }

I get this error:

Scalar found where operator expected at PTComm.pm line 205, near " +} $outData" (Missing operator before $outData?) syntax error at PTComm.pm line 205, near "} $outData"

But if I do this:

my $h = $me->{S}; print $h $outData;

All is well.

I'm sure someone will tell me in short order: What gives? (Please.)

(Thanks, as usual.)

-Ken

"This bounty hunter is my kind of scum: Fearless and inventive." --J.T. Hutt

Comment on print complains about a file handle variable
Select or Download Code
Re: print complains about a file handle variable
by caelifer (Scribe) on Oct 15, 2006 at 03:09 UTC
    Try this one:
    print {$me->{S}} $outData;
    BR
Re: print complains about a file handle variable
by grep (Monsignor) on Oct 15, 2006 at 03:19 UTC
    From perldoc -f print
    Note that if you're storing FILEHANDLEs in an array, or if you're using any other expression more complex than a scalar variable to retrieve it, you will have to use a block returning the filehandle value instead:

    Something like:

    open(FH,'>test.txt') or die "$!\n"; my $obj = { S => *FH{IO} }; print {$obj->{S}} "foo\n";


    grep
    One dead unjugged rabbit fish later

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578341]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-11-29 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls