Thread Schreibweise für GLOB
(4 answers)
Opened by rosti at 2018-11-24 12:55
Interesant, 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? Die andere Sache ist die Verwendung: Ein solcher Glob steht mir in jeder Funktion zur Verfügung, die Alternative wäre eine globale Variable die sich statisch verhält (Klassenvariable). Verhält sich *self auch statisch (wenn $self die Instanz in einer Methode ist)? Ich überlege mir wie ich das mal prüfen könnte. In Verwendung untenstehend: Code (perl): (dl
)
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; So wies aussieht ist in der main kein Rankommen an diesen Glob. . Last edited: 2018-11-25 08:18:50 +0100 (CET) |