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

Redirecting STDERR output to file in the cpan shell

by lancer (Beadle)
on Dec 02, 2010 at 10:54 UTC ( #874866=perlquestion: print w/replies, xml ) Need Help??
lancer has asked for the wisdom of the Perl Monks concerning the following question:

Hello All!

I'm trying to install a module using the Perl CPAN shell, in a Windows environment. The module install fails with a very long list of errors that scrolls off the top of the "cmd" console. I want to redirect it to a file to be able to read it all.

I've read up on the CPAN documentation and found that output redirection (and even piping) is possible, using syntax such as this:

cpan> install Cairo > install_log.txt

But unfortunately it redirects only STDOUT. The errors still go to the console, and don't get captured in the file.

I want to be able to redirect both STDOUT and STDERR into a file, is this possible?

Replies are listed 'Best First'.
Re: Redirecting STDERR output to file in the cpan shell
by chrestomanci (Priest) on Dec 02, 2010 at 11:04 UTC

    A simpler solution would be to increase the number of lines you windows console remembers. If you click on the icon on the top left corner of the console you can change the preferences for the window, including increasing the number of lines recorded, and the width of the window.

    It has been a while since I coded under windows, but as I recall, the default was 300 lines, and I always changed it to at least 3_000, sometimes more.

    While you are in that menu, I would enable quick edit mode, as it makes it a lot easer to cut and paste stuff. (Such as pasting your error message into another post here). (Make sure you use <code> tags).

      Thank you for the excellent suggestion, chrestomanci.

      I have increased the number of lines for the console and the size of the window. Now I can see all the errors.

      And also thanks for the quick edit suggestion, it was really helpful too.

      Now I only have to figure out what causes the install to fail...

      Have a nice day! :)

Re: Redirecting STDERR output to file in the cpan shell
by cdarke (Prior) on Dec 02, 2010 at 12:21 UTC
    cmd.exe, and the cpan shell, on Windows is strangely like most UNIX shells (not csh) in this syntax:
    cpan install fred >install_log.txt 2>&1
    Unfortunately you then cannot see any output, because it is all going to a file! So, it might be better to pipe output (and stderr) to a pipe:
    cpan install fred 2>&1|more
    Again, this is the same as most UNIX shells, except csh.

      Thank you cdarke, this is even closer to what I originally intended to do.

      I didn't know the "cpan" tool could be invoked by itself from the command line. It does work however, and from there it's possible to redirect STDERR and STDOUT to a file with the cmd shell, exactly as you specified in your example.

      Now I got the output into a file, and it's over 30Kb's long.

      Piping into the "more" utility also works, but it's a lot less useful than "less" on Linux. But still, it's good to know that this technique exists.

      Have a nice day! :)

        It sometimes helps to pipe to "tee" (see linux command, man tee) the output so that it still appears on your console, but you can capture it. This way if there's any interractive input required by the script you can see the prompts. The 2>&1 directive tells the shell to send STDERR to the same place as STDOUT, that's for the bash shell. For C shell the equivalent redirective would be  >& or for pipe |& .

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://874866]
Approved by marto
Front-paged by salva
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-07-25 22:08 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (382 votes). Check out past polls.