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

Hi guys, I am trying to make my perl program execute simple system commands, but for some reason, it isn't working. I've tried: print `cd desktop`; print `mkdir robert`; and still no folder appears. Any painfully easy answer for my problem?

Replies are listed 'Best First'.
Re: System Commands
by duff (Parson) on Nov 05, 2005 at 02:02 UTC

    FYI, `cd some_dir` will never work. cd is a shell built-in because it has to be. Just think what happens: perl spawns a shell, the shell changes directory in it's local environment then the shell dies leaving the parent process (your perl program) in the same directory it started in.

    This has nothing to do with perl. This is just a consequence of how processes work.

Re: System Commands
by blue_cowdawg (Monsignor) on Nov 05, 2005 at 03:31 UTC
        I've tried: print `cd desktop`; print `mkdir robert`;

    OK: why are you trying to print values from a shell command that isn't going to write anything to STDOUT/STDERR?

    As belg4mit pointed out what you really want to use are the built-in functions. Examples:

    : : : mkdir $some_dir; chdir $some_dir; : :

    Now, what I've shown above is a very basic way of using those built-ins. You can also trap some errors from these built-ins as well. For instance:

    | | chdir $some_dir or die "HEY! I couldn't CD into $some_dir:$!"; | | mkdir $another_dir or die "OHMIGOD! I couldn't make $another_dir:$!"; | |

    Now, it must be noted that under *nix the value of umask is going to influence the behavior of mkdir and what the resultant permissions of the created directory are. But that, kind monk I leave as an excersise in your own ability to learn more about.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: System Commands
by belg4mit (Prior) on Nov 05, 2005 at 01:44 UTC
    See perlfunc and use the built-ins (chdir and mkdir).

    --
    In Bob We Trust, All Others Bring Data.

Re: System Commands
by sh1tn (Priest) on Nov 05, 2005 at 11:45 UTC
Re: System Commands
by Ad Aspera (Acolyte) on Nov 05, 2005 at 03:08 UTC
    Try system ('mkdir c:\Fish');

      Perl can mkdir quite easily without the need to shell out. A quick perldoc -f mkdir should show you the ropes. Or, use File::Path for full coverage.

      -- vek --