Nikita
Nikita

Reputation: 857

Perl: What type should be used for class object in "validate" function

I want to pass the reference of class object called "A" in constructor. And use "validate" function to check it.

like that:

test1.pm

my $object = Object1->new;

my $newObject = Object2->new({
    param1 => $object,
});

test2.pm

sub new {
    my $class = shift;

    my (%options) = validate (@_, {
        param1 => { type => SCALARREF, default => undef},
    });
    ...
}

The problem that I'm not sure about the type of the parameter param1. I tried "OBJECT" and "SCALARREF" but there were errors like "SCALARREF not allowed while strict sub".

What type should I use?

Upvotes: 0

Views: 174

Answers (1)

Axeman
Axeman

Reputation: 29854

It looks you're trying to do a quasi-Moose thing here. But in Moose, you don't create new subs, because Moose does that for you. If you need anything--you create a BUILD sub.

The Perl (5) base object system doesn't work like Moose, so 'SCALARREF' or whatever is what you make it in base Perl.

  • Do you realize that you are passing a hashref to new?
  • Do you realize that vaildate is getting two hashrefs?
    validate( {}, {} )

And if SCALARREF has not been defined, it will always be a bareword.

Read up on Moose. You can start with the Moose Types Manual, to see how ScalarRef is used, but since you don't even show "use Moose", you should start at page 1 of the Manual.

Upvotes: 2

Related Questions