[BNE-PM] The Perl one-liner
David Bussenschutt
d.bussenschutt at mailbox.gu.edu.au
Thu Sep 19 23:21:20 CDT 2002
Speaking of filehandle identifiers, typeglobs and kludges.....
just the day before yesterday, I was funbling with this very same
"typeglob" issue, and how to pass this around into functions etc. EXCEPT,
mine wasn't a filehandle, so I couldn't use IO:File ... it was a Socket.
I didn't want to 'use IO:Socket' because it's got quite large overheads in
comparison to just 'use Socket'.
Of course, the other problem was that I was creating the socket inside a
->new() object creation routine, and had to then save this newly created
socket/filehandle into the anonymous hash that the object uses for it's
object data.
I eventually figured out the following...
....
below this is an extract showing the creation of the object/socket. The
really important line is the use of Symbol::gensym() to generate a
reference to a typeglob that I then store into a scalar ( $self->{socket}
) that is part of the object $self.
This bit took me a while to figure out.....eventually I stumbled onto a
note about 'Symbol' in one of my 4 perl reference books , and I
immediately said "that's it!"... ;-)
....
use Symbol;
sub new {
my invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
server => '1.2.3.4',
port => '1234',
@_,
};
${$self->{socket}} = Symbol::gensym(); # get a
reference to a new typeglob
socket(${$self->socket}},PF_INET,SOCK_STREAM,getprotobyname('tcp')); #
make the socket
my $i_addr = inet_aton($self-{server}); # get ip
of server
my $addr = sockaddr_in($self->{port},$i_addr); #get full addr of
server (ip+port)
conect(${$self->socket}},$addr); # connect to the
server.
select((select(${$self->socket}}), $|=1)[0]); # autoflush
buffers
return bless $self, $class; # return the new object
}
David.
--------------------------------------------------------------------
David Bussenschutt Email: D.Bussenschutt at mailbox.gu.edu.au
Senior Computing Support Officer & Systems Administrator/Programmer
RedHat Certified Engineer.
Member of Systems Administrators Guild of Australia.
Location: Griffith University. Information Technology Services
Brisbane Qld. Aust. (TEN bldg. rm 1.33) Ph: (07)38757079
--------------------------------------------------------------------
Derek Thomson <derek at wedgetail.com>
Sent by: owner-brisbane-pm-list at pm.org
20/09/2002 01:59 PM
To: Don.Simonetta at mincom.com
cc: anthony at cit.gu.edu.au, brisbane-pm-list at happyfunball.pm.org
Subject: Re: [BNE-PM] The Perl one-liner
Don.Simonetta at mincom.com wrote:
> Totally agree with you Derek.
> My only comment is if you don't want to go to the trouble of importing
> DirHandle, you could use the standard perl functions of opendir &
> readdir.
I've gotten so used to using DirHandle and IO::File, it just came out
that way!
I *always* use these, as the whole file handle identifier thing is a
kludge. When you say:
open FILE, "< file";
... how do you then pass FILE to a function? How do you put it into an
array or a hash? You can't, unless you pass the symbol table entry for
the identifier (the "typeglob"). This is so much low-level hacking for
such a simple thing, and is such a stumbling block to learners, that I
just teach:
$file = IO::File->new("< file");
... right from the start. Since $file is just a reference, you can pass
it to functions, assign it to other variables, put it in arrays and
hashes, and the world is a better and saner place. The "read line"
operator "<>" even works with this, as in "while (<$file>) { ... }", so
there's no reason that I can see to use the older notation.
--
D.
More information about the Brisbane-pm
mailing list