t-rex has asked for the wisdom of the Perl Monks concerning the following question:

hello monks, i am trying something like

sub client_socket_create() { my $socket = new IO::Socket::INET ( PeerHost => 'ltsbml02.aus.stglabs.ibm.com', PeerPort => 7777, Proto => 'tcp', ); print "$socket \n"; return $socket; }

then my calling script calls client_main()passing this $socket but to my shock when i print this $socket in client_main() it gives me nothing a space just

sub client_main($) { my $socket = @_; print "$socket \n"; # some processing # }

what could i possibly doing wrong ?

Replies are listed 'Best First'.
Re: socket programming error while using it in modular way
by hippo (Archbishop) on Jun 28, 2016 at 16:28 UTC

    You are assigning a list (@_) to a scalar ($socket). Fixing this, cleaning up the indenting, removing the prototypes and connecting to a public service gives:

    #!/usr/bin/env perl use strict; use warnings; use IO::Socket; my $s = client_socket_create (); client_main ($s); exit; sub client_socket_create { my $socket = IO::Socket::INET->new ( PeerHost => 'www.perlmonks.org', PeerPort => 80, Proto => 'tcp' ); print "$socket \n"; return $socket; } sub client_main { my ($socket) = @_; print "$socket \n"; }

    which prints out the same socket twice as one supposes is intended.

Re: socket programming error while using it in modular way
by Cow1337killr (Monk) on Jun 28, 2016 at 21:44 UTC

    Just so any beginners that are reading this thread don't get the wrong idea: There are easier ways to surf the Internet using Perl. One does not have to immerse themselves in this level of detail.

    For example, WWW::Mechanize from CPAN.

    One can drive a Formula 1 race car to work. It gives one total control. It also requires specialized training. 99.99% of the population doesn't need that level of control.