'Use'ing Moose-based packages in Perl Scripts

230 Views Asked by At

I have been scratching my head for about an hour for trying to get a .pm to work as a module for me.

My issue is quite simple.

The story:

  1. I have made a package and used Moose for OOP.
  2. I have saved the package, My::FileIO in the home directory under a common dir for all such custom modules in the form /home/USER/DIRECTORY/My/FileIO.pm. In fact, this was done after I used Module::Starter and used perl Makefile.PL which installed it in that format.
  3. I used an external script to test the module. In that, I used use lib '/home/USER/DIRECTORY/' and then confirmed the presence of the module by checking %INC which yielded the correct filename value. This led me to conclude that perl had loaded the module without any complaints.
  4. I tried to use this module by My::FileIO and the testing script had not complained at that time as well.
  5. The testing script failed after I used the constructor in the Moose way, my $test = My::FileIO->new() and this is what it threw - Can't locate object method "new" via package "My::FileIO" (perhaps you forgot to load "My::FileIO"?) at line 1.
  6. I tried to make a mock subroutine named init and it failed too.
  7. The author of Perl Maven however succeeds using these codes (https://perlmaven.com/object-oriented-perl-using-moose)

His testing script

use strict;
use warnings;
use v5.10;

use Person;

my $teacher = Person->new( name => 'Joe' );
say $teacher->name;

His test module

package Person;
use Moose;

has 'name' => (is => 'rw');

1;

Summary:

  1. The testing script is able to load the module successfuly.
  2. I am not able to use the package at all.

This is the assumed FileIO.pm

package My::FileIO; 
use feature 'state'; 
use List::Util qw(max); 
use Data::Dumper; 
use Moose;
use Type::Params qw(compile);
use Type::Utils; 
use Types::Standard qw(Str FileHandle Int HashRef ArrayRef Maybe);

[ ALL THE CODE ]

1;

Full code is at https://pastebin.com/1kxiPazd

This is my testing file - test.pl

use lib '/home/USER/DIRECTORY';
use My::FileIO; 

$test = My::FileIO->new() # Fails 

What am I doing wrong? I made a mock module like Perl Maven's and tried testing it and it failed too.

Update - I apologize for the silly errors in my writeup.

1

There are 1 best solutions below

4
ikegami On

What you posted at the bottom will work assuming the full path of the file you called FileIO.pm is /home/USER/DIRECTORY/My/FileIO.pm.

The code you posted at the bottom is clearly not what you used in your test that gave error. The error message you provided said you used -e rather than using test.pl, and it said you tried to create an object of class My::Test rather than My::FileIO.