Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Reopen file when contents changed?

by bofh_of_oz (Hermit)
on Jun 07, 2005 at 14:47 UTC ( [id://464329]=note: print w/replies, xml ) Need Help??


in reply to Reopen file when contents changed?

After reading all these *very* helpful advices, here's what i've got:

use strict; use warnings; while() { my @loads; my $i = my $cpuload = 0; open(INFIL,"< /proc/stat") || die("Unable To Open /proc/stat\n"); <INFIL> =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+).*/; @loads = ($1, $2, $3); sleep 1; seek INFIL, 0, 0; <INFIL> =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+).*/; foreach ($1, $2, $3) { $cpuload += $_ - $loads[$i++]; } close(INFIL); print "$cpuload\n"; }

Tie::File looks interesting, so that might not be the final version ;)

Thanks a lot folks!

--------------------------------
An idea is not responsible for the people who believe in it...

Replies are listed 'Best First'.
Re^2: Reopen file when contents changed?
by blazar (Canon) on Jun 07, 2005 at 15:31 UTC
    use strict; use warnings;
    Good!
    while()
    I think that
    while(1)
    is more customary. To be fair I thought yours wouldn't have worked at all, but to be sure I tried and it did!
    { my @loads; my $i = my $cpuload = 0; open(INFIL,"< /proc/stat") || die("Unable To Open /proc/stat\n");
    I, for one (but I'm not the only one!), recommend using the three args form of open. Also, low precedence or is better suited for flow control and you may benefit from including $! in the error message.
    <INFIL> =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+).*/; @loads = ($1, $2, $3);
    This is overly complex for what it does, IMHO. I would probably do something like
    my @loads = (<$fh> =~ /\d+/g)[0,1,2];
    instead. Of course this is not strictly equivalent to yours. Indeed it may be worth to check that the line is the correct one. In that case you may still do something like this:
    local $_=<$fh>; (warn "something wrong!\n"), next unless /^cpu\b/; my @loads = (/\d+/g)[0..2];
    sleep 1; seek INFIL, 0, 0; <INFIL> =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+).*/; foreach ($1, $2, $3) { $cpuload += $_ - $loads[$i++]; } close(INFIL);
    So you want to iterate over the two lists in parallel. Now this is a situation in which some Perl6 features would turn out to be very handy!

    However, and this is not strictly perl-specific, instead of getting the same info twice per cycle, you may get it once only, and take a "backup" version of it to compare with.

      Side discussion: why

      while () { ... }

      is interpreted as

      while (1) { ... }

      I can't find anything relevant in perldoc perlsyn.

      $ perl -MO=Deparse -e 'while () {print "tick!\n"}' while (1) { print "tick!\n"; }

      Update: Below is the deparse from perl 5.6.1. Above was 5.8.

      $ perl561 -MO=Deparse -e 'while () {print "tick!\n"}' for (;;) { print "tick!\n"; }

      Update 2: See more discussion in thread while ()

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://464329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found