in reply to Re: Typeglob substitution in objects based typeglob
in thread Typeglob substitution in objects based typeglob

No. I want to override builtin connect() function (CORE::connect).
  • Comment on Re^2: Typeglob substitution in objects based typeglob

Replies are listed 'Best First'.
Re^3: Typeglob substitution in objects based typeglob
by Anonymous Monk on Jan 19, 2011 at 13:59 UTC
      Prototype does not help me. I'll try to explain what I want to do. I want to socksify any module that uses the network. So, I need to override CORE::connect(). Here what I get:
      use Socket; BEGIN { *CORE::GLOBAL::connect = sub(*$) { my ($socket, $name) = @_; return CORE::connect($socket, $name) if (scalar(caller 2) eq ' +IO::Socket::Socks'); my ($port, $host) = sockaddr_in($name); $host = inet_ntoa($host); $_[0] = IO::Socket::Socks->new( ProxyAddr => 'localhost', ProxyPort => 1080, SocksVersion => 5, ConnectAddr => $host, ConnectPort => $port, SocksDebug => 1 ) or return undef; return 1; } } use IO::Socket::Socks;
      It works with simple socket
      my $remote = "perlmonks.org"; my $port = 80; my $iaddr = inet_aton($remote) || die "no host: $remote"; my $paddr = sockaddr_in($port, $iaddr); my $proto = getprotobyname('tcp'); socket(my $sock, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect($sock, $paddr); syswrite($sock, "GET / HTTP/1.0\r\n"); close $sock;
      But doesn't work with IO::Socket::INET:
      my $sock = IO::Socket::INET->new('perlmonks.org:80') or die $@; $sock->syswrite("GET / HTTP/1.0\r\n"); $sock->close();
      The last example raises SIGPIPE, because $sock remains closed IO::Socket::INET object.
      Any ideas?