Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

koschei (147)

koschei
  useperl@dellah.org
http://eh.org/~koschei/

Um. Me. Iain Truskett. Can be found as either Spoon or Koschei around the place, and as Braxiatel in rare circumstances. Um. Cool. Ta.

My perl svn repository [dellah.org] if you want bits of code by me. Also see dellah.org [dellah.org] for most of them in action.

My friends [perl.org], foes [perl.org], fans [perl.org], and freaks [perl.org]. And you can see what my friends are journalling [perl.org].

And everybody's journals [perl.org] (in order of updating).

Journal of koschei (147)

Monday June 10, 2002
06:32 PM

Hmm. So

[ #5561 ]

Following on from Simon's example of many modules for simple things...

#!/usr/bin/perl -w
use strict;
use Time::Piece;
use Time::Duration;
use IO::File;
 
my $uptime = do {
    my $l = (IO::File->new('/proc/uptime')
            or die "No! Can't open!: $!\n")->getline;
    $l =~ /^(\d[\d.]+)\s+(\d[\d.]+)$/;
    $1||die "Invalid uptime!\n";
};
my $now = Time::Piece->new;
my $boot = $now-$uptime;
 
printf <<"EOF", duration($uptime), $boot->cdate, $now->cdate;
Uptime: %s
Boot:   %s
Now:    %s
EOF

The question is: is it useful?

(Alternative question is: maybe I should've used more modules?)

[Later edited courtesy jdporter...]

#!/usr/bin/perl -w
use strict;
use Time::Piece;
use Time::Duration;
use IO::File;
 
my ( $uptime ) = (
    IO::File->new('/proc/uptime') or die "No! Can't open!: $!\n"
)->getline =~ /^(\d[\d.]+)\s+(\d[\d.]+)$/ or die "Invalid uptime!\n";
 
my $now = Time::Piece->new;
my $boot = $now-$uptime;
 
printf <<"EOF", duration($uptime), $boot->cdate, $now->cdate;
Uptime: %s
Boot:   %s
Now:    %s
EOF

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I'm quite happy that people are using Time::Duration for things!
    • Ah yes - my apologies; I forgot.

      Thank you for this module --- it's marvellous. It's one of those things that you think "surely someone's written that". I wrote some code for a friend a few years back and he was surprised at how many lines were needed to nicely format the duration. This module solves that =)

      cheers!

      (Any sarcasm you read into the above was not put there by me.)
      --
        ---ict / Spoon
    • Wow! That simplifies a script I wrote to calculate resolution and response time for customer ticket.

      I will thank TorgoX in advance for what I think this will do and how it will help me. Cool.
  • Methinks it's time to write 'uptime' as an extension so us poor slobs using Solaris can use it (no /proc/uptime here). :)
  • How about this tweak (untested):

        my( $uptime ) =   (IO::File->new('/proc/uptime') or die "No! Can't open!: $!\n")->getline =~ /^(\d[\d.]+)\s+(\d[\d.]+)$/or die "Invalid uptime!\n";