Question

I am trying to understand Moose::Exporter, but no matter what I try, the example almost as exactly from the manual is not working.

package HasRw;
use Moose;

use Moose::Exporter;

Moose::Exporter->setup_import_methods(
        with_meta => ['has_rw'],
        also=>'Moose');

sub has_rw {
      my ( $meta, $name, %options ) = @_;
      $meta->add_attribute(
          $name,
          is => 'rw',
          %options,
      );
}

1;

package Another;
use Moose;
has_rw 'foo';


package main;

my $ww = new Another(foo=>"bar");

This is in the file example.pl; when I try to run it with perl, I got this error message

String found where operator expected at example.pl line 23, near "has_rw 'foo'"

(Do you need to predeclare has_rw?) syntax error at example.pl line 23, near "has_rw 'foo'"

Execution of example.pl aborted due to compilation errors.

What am I doing wrong?

Was it helpful?

Solution

Another never even attempts to import has_rw from HasRw.

If you did, would have have to do so before the call to has_rw is compiled, so it would have to be done at compile time. Don't forget that Moose::Exporter->setup_import_methods would have to be executed even before that!

Using use did all this for you. The inline equivalent of use HasRw; is

BEGIN {
   package HasRw;
   ...
   $INC{'HasRw.pm'} = 1;
}

use HasRw;

OTHER TIPS

Moving HasRw to another file HasRw.pm and specifically importing it using use HasRw; seemed to fix the issue.

So that's it, I guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top