Mighty Monks,
I've been reading through "Network Programming with Perl" and, while working through some of the examples, I ran into a problem using IO::File to redirect standard output (STDOUT).
I thought the following code would print "Here's some data\n" to 'test.log':
1 #! /usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 use IO::File; 7 8 my $logfile = 'test.log'; 9 10 STDOUT->open($logfile, '>') or die "Can't re-direct STDOUT:$!"; 11 12 print "Here's some data...\n";
but instead, I get the following error:
Can't locate object method "open" via package "IO::Handle" (perhaps yo +u forgot to load "IO::Handle"?) at ./redirect_stdout line 11. shell returned 255
But the following works on my created filehandle:
1 #! /usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 my $logfile = 'test.log'; 7 my $FH = new IO::File; 8 9 $FH->open($logfile, '>') or die "Can't open $logfile for writing:$!" +; 10 11 $FH->print("Here's some data...\n");
What's going on here?
njcodewarrior
In reply to Using IO::File to redirect STDOUT by njcodewarrior
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |