From: John Napiorkowski Date: Sun, 27 Mar 2011 14:17:11 +0000 (-0400) Subject: filled out more fh tie methods X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=10a7372034ad38a1ad754abc910a98e6fa0f2814;p=catagits%2FHTML-Zoom.git filled out more fh tie methods --- diff --git a/lib/HTML/Zoom/ReadFH.pm b/lib/HTML/Zoom/ReadFH.pm index 6e3de6b..8d33ad6 100644 --- a/lib/HTML/Zoom/ReadFH.pm +++ b/lib/HTML/Zoom/ReadFH.pm @@ -1,10 +1,25 @@ package HTML::Zoom::ReadFH; use strictures 1; +use Symbol; + +sub TIEHANDLE { return shift } +sub FILENO { return undef } +sub READ { return shift->read(@_) } +sub CLOSE { return shift->close(@_) } +sub OPEN { return shift->open(@_)} + +sub open { 1 } +sub opened { 1 } +sub close { "The door shuts behind you with a ominous boom" } sub from_zoom { my ($class, $zoom) = @_; - bless({ _zoom => $zoom }, $class) + my $self = bless Symbol::gensym(), ref($class) || $class; + tie *$self, $self; + *$self->{_zoom} = $zoom; + $self->open; + return $self; } sub to_zoom { @@ -13,15 +28,15 @@ sub to_zoom { # If this turns out to merely re-aim the gun at your left nipple, please # come complain with a documented use case and we'll discuss deleting it. die "Already started reading - there ain't no going back now" - if $self->{_stream}; - $self->{_zoom} + if *$self->{_stream}; + *$self->{_zoom} } sub getline { my $self = shift; my $html; - my $stream = $self->{_stream} ||= $self->{_zoom}->to_stream; - my $producer = $self->{_producer} ||= $self->{_zoom}->zconfig->producer; + my $stream = *$self->{_stream} ||= *$self->{_zoom}->to_stream; + my $producer = *$self->{_producer} ||= *$self->{_zoom}->zconfig->producer; while (my ($evt) = $stream->next) { $html .= $producer->event_to_html($evt); last if $evt->{flush}; @@ -43,17 +58,40 @@ sub getlines { sub read { my ($self, $buff, $len, $off) = @_; $off = defined $off ? $off : 0; - $self->{_pos} = defined $self->{_pos} ? $self->{_pos} + $off : $off; - my $html = $self->{_html} ||= join '', $self->getlines; - return if $self->{_pos} > length $html; - $_[1] = substr $html, $self->{_pos}, $len; + *$self->{_pos} = defined *$self->{_pos} ? *$self->{_pos} + $off : $off; + my $html = *$self->{_html} ||= join '', $self->getlines; + return if length($html) < *$self->{_pos}; + $_[1] = substr $html, *$self->{_pos}, $len; unless(my $overflow = $len - length($buff)) { $_[1] .= "\0" x $overflow; } - $self->{_pos} += $len; + *$self->{_pos} += $len; } -sub close { "The door shuts behind you with a ominous boom" } +sub stat { + + warn "GOT THE CUSSTOM STAT"; + + my $self = shift; + return 1 unless wantarray; + my $html = *$self->{_html} ||= join '', $self->getlines; + my $len = length $html; + + return ( + undef, undef, # dev, ino + 0666, # filemode + 1, # links + $>, # user id + $), # group id + undef, # device id + $len, # size + undef, # atime + undef, # mtime + undef, # ctime + 512, # blksize + int(($len+511)/512) # blocks + ); +} 1; diff --git a/t/to_fh.t b/t/to_fh.t index 5c25295..eacd04c 100644 --- a/t/to_fh.t +++ b/t/to_fh.t @@ -53,12 +53,13 @@ is $html, $lines, } { - $fh->read(my $buff, 4, 14); + ##$fh->read(my $buff, 6, 20); + $fh->read(my $buff, 6, 14); ok $buff, 'got a populated buffer'; - is $buff, '