[PLUG] perl reading /proc/loadavg

Curtis Poe cp at onsitetech.com
Fri Mar 22 00:41:18 UTC 2002


----- Original Message -----
From: "Paul Heinlein" <heinlein at attbi.com>
To: <plug at lists.pdxlinux.org>
Sent: Thursday, March 21, 2002 4:16 PM
Subject: Re: [PLUG] perl reading /proc/loadavg


> On Thu, 21 Mar 2002, Curtis Poe wrote:
>
> > You are correct that the diamond operator doesn't reset after hitting
> > EOF, but the EOF flag is reset whenever a seek is issued.
>
> The <> operator calls the readline() function internally (or is it the
> other way around?), so I'm trying to interpret the readline() section
> from the perlfunc(1) man page:
>
>   readline EXPR
>
>     Reads from the filehandle whose typeglob is contained in EXPR. In
>     scalar context, each call reads and returns the next line, until
>     end-of-file is reached, whereupon the subsequent call returns undef.
>     In list context, reads until end-of-file is reached and returns a
>     list of lines. Note that the notion of "line" used here is however
>     you may have defined it with $/ or $INPUT_RECORD_SEPARATOR). See the
>     section on $/ in the perlvar manpage.
>
>     When $/ is set to undef, when readline() is in scalar context (i.e.
>     file slurp mode), and when an empty file is read, it returns '' the
>     first time, followed by undef subsequently.
>
> My reading is that <> (aka readline()) will only return true one in a
> scalar context, seemingly regardless of any seek()s run on the
> filehandle. If you need to combine <> and seek(), then you need to wrap
> the <> in a while loop.
>
> Right?
>
> -- Paul Heinlein <heinlein at attbi.com>

Paul,

Could you rephrase your question?  I am particularly stumbling on this bit:

> My reading is that <> (aka readline()) will only return true one in a
> scalar context

Do you mean "will only return true once"?  I'm not sure what you're asking.

The diamond operator doesn't return true or false.  It will return data, or
undef.  The data could simply be the empty string, so it would evaluate as
false.  It could be "Ovid", which evaluates as true.  Perl handles "truth"
differently from most other languages, so I am am wondering if you and I are
just using different terminology?

The classic example of using seek to reset the EOF flag comes straight from
"perldoc -f seek":

    This is also useful for applications emulating "tail -f". Once
    you hit EOF on your read, and then sleep for a while, you might
    have to stick in a seek() to reset things. The "seek" doesn't
    change the current position, but it *does* clear the end-of-file
    condition on the handle, so that the next "<FILE>" makes Perl
    try again to read something. We hope.

    If that doesn't work (some stdios are particularly
    cantankerous), then you may need something more like this:

        for (;;) {
            for ($curpos = tell(FILE); $_ = <FILE>;
                 $curpos = tell(FILE)) {
                # search for some stuff and put it into files
            }
            sleep($for_a_while);
            seek(FILE, $curpos, 0);
        }

Do you like the words "We hope" in that text?  If that doesn't inspire
confidence in Perl ... :)

This suggests to me that we're talking about a non-portable feature which
your work-around managed to solve.  Unfortunately, I'm at the end of my
rather limited intellectual rope, here, so I can only hope that some Savior
with deeper wisdom has a little more rope handy.

Cheers,
Curtis "Ovid" Poe
--
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push at A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift at a;shift at a if $a[$[]eq$[;$_=join q||, at a};print $_,$/for reverse @A






More information about the PLUG mailing list