I have a script which calls a subroutine in a module. There is a possibility that the subroutine would give some error message and I would like to capture the standard error and save it on a file.
Below is what I have done so far, but I'm not sure if I'm doing it correctly or is there a better way of doing it?
Below is my test module.
package test;
use strict;
sub Check_File {
my $file = `ls -l /tmp/hello.txt > /tmp/result.txt`;
}
1
And below is the script that would print the standard error when I call the Check_File subroutine from the test module. So, if the "/tmp/hello.txt" does not exists, then it will give a standard error output. This is what I want to capture and save in a file.
#!/usr/bin/perl
use strict;
use test;
print "Start of print.\n";
open( STDERR, ">", "/tmp/error.txt" )
or die "Error: Cannot open file - $!";
test->Check_File();
close( STDERR );
print "End of print.\n";
Any comments are appreciated. Thanks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.