In perdoc Socket page, they use global filehandle for socket. But if I create a socket in a subroutine called by child processes, is it better to use lexical filehandle still using Socket ?
like this:
use strict;
use Socket;
sub sendData
{
my $proto = getprotobyname('tcp');
my $socket;
socket($socket, PF_INET, SOCK_STREAM, $proto);
...
close($socket)
}
instead of:
sub sendData
{
my $proto = getprotobyname('tcp');
socket(SOCKET, PF_INET, SOCK_STREAM, $proto);
...
close(SOCKET)
}
It seems to be ok, but I don't know if it's a better practice or completely useless...
Thanks
Yes, it's a better practice to use lexical filehandles. But Perl 5.0 didn't have them, so there's plenty of older code and documentation that uses global filehandles, and much of it hasn't been updated to use lexical ones.
P.S. You know that you can say
socket(my $socket, PF_INET, SOCK_STREAM, $proto);
instead of putting the my
on the previous line, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With