I'm trying to learn to develop Perl modules in an effort to have clean 
directory structure and more reusable code.

I'm hitting a little snag in my test code that I don't quite understand. I'm 
hoping someone can help explain where I may be going wrong here.

The error I'm receiving is this:

ln09{}: perl uses_Mod.pl
Can't locate object method "new" via package "myMod" (perhaps you forgot to 
load "myMod"?) at uses_Mod.pl line 11.

I believe that I've done everything I should do to "load" the module, and can't 
make it work properly. 

The lines of code which "load" the module in my caller are these:
use lib 'Mod';
use Mod::myMod;

The line of code where I get the error is this:
my $obj = myMod->new();

My best guess is that my directory structure is wrong, and the caller simply 
can't find the module. Here are the details of that:

ln09{}: ls -l
total 8
drwxr-xr-x 2 travis engr 4096 Jul  9 21:10 Mod
-rwxr-xr-x 1 travis engr  122 Jul  9 21:05 uses_Mod.pl

ln09{}: ls -l Mod
total 4
-rw-r--r-- 1 travis engr 456 Jul  9 21:10 myMod.pm

Can someone take a look at my files below and comment on anything obvious that 
I'm not seeing?

Here are the files:

[File: ./uses_Mod.pl]
#!/usr/bin/perl

use strict;
use warnings;

use lib 'Mod';

use Mod::myMod;


my $obj = myMod->new(); # <----- THIS IS THE LINE THAT ERRORS

$obj->myModFunc();
[end uses_Mod.pl]


[File: ./Mod/myMod.pm]
package Mod::myMod;

use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(new myModFunc);

sub new
{
    my $class = shift;
    my $self = {};

    # Just so the constructor does something I can see and verify.
    $self->{'time_created'} = time();
    print STDOUT "Object created at " . $self->{'time_created'} . "\n";

    bless ( $self, $class );
    return $self;
}

sub myModFunc
{
    my $self = shift;
    print "myModFunc called\n";
}

1;
[end myMod.pm]


      

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to