1

I have one object that has an attribute of an array of another group of objects. I have a toString method that I want to print out the entire contents of the objects. The main goal is to have the Job object call all the post process jobs that are in the array. I want to call the method toString on the objects in the object array as well. Currently, I'm getting this error:

Can't call method "toString" without a package or object reference at JobClass.pm line 52, <JOBFILE> line 5. (which is $item->toString(); in the foreach loop)

Dumper on $item shows the following:

$VAR1 = bless( {
             'ImportID' => '22',
             'ImportTableID' => '1234',
             'ImportTable' => 'testImport'
           }, 'PostJob' );

Main goal of what I'm trying to understand is how I can call a method on an object returned from an member array.

Classes instantiated this way:

    my $postJob = PostJob->new(ImportTable => "testImport",ImportTableID => "1234", ImportID => "22");
    my @postJobs ="";
    push (@postJobs,$postJob);
    $postJob->toString(); #this works fine
    my $job = Job->new(DirectoryName => "testDir",StagingTableName => "stageTable", QBStagingTableID => "5678",postProcessJobs => \@postJobs); 
    $job->toString(); #Breaks with error above

Code is below:

    package PostJob;
    use Moose;
    use strict;
    use Data::Dumper;

    has 'ImportTable' => (isa => 'Str', is => 'rw', required => 1);
    has 'ImportTableID' => (isa => 'Str', is => 'rw', required => 1);
    has 'ImportID' => (isa => 'Str', is => 'rw', required => 1);

    sub toString {
     # Print all the values 
     my $self = shift;;
    print "Table Name for Post Job is ".$self->ImportTable."\n";
    print "Table ID for Post Job is ".$self->ImportTableID."\n";
    print "Import ID for Post Job is ".$self->ImportID."\n";
    }

    package Job;

    use strict;
    use Data::Dumper;
    use Moose;

    has 'DirectoryName' => (isa => 'Str', is => 'rw', required => 1);
    has 'StagingTableName' => (isa => 'Str', is => 'rw', required => 1);
    has 'StagingTableID' => (isa => 'Str', is => 'rw', required => 1);
    has 'postProcessJobs'=> (isa => 'ArrayRef', is => 'rw', required => 0);


    sub addPostJob {
     my ($self,$postJob) = @_;
     push(@{$self->postProcessJobs()},$postJob);

    }

    sub toString 
     {
     # Print all the values.
     my $self = shift;
     print "DUMPING JOB OBJECT CONTENTS*****************************\n";
     print "Directory is ".$self->DirectoryName."\n";
     print "Staging Table is ".$self->StagingTableName."\n";
     print "Staging Table ID is ".$self->StagingTableID."\n";

        print "DUMPING POST JOB CONTENTS*****************************\n";   
        foreach my $item (@{$self->postProcessJobs()})
            {

                $item->toString();
                print Dumper($item);
            }
        print "END DUMPING JOBS*****************************\n";    
    }


    1;

1 Answer 1

2

The problem is on the following line:

my @postJobs ="";

This creates the first member of the array, but this member is not a job, it is an empty string. Replace it with

my @postJobs;

and the error goes away.

Sign up to request clarification or add additional context in comments.

2 Comments

fantastic! Thanks! What is the proper way then to make sure an array is empty and has no elements in perl?
@glacierDiscomfort: If you are declaring it, my itself creates the array empty. If you want to clear an array, use undef @array or @array = () (keeps memory allocated).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.