0

Hi I need to have perl script's output in both file and STDOUT but I cannot redirect output from shel like ./a.pl > out.log. Is it possible?

Gayane
  • 627
  • 1
  • 11
  • 23
  • Why do you need it going to stdout? Is someone going to watch that, or is it being redirected as well? – matt freake Mar 26 '13 at 13:29
  • @sputnick, the simple answer to this question is [man tee](http://www.openbsd.org/cgi-bin/man.cgi?query=tee). Gayane made use of the unix command line and didn't reference any other OSes. Your link doesn't even mention tee. – Julian Fondren Mar 26 '13 at 13:32
  • @JulianFondren I don't agree, OP seems to said that he don't have control of command line – Gilles Quénot Mar 26 '13 at 13:34
  • @sputnick, just now, yes :-/ But scripts (`#!/bin/sh` , `realscript | tee log`) are a away to "force users" and Gayane already has them invoking a script, so... – Julian Fondren Mar 26 '13 at 13:38

2 Answers2

4

Obviously, you should be using

./a.pl | tee out.log

but it sounds like you will reject that. Next best is probably File::Tee.

use File::Tee qw( tee );
tee(STDOUT, '>', 'out.log');
ikegami
  • 367,544
  • 15
  • 269
  • 518
2

Try this instead:

a.pl | tee out.log

imran
  • 1,560
  • 10
  • 8
  • actually The script is being run by other people, I cannot force the to call it in other way. So I need to do something in script (redirect/save STDOUT or something else) – Gayane Mar 26 '13 at 13:33
  • @Gayane, So you're saying you know better than your users what they want. You should never force your users to do anything. – ikegami Mar 26 '13 at 13:35
  • They want to have the output both in log and in STDOUT. But they don't want to redirect output from shell – Gayane Mar 26 '13 at 13:36
  • Use the File::Tee module ikegami mentioned but be careful about the file you redirect your STDOUT to (always append or maybe create a unique file every time the script is run or a new file per day) – imran Mar 26 '13 at 13:54