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

Okay ... I'm having trouble moving one of my perl scripts from linux to windows. this is what i had in linux, that worked correctly:
$dir_name = "/home/mbond/temp"; mkdir($dir_name);

That does not work in windows though ... I can't get mkdir to work at all, so i desided to try a system call like this:
$dir_name = "mkdir c:/testdir"; system($dir_name);

again this did not work, however this did:
system("mkdir c:/testdir");

Any ideas? I need ot be able to pass it a variable directory name because it will be changing each time the script is run.

Mbond.

Replies are listed 'Best First'.
Re: creating directories
by Fastolfe (Vicar) on Jan 30, 2001 at 00:04 UTC
    This doesn't make any sense:
    $dir_name = "mkdir c:/testdir"; system($dir_name); # versus system("mkdir c:/testdir");
    These should behave exactly the same way. Since that does not appear to be the case, I'm wondering if there's something else going on that you aren't telling us. Variable interpolation? Trailing newlines?

    mkdir does not require a second argument (at least in 5.6; I don't know about earlier versions). As the documentation under 5.6 says, if omitted, it defaults to 0777. The documentation on the site seems to imply that it is required, so perhaps that's your entire problem if you're using an earlier version of Perl.

    Otherwise, are you paraphrasing code here, or is this the actual live code you are using? Something else must be going on that we aren't seeing.

Re: creating directories
by lemming (Priest) on Jan 29, 2001 at 23:56 UTC
    Using the File modules helps in this. Otherwise you have to redo your own work. File::Path would be specific for your current needs. And since File is installed along with the base installation, you should be set.
    Some of your problems have to do with constructing the directory path deep enough. The other has to do with "\" and "/" system differences.

    Update:I'm also looking at your code and I agree with Fastolfe, those two should be functionally equivalent, but since you use a "/" it should say that you have an invalid argument. All bets are off if the mkdir is actually the cygnus version. One problem with system calls, they might different programs on different machines based on the search path they inherit. Go with the File modules, you'll be happier in most cases.

Re: creating directories
by chromatic (Archbishop) on Jan 29, 2001 at 23:58 UTC
    I believe mkdir in Perl 5.5.3 and earlier requires a second parameter, the mode of the directory to create. (perlfunc -f umask recommends 0777). That's worth a try.

    Otherwise, you could use the list form of system for your mkdir call:

    my $dir = 'c:/testdir'; system('mkdir', $dir);
    I'm not a Windows hacker, so this is untested. Perhaps you could post any error messages when run under -w and strict.
Re: creating directories
by runrig (Abbot) on Jan 29, 2001 at 23:56 UTC
    Try File::Path if you are creating more than one directory level at a time.

    Update:My mistake. You are obviously NOT creating more than one level, but you might also want to check out File::Spec.

    Another update: What's the $! error message from, e.g.:
    mkdir($dir) or die "Can't create $dir: $!";
Re: creating directories
by stefan k (Curate) on Jan 30, 2001 at 14:23 UTC
    Hi,
    you should consider using File::Spec->catfile for the generation of filenames when you want to write portable code, I think. It takes care of system properties like the path-separator "/" (real) or "\" (fake ;-)
    Regards
    Stefan K
    $dom = "skamphausen.de"; ## May The Open Source Be With You! $Mail = "mail@$dom; $Url = "http://www.$dom";