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

Undefined value as a symbol reference

by peschkaj (Pilgrim)
on Mar 11, 2003 at 13:26 UTC ( [id://242012]=perlquestion: print w/replies, xml ) Need Help??

peschkaj has asked for the wisdom of the Perl Monks concerning the following question:

Forgive me brothers, it has been a long time since I last sought your wisdom.

I am working on a socket server to monitor remote machines, and I thought that it would be nice to write a pair of functions, writeClient and writeLog. writeLog appears to be working just fine. However, writeClient throws

Can't use an undefined value at ./myPerlServ.pl line 150.

Here are some of the relevant pieces of code:

my $client; while ( $client = $server->accept() ) { ... writeClient("I'm trying to do something, really!"); } ... sub writeClient { ### Read args, take first arg and use it, dump the rest my ( $message, @junk ) = @_; my $clientMessage = "$message\r\n"; print $client $clientMessage; }

I'm curious, do I need to pass $client into writeClient and then dereference it? (References in Perl confuse me a great deal sometimes.) Or is there something else that I am completely missing?



If you make something idiot-proof, eventually someone will make a better idiot.
I am that better idiot.

Replies are listed 'Best First'.
(z) Re: Undefined value as a symbol reference
by zigdon (Deacon) on Mar 11, 2003 at 13:45 UTC

    You're trying to print to the filehandle $client, but it's not defined in writeClient, is it? or is that 'my' not bound by any codeblock?

    Easy way out - Add a line before the 'print':

    warn "client not defined!" and return unless defined $client;

    -- zigdon

Re: Undefined value as a symbol reference
by robartes (Priest) on Mar 11, 2003 at 13:47 UTC
    $client might be out of scope in sub writeClient. In fact, the fact that the my $client is indented further than the writeClient sub is, kind of hints that way.

    CU
    Robartes-

Re: Undefined value as a symbol reference
by peschkaj (Pilgrim) on Mar 11, 2003 at 13:58 UTC

    I think you've both hit the nail on the head.

    Although writeClient is being called from within the while loop, where $client is a valid variable, $client isn't in scope in writeClient. I failed to realize that I needed to to pass the handle into the subroutine.

    Once I did that everything worked like a charm. Thank you both



    If you make something idiot-proof, eventually someone will make a better idiot.
    I am that better idiot.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-16 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found