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

Hi,
I want to use shell commands on win32. I can't seem to run cp() (bash) or copy() (dos). I'm new to this and seems like a very simple problem, but I'm stuck!
Thx Andy

Replies are listed 'Best First'.
Re: Shell commands on Win32
by davis (Vicar) on Feb 20, 2003 at 15:19 UTC

    It sounds like you're trying to run the cp or copy commands from the perl script directly, much as you would do in shell script. You can't really do that - if you want to run a system command (such as cp), you'd need to invoke perl's system() function - e.g.:

    system("cp file1 file2");

    If you want to copy files in an operating system-independent way (and you probably do), then I'd like to direct you to the File::Copy module, which comes with Perl as standard. Use it thusly: (ripped verbatim out the POD)

    use File::Copy; copy("file1","file2"); copy("Copy.pm",\*STDOUT);' move("/dev1/fileA","/dev2/fileB");
    Hope this helps
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
    Update: Minor typo fix
Re: Shell commands on Win32
by JamesNC (Chaplain) on Feb 20, 2003 at 16:51 UTC
    Try using backticks` ` until you read more about what Perl built-in's and modules can do for you. You can capture the response into a scalar or an array and then process it using the power of Perl's regular expressions. You can also use the system command if you don't want to process any return stuff from Win32 shell but just want to execute it.
    use strict; use warnings; my @shell_rtn_val; print "Enter name of file to copy: "; my $file = <STDIN>; # open STDIN and get input chomp $file; # remove the \n $file =~s /\\/\\\\/g; # Convert to Win32 Shell path sep $file =~s /\//\\\\/g; # Use Unix path's too :-) @shell_rtn_val = `copy $file $file."bak"`; foreach(@shell_rtn_val){ #Let's look for interesting stuff and process it # $_ is implicitly searched for in each if # just like you wrote: $_ =~ m/regex-pattern/ if ( /(\d) file\(s\) copied/ ){ my $num = $1;print "You copied $num files"; } if ( /cannot find/ ){ print "System can't find a file by that name"; } if ( /syntax/ ){ print "Bad syntax"; `copy /?`; } }
    There are modules which can make system interaction trivial...like File::Find for instance, or if you want to capture key strokes in Win32 you will need Term::ReadKey. The list is really endless, and the guys in here are smart as all get out.. they only ask that you do some homework and show some code before you ask a question! :) Cheers, James
Re: Shell commands on Win32
by CountZero (Bishop) on Feb 20, 2003 at 15:12 UTC

    Perhaps if you gave an example of what you tried (but didn't work) we could help you.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Shell commands on Win32
by andyram27 (Initiate) on Feb 20, 2003 at 15:22 UTC
    copy() does work on Win32. I just misspelled copy which is why the interpreter returned with unrecognized function errors.