in reply to Typeglob substitution in objects based typeglob

I'm not sure I'm understanding you correctly, but it somehow sounds like you want to subclass IO::Socket or IO::Socket::INET, and override selected methods with your own implementations (?)   Calls to other methods would then be forwarded to the superclass...

#!/usr/bin/perl -w use strict; package IO::Socket::WhatEver; use IO::Socket::INET; use base "IO::Socket::INET"; sub connect { print "doing my own stuff in connect()...\n"; my $sock = shift; return $sock->SUPER::connect(@_); } package main; my $mysock = IO::Socket::WhatEver->new( PeerAddr => 'perlmonks.org', PeerPort => 'http(80)', Proto => 'tcp', ); # ...
$ ./883113.pl doing my own stuff in connect()...

Instead of calling $sock->SUPER::connect(...) in the overridden method (which eventually calls the connect() builtin), you can of course also call your own low-level connect() implementation... provided it does something which is compatible with what the regular builtin would do.

Replies are listed 'Best First'.
Re^2: Typeglob substitution in objects based typeglob
by OlegG (Monk) on Jan 19, 2011 at 13:47 UTC
    No. I want to override builtin connect() function (CORE::connect).
        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?