开发者

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 "SCA开发者_开发技巧LARREF" but there were errors like "SCALARREF not allowed while strict sub".

What type should I use?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜