Bloody hell.

I'm largely thinking aloud, and this is not a deep meditation: I'm just wondering if it was a design decision to not allow ->input_record_separator to be handle specific. It seems rather silly and unintuitive, I started using IO::File a lot so I could turn off buffering on some streams, but it sure seems to lack some consistency. I really wanted to and did create a uniform method ->get_fh('file') so I could have centralized error-catching and logging for a system that might process multiple files using poe or the like. If one process takes on multiple concurrent FH what is the plan of action? -- Read the whole file in and split it with a regexen? If you have to match at the end of the line how do you go about setting m/$/m to a custom regex? Or should I ignore $ and compile the regex for each possible line termination.


grr sometimes


Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re: IO::File is gimp.
by xdg (Monsignor) on Aug 12, 2007 at 20:34 UTC

    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.

      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