Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Says turnstep:

> why not just post it here? The whole thing is less
> than 70 lines...

One reason was that I thought it was longer than it was.

But the other reason is that I didn't think it would be useful to post it without also posting the demo program, because I was afraid nobody would be able to figure out how to use it. And the demo program is another 77 lines.

But I guess it couldn't hurt to give it a try. This is ArrayHashMonster.pm:

#!/usr/bin/perl # # Copyright 2000 M-J. Dominus and the Mad Hungarian Software Works # Unauthorized distribution strictly prohibited # package ArrayHashMonster; require 5.00502; use strict; our $VERSION = '0.03'; BEGIN { @ArrayHashMonster::Siphuncle::ISA = __PACKAGE__ } sub new { my ($pack, $acode, $hcode) = @_; my $siphuncle = $pack->new_siphuncle; my @a; tie @a => $pack, $siphuncle, $acode, $hcode; \@a; } sub new_siphuncle { my ($pack) = @_; my %h; tie %h => $pack; return \%h; } sub set_flag { my ($self, $flagref) = @_; $self->{FLAG} = $flagref; } sub TIEHASH { my ($pack) = @_; my $self = {FLAG => undef}; bless $self => $pack . '::Siphuncle'; } sub TIEARRAY { my ($pack, $siphuncle, $acode, $hcode) = @_; my $flag = undef; my $self = {FLAG => \$flag, SIPHUNCLE => $siphuncle, ACODE => $acode +, HCODE => $hcode}; (tied %$siphuncle)->set_flag(\$flag); bless $self => $pack; } sub FETCH { my ($self, $key) = @_; if (ref($self) =~ /Siphuncle/) { ${$self->{FLAG}} = $key; return 1; } elsif ($key == 0) { return $self->{SIPHUNCLE}; } elsif (defined $ {$self->{FLAG}}) { my $rv = $self->{HCODE}->($ {$self->{FLAG}}); undef $ {$self->{FLAG}}; return $rv; } else { return $self->{ACODE}->($key); } } 1;
This is the demo program that uses it:

#!/usr/bin/perl use ArrayHashMonster; # Hi. What am I for? # I provide a reference that looks like a reference to an array and # like a reference to a hash at the same time. # If $z is an ArrayHashMonster object, then you can ask for either of # $z->{foo} or $z->[7]. # # Some sample demonstration uses follow. # Tests start here. print "\n\nTest set 1:\n"; my $x = new ArrayHashMonster sub {"Array $_[0]"}, sub {"Hash $_[0]"}; print $x->[2], "\n"; print $x->{jan}, "\n"; print $x->{February}, "\n"; print $x->{2}, "\n"; # This shows that $x->[2] and $x->{2} are differe +nt print $x->[4], "\n"; ################################################################ print "\n\nTest set 2:\n"; my @fmo = qw(xx janvier février mars avril mai juin juillet août septembre octobre novembre décembre); my @emo = qw(xx January February March April May June July August September October November December); my %e2f; for ($i = 1; $i <= 12; $i++) { my $abbr = substr($emo[$i], 0, 3); $e2f{lc $emo[$i]} = $e2f{lc $abbr} = $fmo[$i]; } my $y = new ArrayHashMonster sub {$fmo[$_[0]]}, sub {$e2f{lc $_[0]}}; print "Feb: ", $y->[2], "\n"; print "Jan: ", $y->{jan}, "\n"; print "Feb: ", $y->{February}, "\n"; print "Apr: ", $y->[4], "\n"; ################################################################ print "\n\nTest set 3:\n"; opendir T, '.' or exit 0; my $start = telldir T; sub fileinfo { my $file = shift; my @stat = stat $file; $stat[2] = sprintf "%o", $stat[2]; $stat[2] =~ s/^4/ d/; $stat[2] =~ s/^10/ f/; $stat[2] =~ s/(\w)0/$1 /; sprintf "File %-32s Size %8d Owner %6d Mode %6s\n", $file, @stat[7,4,2]; } sub filenumber { my $index = shift; seekdir T, $start; my $i = 1; while ($i++ < $index) { readdir T; } return readdir T; } my $z = new ArrayHashMonster \&filenumber, \&fileinfo or die; my $n = 1; for (;;) { my $filename = $z->[$n]; last unless defined $filename; print $z->{$filename}; $n++; }

In reply to Re: ArrayHashMonster by Dominus
in thread ArrayHashMonster by Dominus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2019-02-17 22:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I use postfix dereferencing ...









      Results (98 votes). Check out past polls.

      Notices?