http://www.perlmonks.org?node_id=578341

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

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";


    grep
    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;
    BR