Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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 line 205, near " +} $outData" (Missing operator before $outData?) syntax error at 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.)


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

Replies are listed 'Best First'.
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";

    One dead unjugged rabbit fish later
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;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2016-10-23 12:01 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (301 votes). Check out past polls.