in reply to System and a question of style

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 ...');

Replies are listed 'Best First'.
Re: Re: System and a question of style
by submersible_toaster (Chaplain) on Feb 07, 2003 at 05:42 UTC

    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