in reply to Handling returns from mkdir.

Why not use mkdir directly?

Or even -d (and -l) if you really need to check for existence yourself, instead of simply trying to create the directory and judging success by its results.

Replies are listed 'Best First'.
Re^2: Handling returns from mkdir.
by afoken (Chancellor) on Dec 24, 2010 at 11:46 UTC

    Good point. But remember that TOCTOU might be a problem. mkdir $directory, check $!, and ignore EEXIST ("File exists"). After that, $directory may still be something non-directory, but you will know that when chdir $directory or open(...,"$directory/filename") fails with ENOTDIR ("Not a directory").

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Handling returns from mkdir.
by dannyd (Sexton) on Dec 24, 2010 at 10:07 UTC

    Thanks for the help (Im trying to keep this code same for linux and windows, so im scared to use any options)

    my $err = `mkdir $FolderName 2>&1`; chomp ($err); if ($? != 0) { if ($? == 256) { print "Warning : $err"; } else { print "Error : $err"; } }

    That part of the code now works like a charm :)

      Im trying to keep this code same for linux and windows, so im scared to use any options

      One more reason NOT to use an external mkdir. Perl's builtin function works the same everywhere. "2>&1" doesn't work on all Windows versions, so you can't use it according to your rules. $FolderName is not quoted properly, this will bite you soon. Remember that you need to quote differently on Windows and Linux, because you have different shells. Don't think that quoting rules are the same for all shells that you might see on Linux. bash is the most common shell on Linux, but it's not the only one.

      Short: Use the buildin mkdir function. It is faster, cleaner, and opens no security holes.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)