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

Hello,

A quick and easy one - is it possible to tell whether a system call has been executed properly?

I have the following which does not appear to create a directory, nor give the error message:

system ("mkdir($NEW_AGENDA_ROOT,0777)") ||  die(" Failed to create directory $NEW_AGENDA_ROOT.");

Any ideas?

Replies are listed 'Best First'.
Re: System Calls
by broquaint (Abbot) on Jul 07, 2003 at 09:16 UTC
    General convention to indicate whether a program has exited successfully or not is the exit status of the program. If the exit status is equal to 0 then it was successful, otherwise a failure e.g
    system("some_program --and arguments") == 0 or die("Couldn't do something with stuff");
    Although in your case it looks like mkdir would be the better option. See. system for more info on the exit status of programs (which lives in the $? variable).
    HTH

    _________
    broquaint

Re: System Calls
by choocroot (Friar) on Jul 07, 2003 at 09:39 UTC
    Hum ... do you want to call the system command "mkdir" ? or use the Perl function "mkdir()" ? your example seems to mix the call to the mkdir system command with the use of the mkdir function ...

    If you want to call the system command /bin/mkdir with system() :
    system( "mkdir", $NEW_AGENDA_ROOT ) == 0 or die( "Failed to create dir +ectory $NEW_AGENDA_ROOT." );
    (see "perldoc -f system" for explanation on this construction)

    If you want to use the mkdir() function then :
    mkdir($NEW_AGENDA_ROOT, 0777) or die( "Failed to create directory $NEW +_AGENDA_ROOT: $!" );
    (see "perldoc -f mkdir" for the mkdir function help)

    By the way, you should use this mkdir() Perl function. It's the prefered way to make a directory from perl, as it's a builtin function available on all platform, and is much more secure than the use of a system call.

    update: Removed the final "0777" argument on the system() call for the mkdir command as it won't set the access mode but simply create the directory "0777". (If you wants to set the access rights you can use the mkdir "--mode " switch). Thank you grantm for pointing me on this error.
Re: System Calls
by ant9000 (Monk) on Jul 07, 2003 at 09:34 UTC
    The correct answer (ie, check $?) has been already given, so this is just out of curiosity... what was
    system ("mkdir($NEW_AGENDA_ROOT,0777)")
    supposed to do? Inside system you have to put system commands, not Perl functions!