how to dump output of a perl script on linux into a log file with file name as timestamp

I have a perl script that I run on linux box. I need to capture the output from the execution of the perl script and timestamp its name. I know the output redirection method but I am looking for a way to do it from the script itself (if possible)


You can redirect output and error streams using Typeglobs or you can do the following.


open(STDOUT, '>',time.".out") or die $!; #the time function gives you the timestamp
open(STDERR, '>',time.".err") or die $!;
print "output"; #goes to <timestamp>.out
warn "error";   #goes to <timestamp>.err

It's easy to redirect the STDOUT to a file. Just do this at the beginning of the script:

open STDOUT, '>', "my_stdout_file.txt";

And this is a function that returns a file name with a timestamp:

sub generate_timestamp_filename {
    my $tstamp = sprintf( "%04d%02d%02d%02d%02d%02d", 
                                $now[0] );

    return "my_prefix_$tstamp.txt";

Here is the quickest way to do this:

 open(STDOUT, ">myfile") or die $!;
 print "Hello world"; #now goes to myfile

 open(STDERR, ">hisfile") or die $!;
 warn "Error here"; #and that's in hisfile

The other alternative is to use select:

open(my $FILE, ">stdout") or die $!;
my $old = select($FILE);

# Done capturing output
select $old;

Need Your Help

Get current user in Django model from form

python django

I'm trying to get the current loggedin user's username as a CharField in my model so I can track all the reviews from one user later on. But I'm not understanding how to do it. I don't understand t...

Finding min in a stack- Won't print out any result

javascript minimum stack

Trying to find the min for this stack; however, whenever I run this in JSFiddle nothing prints out... anyone explain to me why? here's the code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.