in reply to Re4: Parsing 'uptime' output
in thread Parsing 'uptime' output

Perhaps my one line link could have used a bit more explanation, especially for those who weren't around for the original discussion....

Two argument open() is badly designed because the second argument actually represents two vastly different types of data (MODE and FILENAME). Getting these two pieces of data from your head to the CPU involves gluing them together and then parsing them apart again. This co-mingled intermediate form is what tilly, Dominus, and Dan Bernstein are warning against.

In the uptime case, the intermediate form is the english representation of a span of time. If you want your computer to tell you how many seconds its been up, asking it to stringify that information into an english phrase for you to reparse seems like the wrong path to me.

Given the number of Updates: in this thread, parsing the english is actually somewhat tricky. Why go to all that trouble if your machine can just hand you the exact piece of data you're interested in?

-Blake

Replies are listed 'Best First'.
Re: Re5: Parsing 'uptime' output
by sauoq (Abbot) on Aug 19, 2002 at 17:08 UTC

    Thanks for the explanation. I agree with you but I still think the application of the principle in this case isn't merited.

    If you want your computer to tell you how many seconds its been up...

    Parsing it into seconds wasn't requested. I agree that it might be what was wanted. People often don't ask for what they really want. It is just as likely, however, that he wanted to display the English form on a web page or some such. In any case he wanted to collect the time, the number of users, and the load averages as well.

    The fact remains that using uptime to retrieve these pieces of information provides a relatively stable and convenient interface and is able to be parsed by the same regex on at least the several platforms that I tested and probably many more. It might be a little messy (like a lot of the world) but Perl excels at dealing with messiness.

    In defense of my Updates, all I can say is I got it right the first time but I retyped it wrong. Twice. I would have cut and pasted but I was using the console on a different machine. There really was no excuse for such sloppiness and I've put a link to the node on my home node as a reminder to myself to take my time and get it right. ;-)

    -sauoq
    "My two cents aren't worth a dime.";