Home > Archive > PERL Miscellaneous > June 2005 > Passing a socket as a parameter for a subroutine
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Passing a socket as a parameter for a subroutine
|
|
| muzzaman 2005-06-06, 3:57 pm |
| 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 murray.thomas@gmail.com would be very much appreciated.
Regards
Muzzaman
| |
| Brian McCauley 2005-06-07, 4:01 pm |
| muzzaman wrote:
> ...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.
| |
| Anno Siegel 2005-06-08, 3:59 pm |
| Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
> ... 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
| |
| Brian McCauley 2005-06-08, 3:59 pm |
| Anno Siegel wrote:
> Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
>
>
>
>
> 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.
|
|
|
|
|