Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

RE: RE: onlyone

by BoredByPolitics (Scribe)
on Oct 31, 2000 at 14:52 UTC ( #39272=note: print w/replies, xml ) Need Help??


in reply to RE: onlyone
in thread onlyone

Thanks for taking the time to reply :)

  • open (FROMKID, "-|") or exec ("who", "-m") or system_log ("Can't run who: $!");

    You probably want to die here, otherwise both processes (open here does an implicit fork) would continue to run in the event your exec fails.

    Ah, so the die in the open ... or exec ... or die construct actually applies to the child, not the entire script? I was trying to make sure that any runtime errors get logged to the system logger, so I can diagnose problems after the event, hence the exit 1 on the following line. Is there a better way of trying to achive this?

  • Regular expressions like /tty(.*?)\s/ are probably a little more efficient written like /tty(\S+)/.

    I'm slowly starting to get to grips with regexp, however I do find it confusing when perl, tcl, and sed seem to do things slightly differently!

  • $killresult = kill 15, $1;

    Your code will be a bit more readable if you were to use, say, 'TERM' instead of 15.

    That would be better, I hadn't realised I could do that - is that a product of the English module, or built in?

  • ... or system_log ("Can't run ps: $!");

    While I don't see any immediate problem, since $! probably won't be touched by the user, you don't generally want to call syslog with only a single (well, this second) argument, because it leaves you open to format-based attacks.

    Thanks for the heads up - I shall adjust my calls to the system_log subroutine accordingly.

  • my $hit_enter = <STDIN>;

    If you're just discarding the input anyway, just call <STDIN> in a void context:

    <STDIN>;

    I keep getting confused over when I can do that, and when I can't - guess it's also because my other main programming language is a varient on Basic!

  • I'm curious what you're using the English module for..? You don't seem to be using the "long" versions of any system variables.

    Ah, that's left over from when I was creating a load of code to make the child safe in open's, but then changed my mind and decided to make the entire script safe instead.

    Kudos for using strict, but I'd be most impressed if your script ran warning-free with the -wT options (even if you don't need to use them in your end product).

    I must get into the habit of doing that!

Hope this helps!

Immensely, thanks very much :)

Replies are listed 'Best First'.
RE: RE: RE: onlyone
by Fastolfe (Vicar) on Oct 31, 2000 at 20:32 UTC
    I was trying to make sure that any runtime errors get logged to the system logger, so I can diagnose problems after the event, hence the exit 1 on the following line. Is there a better way of trying to achive this?

    I might drop all of the 'or's and write it like this:

    my $pid = open(FROMKID, "-|"); # There are now two processes running, one with # $pid set to the PID of the child, and the other # with $pid set to 0 (or undefined upon an error). unless ($pid) { if (!defined($pid)) { system_logger("Unable to fork: $!"); die "fork: $!"; # or just exit as you were } # note that in this test, $pid is undefined, # which means we're the child thread, so no # matter what happens, we must exec or exit/die unless (exec(...)) { system_logger("Cannot exec ...: $!"); die "exec ...: $!"; # or just exit } }
    is (the use of 'TERM' in kill) a product of the English module, or built in?

    It's built-in.

    Hope this helps.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-01-25 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?