Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: ppk

by graff (Chancellor)
on Apr 18, 2005 at 06:24 UTC ( [id://448745]=note: print w/replies, xml ) Need Help??


in reply to ppk

I have couple things to add to what merlyn said, regarding this comment in your description:
this script is for use exclusively under Unix, or Unix like, operating systems (if the system command ps -eaf could be rewritten in pure Perl, then the OS restriction could be lifted)

Unfortunately, one of the issues that differ significantly across versions of unix and unix-like operating systems is the behavior of "ps". In particular, the options you use in this script will work on any "SysV" style of unix (e.g. Solaris), and might work on linux (if you're using linux, you know better than me), but it won't work on any BSD style (e.g. (free|open|net)bsd, or the bsd-based macosx), because the latter uses a completely different set of option flags.

It turns out there are a couple ways around this:

  • Have some logic that looks at $^O and sets option flags for "ps" accordingly (update: unless you do this very carefully, you may still have problems because the output formats will be very different for BSD vs. SysV), or
  • use Unix::Process, which allows you to get just the information you want from "/bin/ps", by way of the abbreviations assigned to the different possible output fields ("pid", "ppid", "vsz", etc). It still runs /bin/ps to do its work, and the different versions of "ps" are not 100% consistent in the field names (e.g. Solaris "ps" uses "comm" and "args" where BSD uses just "command"), but several important names are common to both versions -- whereas the command-line options are not. So you still might need to do different stuff depending on the value of $^O, but some things are more likely to be the same for all unixen.

Replies are listed 'Best First'.
Re^2: ppk
by northwind (Hermit) on Apr 18, 2005 at 13:45 UTC

    This post is actually in reply to both previous comments (merlyn's and graff's).

    There is nothing quite like posting your code to an open forum to make you write better code...  :)    Thank you for your comments.

    The o in m//o was something I pulled from Programming Perl, 3rd edition.  It is supposed to give a hint to the regexp compiler that the regexp only needs to be compiled once.  I suppose the same thing could be achieved with qr//.  Also, good catch on blindly using $1.

    I can see how, in a general purpose setting, -9ing a process would be a Very Bad Thing.  This is the first thing on the bug list.  As for systeming out for kill instead of using the built-in, does the built-in allow you access to the error codes?  Also, because the code was already system dependent (ps -eaf), I figured why not add some more (sort of like using $& and friends in a regexp).  The world in which I work/play/code consists of Solaris, Irix, and Fedora Core 2.  So within my admittedly small world, the code works.

    I will be updating the above code over the next day or two (as time permits) to fix your observations of what is wrong.  BTW, does anyone know where there is a list of all the strings $^O could contain?

      the regexp only needs to be compiled once
      You gotta read the rest of the text right around there. I'd quote it, but I don't have my camel with me (I'm on a trip). But the perlop manpage says:
      PATTERN may contain variables, which will be interpolat +ed (and the pattern recompiled) every time the pattern search i +s evalu- ated, except for when the delimiter is a single quote. + (Note that $(, $), and $| are not interpolated because they l +ook like end-of-string tests.) If you want such a pattern to be + com- piled only once, add a "/o" after the trailing delimite +r. This avoids expensive run-time recompilations, and is useful + when the value you are interpolating won't change over the l +ife of the script. However, mentioning "/o" constitutes a pro +mise that you won't change the variables in the pattern. If + you change them, Perl won't even notice. See also "qr/STRING/imosx".
      Thus, /o is useful only when there are variables in the pattern. And you had no patterns with variables!

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Doh!

        I just looked /o up in my camel book.  For future reference, the text in question is on pg. 148.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found