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

Hi, I am an absolute beginner at perl and currently going through the tutorials and these forums. I have a fairly simple task and was wondering if I could get some help on this forum. basically, we have a startup script for a particular software package that is in perl and we need to modify it to follow the following algorithm to get the processor id to bind to: On statrup execute shell script to get processor id to bind to If shell script returns null or non-numeric value, log and error to an error log and continue with alternate method that already exists to get processor id else call bind_cpu with the processor id returned what would a sample perl snippet to accomplish this task look like. thanks for the help.

Replies are listed 'Best First'.
Re: executing a shell script from perl
by Bloodnok (Vicar) on Jan 08, 2009 at 20:04 UTC
    Writing the algorithm/requirement as an HTML ordered list would help readability enormously.

    Having said that, your use of startup script is somewhat ambiguous - do you mean a script to start an application or a script thats runs when the system starts ?

    I suspect the answer you seek lies in the use of backticks ...

    . . my $out = `shell_script`; if (! $out || $out !~ /\d/) { log_error; $out = `alternate_method`; } `bind_cpu $out`; . .
    Have a look at perlfunc, perlop

    A user level that continues to overstate my experience :-))
      It's a bit unusual to have backticks in void context. Backticks are used to collect the output of the program - running backticks in void context kind of defeats that purpose. About the only reason you want to do this is that you know the program is going to generate output, and you don't want to see that output at all. Still, in such a case, I'd prefer:
      system "bind_cpu $out > /dev/null";
        Point taken JavaFan - can't disagree with any of that - I was obviously in too much of a hurry to give it [my response] anything like a _decent_ coating of thinking about ... in which case I'd've done something similar.

        Having said that, since system returns nothing except a return code c/w a signal number, > /dev/null is self-evidently superfluous.

        Update:

        In the light of the update from brother JavaFan, I refer the reader to my last response - which is self-prophesying since I self-evidently give that no thought either - my comment was true from the POV of view of returning output back to the script, but manifestly wrong from the user POV.

        A user level that continues to overstate my experience :-))
        system "bind_cpu $out &> /dev/null";

        That hides all output.
      yeah, when I formatted the question , the algorithm was looking ok. when I hit submit, it all became single line. let me try this and see if it works
        does the above logic also check f0or return code from the execution of the shell script?