2

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
Mike
  • 1,007
  • 2
  • 16
  • 33

1 Answers1

3

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Handles aren't `FileHandle` objects; they are IO::Handle or IO::File objects. Not sure how well it works to actually use that as `isa`, though. – ikegami Aug 12 '14 at 12:23