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

Hello all, I want to display the output of perl Script on console as well as i want to save it on text file also. i got the code which is redirecting output to the text but it wouldn't shown on console as well. following is the code.

open OLDOUT,">&", STDOUT; close STDOUT; open(STDOUT,">index.txt"); print "hello how are u??\n"; close(STDOUT); open (STDOUT, ">&",OLDOUT);
what else i had to do to get the output on both textfil as well as on console??? plz help.....

Update

I am working on Windows..

Replies are listed 'Best First'.
Re: Perl output on console as well as on text file
by Athanasius (Archbishop) on Jan 05, 2015 at 07:03 UTC
Re: Perl output on console as well as on text file
by choroba (Cardinal) on Jan 05, 2015 at 10:32 UTC
    If you are on a *nix, you can use tee:
    my-script.pl | tee output.txt
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl output on console as well as on text file
by karlgoethebier (Abbot) on Jan 05, 2015 at 11:49 UTC

    Powershell and tee?

    Update:

    "... i don't want to install any other module..."

    So Powershell comes for the rescue.

    Windows 7 or Server 2008 R2 comes with Powershell (as fare as i know). Else (Windows Vista, XP, Server 2003) you need to install it. N.B.: i'm no expert about current M$-Dog versions.

    The missing example:

    PS C:\> shyamason.pl | tee-object -filepath C:\path\to\shyamasoni-out.txt

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Perl output on console as well as on text file
by Laurent_R (Canon) on Jan 05, 2015 at 08:00 UTC
    This may seem to be obvious trivial, but you could simply wrap all your printing in a function that prints twice, once to SDTOUT and once to a file.

      I don't have only single print statment. i have big perl script in which i am using so many perl commands as well as no. of print statements. i want whole output to be printed on console as well as on text file.also i don't want to install any other module fom CPAN. is still any way to get out of this problm

        I don't have only single print statment. i have big perl script in which i am using so many perl commands as well as no. of print statements. i want whole output to be printed on console as well as on text file.also i don't want to install any other module fom CPAN. is still any way to get out of this problm

        Its called a subroutine, instead of

        print $fh $str; ... print $fh $str; ... print $fh $str;
        you write
        DoublePrint( $fh, $fh2, $str ); ... DoublePrint( $fh, $fh2, $str ); ... DoublePrint( $fh, $fh2, $str );
        where DoublePrint prints to STDOUT and something else
Re: Perl output on console as well as on text file
by BillKSmith (Monsignor) on Jan 05, 2015 at 21:03 UTC
    This is exactly what the module IO::Tee is for.
    Bill

      Thanku karl but following error is occuring 'tee-object' is not recognized as an internal or external command, operable program or batch file.

        "... 'tee-object' is not recognized..."

        You mean the tee-object cmdlet doesn't work? This works for me:

        PS C:\Dokumente und Einstellungen\karl\Desktop\monks> perl -e 'print q +q(Hello world\n)' | tee-object -filepath shyamasoni-out.txt Hello world PS C:\Dokumente und Einstellungen\karl\Desktop\monks> cat shyamasoni-o +ut.txt Hello world

        Could you copy & paste the exact error you get from PS (activate Quick-Edit mode, mark region in PS with mouse, press ENTER and then CRTL-V...)?

        Update: See also Set-ExecutionPolicy Cmdlet.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»