Passing a socket as a parameter for a subroutine

M

muzzaman

I am trying to print to a socket from many parts of a program and have
therefore written a few subroutines to simplify the task. The only
issue is that it doesn't seem to pass the socket reference correctly (i
think). I get the following error:

Use of uninitialized value at line ...

which basically relates to a string ($string) I am trying to chomp
after having read it from a socket ($socket). These are the pertinent
commands:

#In main method

$socket = new IO::Socket::INET(PeerAddr => $confighost, PeerPort =>
$configport, Proto => "tcp", Type => SOCK_STREAM )or die( "Cannot
connect to $confighost:$configport : $@\n" );

$returnval = sendandreceive($socket, "Some string here");

#subroutines
sub sendandreceive {
$socket = shift;
$string = shift;
sendtosocket($socket, $string);
$returnstr = receivefromsocket($socket);
return $returnstr;
}

sub sendtosocket {
$socket = shift;
$string = subs(shift, " ", "\+");
$string = "$string\n";
print $socket $string;
}

sub receivefromsocket {
$socket = shift;
$string = <$socket>;
chomp $string;
$string = subs($string, "\+", " ");
return $string;
}

sub subs {
$string = shift;
$remove = shift;
$replace = shift;
$string =~ s/$remove/$replace/gi;
return $string;
}

Any help to (e-mail address removed) would be very much appreciated.

Regards
Muzzaman
 
B

Brian McCauley

muzzaman said:
...written a few subroutines...
sub sendandreceive {
$socket = shift;
$string = shift;
sendtosocket($socket, $string);
$returnstr = receivefromsocket($socket);
return $returnstr;
}

That subroutine stomps all over the global variables $socket, $string,
$returnstr.

You should always declare all variables as lexically scoped in the
smallest applicable scope unless there is a reason not to. This has
nothing to do with Perl, it applies to all programming languages. (If
one accepts that the language not having a concept of lexically scoped
variables is reason not to use them).

Putting "use strict" at the top of your Perl source files will swtich
off implicit declaration of global variables[1] and hense cause perl to
tell you when you've forgotten to declare a variable.

[1] Actually that's a half-truth, but if you are new to Perl it helps to
think of it this way.
 
A

Anno Siegel

Brian McCauley said:
... If one accepts that the language not having a concept of lexically
scoped variables is reason not to use them.

Ever used a language named Perl 4? :)

Anno
 
B

Brian McCauley

Anno said:
Ever used a language named Perl 4? :)

Only briefly. I do often use a language called M (formerly know as
MUMPS). This too lacks lexically scoped variables so just as in Perl 4
all the variables used locally in subroutine need to be dynmically scoped.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top