I'm getting the listed error when trying to run code. I started by looking at the previous answers to similar questions. This question gave the best info. It showed me how to look at the item being loaded.
Here is my directory tree:
Pagerduty/
Client.pm
Obj.pm
test/
Here is what the tester currently looks like:
#!/usr/bin/env perl
use lib "../..";
use strict;
use warnings;
use YAML qw/LoadFile/;
use Pagerduty::Client;
use Pagerduty::Obj;
use Data::Dumper;
use constant TRUE => 1;
use constant FALSE => 0;
print($INC{"Pagerduty/Obj.pm"}, "\n");
my $settings = LoadFile("pagerduty.yml");
my $client = Pagerduty::Client->new($settings);
sub create_test_incident{
my $incident_params = define_test_incident();
my $incident = $client->create_incident( $incident_params );
}
sub define_test_incident{
{
service_key => "<omitted>",
description => "Test Incident",
details => {
detail1 => "Details blah",
detail2 => "more detail",
},
}
}
my $obj = define_test_incident();
# print Dumper($obj);
print Dumper( Pagerduty::Obj->new( $obj ) );
# print Dumper( create_test_incident() )
Here is Pagerduty::Obj
package Pageduty::Obj;
use strict;
use warnings;
use Data::Structure::Util qw/unbless/;
use constant TRUE => 1;
sub new{
my $class = shift;
my $self = shift;
# $self->{objCode} = lc($self->{objCode});
return bless $self, $class;
}
sub serialize{
my $self = shift;
unbless($self);
}
sub update_values{
my $self = shift;
my $changes = shift;
if($self->{changes}){
while(my ($key, $value) = each $changes){
$self->{changes}->{$key} = $value;
}
} else{
$self->{changes} = $changes;
}
}
TRUE;
Finally, when I run the code I get the following:
../../Pagerduty/Obj.pm
Can't locate object method "new" via package "Pagerduty::Obj" (perhaps you forgot to load "Pagerduty::Obj"?)
When I cat ../../Pagerduty/Obj.pm I get the code listed above. This code lives in a library I created. I put that library in the variable PERL5LIB. I have the 'use lib ../..' line because I want it work on any computer. I have been trying to figure out for the past 2 days why this code is not working. Any ideas?
The other class works accept not being able to create new Pagerduty::Obj with the same error message.
Your file is Pagerduty/Obj.pm
, and you are trying to use the class Pagerduty::Obj
, but at the top of the module you have
package Pageduty::Obj;
which should be
package Pagerduty::Obj;
By the way, the canonical way of adding include directories relative to the location of the script is to use FindBin
, which sets a scalar variable $FindBin::Bin
(amongst others) to the absolute path to the directory that contains the Perl program file.
use FindBin;
use lib "$FindBin::Bin/../..";
You may have heard about FindBin
being broken, but it has been corrected recently in version 16 of Perl 5.