correct • elegant • free

△ comp.lang.perl △


Filehandle as object instance variable? ▻

tricky i/o with files

Followups to comp.lang.perl.misc, as comp.lang.perl is going away.

In article <3tru66$>,
Scott A. Janousek <> wrote:
>I was thinking of keeping an array to store the documents, and then if
>the expression is found, the array (document) is printed out, otherwise
>the next document is read.

Sounds about right.  Here's the guts of a routine which deletes messages
from a Unix-style mbox file.

        while ($mmsg = $mboxin->next) {
                $smsg = Std11->frommbox($mmsg);
                if (&$predicate($smsg->split)) {
                        print $mboxio $$mmsg;
        truncate($name, $mboxio->tell) or
            warn "$0: can't truncate $name: $!\n";

$mboxin is an Mbox (which ISA FileHandle) opened for input; $mboxio is
an Mbox opened for input and output ("+<") (and locked, but I've omitted
that part for clarity).

The Mbox->next method returns a reference to a scalar holding the next
message read from $mboxin.  The message is munged slightly by frommbox
and split, then handed to &$predicate.  $predicate is a code reference;
the routine called decides whether the message should be kept and
returns true or false accordingly.

The last line should read something like

        truncate($mboxio, $mboxio->tell) or warn ...

but there is a bug in perl5.001l (already reported) which prevents this
from working; I have to use the original file name instead.

Tim Goodwin        | "Once policy-based routing is solved, some new problem
Public IP Exchange | with routing will arise." -- Marshall T Rose

Original headers:

From: (Tim Goodwin)
Newsgroups: comp.lang.perl,comp.lang.perl.misc
Subject: Re: tricky i/o with files
Followup-To: comp.lang.perl.misc
Date: 11 Jul 1995 13:24:33 +0100
Organization: PIPEX, 216 Science Park, Cambridge, England
Message-ID: <3ttqi1$>
References: <3tru66$>

△ comp.lang.perl △


Filehandle as object instance variable? ▻