I am trying to define a file handle attribute in my Perl code using moose as follows:
has validation_log_file_handler => (
is => 'ro', isa => 'File', builder => '_build_validation_log_file_handler'
);
The builder:
sub _build_validation_log_file_handler {
my ($self) = @_;
open(my $fh, ">", $self->validation_log_file)
or die ("ERROR:Can't open file "
. $self->validation_log_file
. " for writing");
return $fh;
}
But when trying to write to a file:
sub run {
my ($self) = @_;
print $self->validation_log_file_handler "Hello\n";
.
.
.
}
I new at Moose. Am I doing something wrong? I get the following compilation error:
syntax error. String found where operator expected
Printing to complex filehandles requires curlies:
print { $self->validation_log_file_handler } "Hello\n";
or you could use the OO notation
use IO::Handle; # Required in older versions of Perl
$self->validation_log_file_handler->print("Hello\n");
Did you define a File
class? If not, use IO::Handle
as the isa
.