Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
use strict; use warnings;
I think that
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.

In reply to Re^2: Reopen file when contents changed? by blazar
in thread Reopen file when contents changed? by bofh_of_oz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-08-08 10:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found