Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

redirecting output in perl

by sparkel (Acolyte)
on Sep 20, 2004 at 16:52 UTC ( #392416=perlquestion: print w/replies, xml ) Need Help??
sparkel has asked for the wisdom of the Perl Monks concerning the following question:


I am a newbie, so please be kind.

I am trying to redirect an output to a file:

 system ( "echo hello > tmp.log" );

It gives me the error:
Can't spawn "cmd.exe": No such file or directory at c:\test\

Please help.

Replies are listed 'Best First'.
Re: redirecting output in perl
by ikegami (Pope) on Sep 20, 2004 at 17:28 UTC

    Update: I rewrote this post after doing some testing.

    The string is passed to the DOS shell cmd.exe to parse and execute. cmd.exe's directory should be in the PATH. (perl should probably check COMSPEC, but it doesn't.) You need to put cmd.exe in the current directory, or place it's directory in the PATH.

    system("c:\windows\system32\cmd.exe /c echo hello > tmp.log"); doesn't help.

    Changing sh= in doesn't help.

    That means if you're in Win95/98/ME, you're in trouble because cmd.exe is for WinNT/2K/XP. You could recompile perl yourself...

    $tmp_log = `echo hello`;
    might be more along the lines of what you want than
    system("echo hello > tmp.log");,
    but that won't fix your problem.

      Thank you so much for your reply. I am using WinXP.

      In fact, the command:
       system('echo hello');
      works fine...

      But, redirecting a command output to the file does not work.

      Also, is there an easier way I can create a file other than:
      unless ( -e $file ) { open ( FH, " > $file" ); close (FH); }

      Thank you,

        Odd, both
        system("echo hello > tmp.log");
        system("echo hello");
        work for me, as they should.

        As for creating a file, opening it for write-append does the trick:
        { local *FH; open(FH, '>>', $file) or die("...: $!\n"); }

        Perl only uses a shell to execute commands if the command string contains shell metacharacters, like the greater-than symbol used to redirect output. That's why it works with no redirection, but breaks with it.

        The solution, as others have said, is probably to add something to your path that has cmd.exe. Generally a Windows system will have this by default, so you may want to look into why your PATH doesn't have this.

        A quick hack that might help is something like:

        $ENV{PATH} .= ";c:\\windows\\command". ";c:\\windows\\system32". ";c:\\winnt\\system32";
Re: redirecting output in perl
by amt (Monk) on Sep 20, 2004 at 19:01 UTC
    If you want to redirect output from STDOUT, the console output, to a file handle, say a log file as your example suggests, you would have to open a file handle and print to that file handle. For example:
    open( LOG, '>>tmp.log'); #open tmp.log for appending print LOG 'hello'; #print hello to LOG file handle

    Because this is a user level file I/O this can be slow at times, and result in you filing your filesystem if not careful. Another, more professional technique to logging events in a system, would be to use the Sys::Syslog module. Demonstrated below:
    use Sys::Syslog; openlog $0, 'pid', LOG_USER; #prepend progname and pid syslog('warning', "printf formated %s", $string); closelog;
    This will print to /var/log/messages in a unix system, using syslogd. This forks off the logging to another process, which prevents your process from blocking on IO.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://392416]
Approved by hmerrill
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-01-24 04:03 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (255 votes). Check out past polls.