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

Hi Perlmonks,

I have recently started working on Perl LWP. I have been able to get a very simple program to work:

my $conn = LWP::UserAgent->new(keep_alive => 1,) or die(Failed to create UserAgent"); $conn->timeout("3"); my $cache = $conn->conn_cache(); my $url = "http://192.168.1.2/test.html" #My local machine my $request = HTTP::Request->new('GET', $url); my $response = $conn->request($request); print STDERR "$response\n"; $cache->drop();
The program works fine and now I am trying to understand the flow. Using debugs, I could figure out that _new_socket() in http.pm creates the socket, connect() in IO::Socket is used to connect to the remote server and drop() routine in ConnCache.pm is used for clearing connections. However, I do not understand where the socket is getting closed. I expected close() or shutdown() in Socket.pm to get invoked from drop(), but neither are (but the FIN is being sent by the client). Am I missing something here? Can some one help me understand where the close() routine (or any equivalent) is getting called.

Thanks & Regards,

Replies are listed 'Best First'.
Re: Query regarding LWP
by Discipulus (Canon) on Jun 23, 2015 at 09:31 UTC
    Just a suggestion (i'm really not an expert): IO::Socket is a IO::Handle and as all handles it autoclose when it goes out of scope and in other circumstances too (filehandle have close on exec: see perlipc ).
    In general sense, Perl does a lot of kind things for you, behind the scenes, garbage collection is one of theese.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for the response. Is there a way to confirm if it is the garbage collector that is performing the socket close in this scenario? I know the specific routine where the close seems to be happening. Any other information that can help confirm the same would be great. Thanks.