in reply to IO::File is gimp.

That's a great idea.

Because IO::File is a blessed symbol underneath the hood, I don't think was possible to store per-object state as well. Inside-out objects now make that fairly easy. It should be a fairly trivial exercise to subclass IO::File transparently with Class::InsideOut.

package IO::File::Stateful; use Class::InsideOut qw/public register id/; use base 'IO::File'; public 'input_record_separator' => my %IRS; sub new { my $class = shift; my $self = IO::File->new(@_); register( $self, $class ); $IRS{ id $self } = $/; # at time of creation return $self; } sub getline { my $self = shift; local $/ = $IRS{ id $self }; return $self->SUPER::getline(); } 1;

Of course, all the read/write subroutines for IO::Handle would need to be wrapped this same way. Output record separators could be done as well.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: IO::File is gimp.
by chromatic (Archbishop) on Aug 12, 2007 at 20:54 UTC
    Because IO::File is a blessed symbol underneath the hood, I don't think was possible to store per-object state as well.

    Sure it is! You just have to reach into the typeglob slots. It's ugly (so ugly that I won't show it here), but it's possible.

      Ugly? C'mon, it's perl...
      my $fh = IO::File->new; *{$fh} = \ "foo"; # we'll split at foo $fh->open("< $file") or die "barf: $!"; my @ary; { local $/ = ${*{$fh}{SCALAR}}; @ary = <$fh>; }

      :-)

      update: a bit less ugly:

      { local $/ = $$$fh; @ary = <$fh>; }

      More ugly:
      You could also shoehorn a hash reference reference into the typeglob SCALAR slot to associate more stuff with it:

      *{$fh} = \ { input_record_separator => "foo" }; { local $/ = $$$fh->{input_record_separator}; @ary = <$fh>; }

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      Socket does this IIRC.

      ---
      $world=~s/war/peace/g