Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

System and a question of style

by D.Millin (Beadle)
on Feb 07, 2003 at 00:49 UTC ( [id://233333]=perlquestion: print w/replies, xml ) Need Help??

D.Millin has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone,

I haven't much experience writting Perl code on Unix, as the company where I work as a Unix administrator only uses the Korn shell, Awk and Sed for everything. For my current project, I have to use Perl :-).As I have never seen or used perl in a system admin role on Unix, I haven't developed my own style. I would value the monks advise on the user of system() and programming style.

In my scripts I need to deport and import volume manger disk groups in order to take a point-in-time copy. Each command must be check to ensure that I get clean copy of the disk group. If I were using the Korn Shell, when I need to check the return code, the code would look something like:

vxdg deport oracle if [ $? ] then echo "Could not deport oracle" exit 1 fi

From digging through the Camel book and perldocs, I read that when executing a command using the system function, $? needs to be shifted by eight bytes to get the proper return code. From searching the net, I have seen this implemented in a variety different way, such as:

system ("vxdg deport oracle"); if ($?){ $rc = $_ >> 8; print "Could not deport oracle"; exit 1; }

What I am unsure about is what is then best/efficient way of implementing this. Is it better to carry out the $rc = $_ >> 8; operation after every system call and then check the return code, or would it be better to encase system within the if statement?, and only do the $rc = $_ >> 8; where I absolutely need to use the proper return code.

Thanks,
Darren

Replies are listed 'Best First'.
Re: System and a question of style
by steves (Curate) on Feb 07, 2003 at 03:31 UTC

    Why don't you write a sub that does everything you want around each system call? That way you can change your mind later; add logging in one place, etc.

    sub run_command { my $command = shift; system($command); if ($?) { my $rc = $? >> 8; die "Failed to run '$command': exit code $rc\n"; } } run_command('vxdg deport oracle'); run_command('something else ...');

      Good idea steves++ , I suppose it depends on one's perl version and personal preference, but being able to call system with list syntax (bypassing the shell hence no jiggery-pokery with escaping things) is IMHO so groovy it hurts doing it the old way!. I'd make a small modification to your subroutine.

      sub run_command { # invoke system(@list) if more than one arg passed. ($#_) ? system(@_) : system($_[0]); if ($?) { my $rc = $? >> 8; die "Failed to run '$command': exit code $rc\n"; } } # Easy arg passing with a list; run_command('blah' , '-v' , '-q'); # Pass as scalar for shell interpretation of metachars run_command('tar cvf /dev/null /tmp/*.sock');

      warning - the args to run_command are untested but no harmful

      I can't believe it's not psellchecked
        Perhaps you should put $! in the die string too.

        Why don't force the list syntax? For example, doing a system(@_,"")?

        --bronto


        The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
        --John M. Dlugosz
Re: System and a question of style
by submersible_toaster (Chaplain) on Feb 07, 2003 at 01:18 UTC

    For starters I hope you meant $? and not $_ >> 8 in your second code snippet. You could wrap that bit shift any way you like such as

    $rc = sub { $? >> 8 }; # one way system(@mysyscall) and die "@mysyscall returned ", &$rc; # or if (system(@mysyscall)) { die "@mysyscall returned ", &$rc };
    But of course TIMTOWTDI!

    Update:BTW: Welcome to the Monastery.
    I can't believe it's not psellchecked
Re: System and a question of style
by bsb (Priest) on Feb 07, 2003 at 06:43 UTC
    If most of the time you are just checking for success and only occasionally need the bit-shifted return code then I'd only do it in those cases.

    The rest of the time just use the normal 'system'.

Re: System and a question of style
by D.Millin (Beadle) on Feb 07, 2003 at 09:59 UTC

    Thanks to everyone for replying to question, you given me plenty of food for thought.

    Thanks again,
    Darren

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found