I think this is about at the limit of length acceptability--~70 lines. It's taken from O'Reilly's Advanced Perl Programming, pp 143-144. It's an example of Tying arrays, in this case making a File into an array. It seems to be losing the value of the my variables $F_OFFSETS and $F_FILEHANDLE. Can anybody please explain why, or if Perl has changed since this 1997 example was done? Code:
tie @lines, 'TieFile', 'just_lines'; print $lines[20]; exit; package TieFile; use Symbol; #use strict; my $F_OFFSETS = 0; my $F_FILEHANDLE = 1; sub TIEARRAY { my ($pkg, $filename) = @_; my $fh = gensym(); open ($fh, $filename) || die "Could not open file: $!\n"; print "TIEARRAY:\$fh=$fh\n"; # correctly says it is a glob bless [ [0], $fh # stores it here ], $pkg; } sub FETCH { print "fetch:\$F_FILEHANDLE=$F_FILEHANDLE\n"; # F_FILEHANDLE has d +isappeared! print "defined!\n" if defined ($F_FILEHANDLE); print "not defined!\n" if !defined ($F_FILEHANDLE); my ($obj, $index) = @_; my $rl_offsets = $obj->[$F_OFFSETS]; my $fh = $obj->[$F_FILEHANDLE]; # my $fh = $obj->[1]; print "fetch:\$fh=$fh\n"; print "fetch:\$rl_offsets=$rl_offsets\n"; if ($index > @$rl_offsets) { print "reading_until\n"; $obj->read_until($index); } else { seek ($fh, $rl_offsets->[$index], 0); } return (scalar<$fh>); } sub STORE { die "Sorry, cannot update file using package TieFile\n"; } sub DESTROY { my ($obj) = @_; close ($obj->[$F_FILEHANDLE]); } sub read_until { my ($obj, $index) = @_; my $rl_offsets = $obj->[$F_OFFSETS]; my $last_index = @$rl_offsets - 1; my $last_offset = $rl_offsets->[$last_index]; my $fh = $obj->[$F_FILEHANDLE]; print "read_until:\$fh=$fh\n"; seek ($fh, $last_offset, 0); my $buf; while (defined($buf = <$fh>)) { $last_offset += length($buf); $last_index++; push (@$rl_offsets, $last_offset); last if ($last_index > $index); } } 1;
And the output I get:
TIEARRAY:$fh=GLOB(0x10011028) fetch:$F_FILEHANDLE= not defined! fetch:$fh=ARRAY(0x1002e8bc) fetch:$rl_offsets=ARRAY(0x1002e8bc) reading_until read_until:$fh=ARRAY(0x1002e8bc) Not a GLOB reference at TieFile.pm line 56.

In reply to TIEARRAY example out of date? by ongaku

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, details, 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, summary, 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.