1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use strict; use warnings; use IO::Socket; use base qw(IO::Socket::INET); use constant CRLF => "\r\n"; my $host = 'smtp.mail.yahoo.com'; my $m = main->new(PeerAddr => "$host:25", Timeout => 1) or die $@; # Eigenschaften via Glob anhängen *m->{FOO} = 'foo'; # einfach Glob *$m->{FOO} = 123; # Glob auf Glob $, = "\n"; print $m->getline, *m->{FOO}, ${*m}{FOO}, *$m->{FOO}, ${*$m}{FOO}; $m->print('Helo localhost', CRLF); print $m->getline; # Ausgabe untenstehend __END__ 220 smtp.mail.yahoo.com ESMTP ready foo foo 123 123 250 smtp403.mail.ir2.yahoo.com Hello localhost [79.254.13.82])
2018-11-24T11:55:21 rostiPS: Solche Schreibweisen finden sich zuhauf in der libnet. So ist es bspw. möglich, *Handles weitere Eigenschaften zuzuweisen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use strict; use base qw(IO::Socket::INET); use Data::Dump qw(dump); my $host = 'smtp.mail.yahoo.com'; my $m = main->new(PeerAddr => "$host:25", Timeout => 1) or die $@; *$m->{FOO} = 123; # Glob auf Glob @{*$m} = qw(glib glob); *$m->[0] = 'glub'; ${*$m} = 'Scalar Ref'; dump $m;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
do {
require Symbol;
my $a = bless(Symbol::gensym(), "main");
*{$a} = \"Scalar Ref";
*{$a} = ["glub", "glob"];
*{$a} = {
FOO => 123,
io_socket_domain => 2,
io_socket_proto => 6,
io_socket_timeout => 1,
io_socket_type => 1,
};
$a;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Eigenschaften via Glob anhängen *m->{FOO} = 'foo'; # einfach Glob print dump $m; do { require Symbol; my $a = bless(Symbol::gensym(), "main"); *{$a} = { io_sock_nonblocking => 0, io_socket_domain => 2, io_socket_proto => 6, io_socket_timeout => 1, io_socket_type => 1, }; $a; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package HashFile; # persistent tied Hash ín File use strict; use warnings; use Tie::Hash; use IO::File; use Fcntl qw(:flock); use base qw(Tie::StdHash); sub TIEHASH{ my $class = shift; my %opts = ( flock => 0, file => '', @_); return eval{ my $self = $class->SUPER::TIEHASH(); my $fh = IO::File->new(); $fh->open($opts{file}, O_CREAT|O_BINARY|O_RDWR) || die $!; flock $fh, LOCK_EX if $opts{flock}; *self->{FH} = $fh; *self->{OPTS} = \%opts; my $h = $self->_thaw(); %$self = %$h; $self; }; } sub write{ my $self = shift; $self->_freeze; } sub _freeze{ my $self = shift; my $fh = *self->{FH}; $fh->seek(0,0); $fh->truncate(0); use bytes; while( my ($k, $v) = each %$self){ my $def = defined $v ? 1 : 0; $v ||= ''; $fh->print(pack("V", length $k), pack("V", length $v), $k, $v, $def); } } sub _thaw{ my $self = shift; my $fh = *self->{FH}; $fh->seek(0,0); my %h = (); use bytes; while( read($fh, my $lens, 8) ){ my ($klen, $vlen) = unpack "VV", $lens; read($fh, my $k, $klen); read($fh, my $v, $vlen); read($fh, my $def, 1); $h{$k} = $def ? $v : undef; } \%h; } sub DESTROY{ my $self = shift; $self->_freeze if *self->{OPTS}{auto}; } 1;######################################################################### package main; use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; tie my %h, 'HashFile', file => 'hashfile.bin', auto => 1 or die $@; #%h = (foo => '', bar => 1, baz => undef); print Dumper \%h;
2018-11-25T07:11:29 rostiInteresant, danke Dir
dump zeigt jedoch nicht alles:
Code (perl): (dl )1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17# Eigenschaften via Glob anhängen *m->{FOO} = 'foo'; # einfach Glob print dump $m; do { require Symbol; my $a = bless(Symbol::gensym(), "main"); *{$a} = { io_sock_nonblocking => 0, io_socket_domain => 2, io_socket_proto => 6, io_socket_timeout => 1, io_socket_type => 1, }; $a; }
Also gibt es ja doch Unterschiede?
1 2 3 4 5 6
use strict; use Data::Dump qw/dump/; *m->{FOO} = 'foo'; # einfach Glob dump %main::m; dump $::{m};