SkullOne has asked for the wisdom of the Perl Monks concerning the following question:

The program I'm trying to write will be a perl based client for a new uptimes gathering server.
Using perl is pretty obvious - everyone has it.
Now for the code, I've got everything down, except one part thats been nagging me.
The only line i have trouble with goes like this:

open(UPTOP,"cat /proc/uptime|awk '{print $1}'");

But, as I execute it, no errors come up, and this line is executed fine, but this line of code produces absolutely no output.
If I remove cat xxx and awk xxx the line will gather /proc/uptime correctly. Ive also tried system() and exec() and neither want to work with awk like this. Im not experienced enough with perl to know of any other better way to accomplish this.
Any ideas would be helpfull.

Replies are listed 'Best First'.
Re: Using awk within system() or open()
by blakem (Monsignor) on Oct 24, 2001 at 12:58 UTC
    I think you want something like:
    #!/usr/bin/perl -wT use strict; my $filename = '/proc/uptime'; open(UTOP,$filename) or die "cant open '$filename': $!"; my $line = <UTOP>; # grab a line from the file close(UTOP); my @fields = split(' ',$line); # split the line into fields print "Value = $fields[0]\n"; # print out the first field =OUTPUT Value = 13888666.52

    and yes, that is my current uptime... 160 days and running.

    -Blake

Re: Using awk within system() or open()
by miyagawa (Chaplain) on Oct 24, 2001 at 12:56 UTC

      I think I'd write it as:

      open(UPTOP, 'cat /proc/uptime | awk "{ print $1 } |"');
      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

        open(UPTOP, 'cat /proc/uptime | awk "{ print $1 } |"');
        Then $1 will be interpolated by shell, won't it? $1 should be AWK variable, neither Perl's one nor shell's one.

        --
        Tatsuhiko Miyagawa
        miyagawa@cpan.org

Re: Using awk within system() or open()
by davorg (Chancellor) on Oct 24, 2001 at 12:55 UTC

    Probably because $1 is undef at this point.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: Using awk within system() or open()
by SkullOne (Acolyte) on Oct 24, 2001 at 14:21 UTC
    Hey thanks guys. All your comments really helped me out!! I've learned not to call external programs if I dont have to, and use internal perl functions, as they seem to be a -lot- more effective in getting things done.
    Again, thanks a lot for your help!