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

I have a program(myprog) which is similar to any install program. It will ask users certain questions before installing,questions are like - directory where to install, Do you want to initialize etc and based on user response will install accordingly. using perl I would like to do below things
1) I should be able to run the program, and direct its output to both STDOUT and a file myfile.txt at the same time.
2) Read the user input from STDIN and direct this input to myfile.txt
so myfile.txt should basically look like this
Running myprog....
Install directory? /work/mydirectory
want to initialize? yes
Installation successful....


I have looked at IPC::Run3, IPC::Open3 options , without any success
  • Comment on Recording input and output of a command in file

Replies are listed 'Best First'.
Re: Recording input and output of a command in file
by Skeeve (Parson) on Jan 15, 2009 at 12:57 UTC

    Why so complicated? Why not simply print to STDOUT and directly after that to your file? And after each input, print that input to the file too?

    You could write a sub my_print that does the double printing, so instead of:

    print "Directory to install to: "; print $log "Directory to install to: ";

    you would write

    my_print("Directory to install to: ");

    and my_print would then be something like:

    sub my_print { my($txt)= @_; print $txt; return unless $log; print $log $txt; }

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Recording input and output of a command in file
by weismat (Friar) on Jan 15, 2009 at 11:57 UTC
    I would advise you to use Log4Perl and to split the code from the logging - thus you can keep your code and change your logging wo changing code.
    Disadvantage is that your client's perl distributions might not necessarily have Log4Perl installed.
Re: Recording input and output of a command in file
by hbm (Hermit) on Jan 15, 2009 at 14:00 UTC
    I haven't used either of these, but it sounds like Tee or IO::Tee would allow you to easily write to multiple file handles.