Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Porting Commands to Windows

by Elegant (Novice)
on Sep 14, 2014 at 08:02 UTC ( [id://1100522]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to port a few things to Windows and I'm fairly certain that how I'm translating them is incorrect despite not receiving any errors.
$self->sys('/bin/mkdir', '-p', "$self->{tmp}/attach"); $self->sys('cmd /C mkdir', '-p', "$self->{tmp}/attach");

The first line is the original (works under linux) and the second line is what I thought was correct under Windows. However, I've never see the results of any of my commands. Is my syntax correct? I have many other cases that involve ln, rm, rm -rf, mv, which are all used in the same manner.

In case you have questions about what sys() is, it's more or less system().

Replies are listed 'Best First'.
Re: Porting Commands to Windows
by LanX (Saint) on Sep 14, 2014 at 08:15 UTC
    Are you aware that Perl has it's own mkdir ? :)

    The portable way to use system calls is to try to avoid them. ;)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    Update

    A quick search revealed that mkdir on win is considerably different to mkdir on unix, especially when handling options like -p .

    But sorry this here is supposed to be a Perl and not an OS forum...

      And for handling nesting, etc, you can use File::Path which is distributed with Perl core.

      Steve
      --
Re: Porting Commands to Windows
by toolic (Bishop) on Sep 14, 2014 at 11:35 UTC
Re: Porting Commands to Windows
by jonadab (Parson) on Sep 15, 2014 at 12:49 UTC

    In general, Windows is sufficiently different from Unix that no single simple rule will cover all of the changes you're going to have to make if you're using system(). If you can find ways to do things in pure Perl, without relying on external commands, that will be much more portable. As others have pointed out, mkdir exists as a Perl builtin. Other things can be done with modules (although getting CPAN working on Windows can be non-trivial, so you may want to constrain yourself to modules that ship with a particular Perl distribution for Windows).

Re: Porting Commands to Windows
by stylechief (Sexton) on Sep 15, 2014 at 18:37 UTC
    You mention that you never see the results of your commands. If the module you are using does not have a method to help with this, e.g.
    my $error = $self->message();
    or something similar. You could, for development and testing purposes, use something like
    $result = qx(mkdir $path);
    To view results.

    eval would also provide information if there are problems:
    eval{$self->sys('cmd /C mkdir', '-p', "$self->{tmp}/attach")}; if ($@){ print "$@\n"; }
    SC
Re: Porting Commands to Windows
by sam_bakki (Pilgrim) on Sep 19, 2014 at 08:50 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100522]
Approved by GrandFather
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found