开发者

Perl Moose TypeDecorator error. How do I debug?

开发者 https://www.devze.com 2023-02-03 12:10 出处:网络
I\'ve recently run into a problem I\'d greatly appreciate any insight into.I posted a similar question prior to Christmas over at PerlMonks with some feedback to switch away from MooseX::Declare ([htt

I've recently run into a problem I'd greatly appreciate any insight into. I posted a similar question prior to Christmas over at PerlMonks with some feedback to switch away from MooseX::Declare ([http://www.perlmonks.org/?node_id=877703][1]). I have now switched the code over to vanilla Moose with MooseX::Types and MooseX::Params::Validate. However, the same error is occurring in the same spot. Not surprising since it appears to be MooseX::Types related.

I am getting the following error (tried to space this out for readability and bottom of stack truncated):


plxc16479> tmp10.pl

Argument cannot be 'name' at /nfs/pdx/disks/nehalem.pde.077/perl/lib64/site_perl/MooseX/Types/TypeDecorator.pm line 88

MooseX::Types::TypeDecorator::new('MooseX::Types::TypeDecorator=HASH(0x1620c58)', 'name', 'g1145114N5582201_16161616a2x_FU02xxT_2bxc2e3_6x0xxxp0fx0xxx0x...', 'mask_data', '', 'tags', 0) called at /nfs/pdx/disks/nehalem.pde.077/projects/lib/Program-Plist-Pl/lib/Program/Plist/Pl.pm line 61

Program::Plist::Pl::_create_pattern_obj(undef, 'name', 'g1145114N5582201_16161616a2x_FU02xxT_2bxc2e3_6x0xxxp0fx0xxx0x...', 'mask_data', '', 'tag_data', '') called at /nfs/pdx/disks/nehalem.pde.077/projects/lib/Program-Plist-Pl/lib/Program/Plist/Pl.pm line 77

Program::Plist::Pl::BUILD('Program::Plist::Pl=HASH(0x162d6c0)', 'HASH(0x162d648)') called at generated method (unknown origin) line 101

Program::Plist::Pl::new('Program::Plist::Pl', 'name', 'bist_hfmmin_16161616_list', 'parents', 'HASH(0xccf040)', 'fh', 'GLOB(0xccc928)', 'external_pl_code', 'CODE(0x14910b0)', ...) called at /nfs/pdx/disks/nehalem.pde.077/projects/lib/Program-Roles-PlHandler/lib/Program/Roles/PlHandler.pm line 52

Program::Roles::PlHandler::_create_global_pl_obj(undef, 'name', 'bist_hfmmin_16161616_list', 'parents', 'HASH(0xccf040)', 'fh', 'GLOB(0xccc928)') called at /nfs/pdx/disks/nehalem.pde.077/projects/lib/Program-Plist-Pl/lib/Program/Plist/Pl.pm line 77

Program::Plist::Pl::BUILD('Program::Plist::Pl=HASH(0xccd300)', 'HASH(0xccc628)') called at generated method (unknown origin) line 101

Program::Plist::Pl::new('Program::Plist::Pl', 'name', 'bist_list', 'parents', 'HASH(0xccce80)', 'fh', 'GLOB(0xccc928)', 'external_pl_code', 'CODE(0x14910b0)', ...) called at /nfs/pdx/disks/nehalem.pde.077/projects/lib/Program-Roles-PlHandler/lib/Program/Roles/PlHandler.pm line 52

The problem i seems to be the top call to TypeDecorator::new. The TypeDecorator constructor seems to expect two arguments, the class/self argument and a reference to a TypeDecorator or TypeConstraint object. Instead, it's somehow receiving the arguments from my create pattern object call. I have verified that the arguments coming into the _create_pattern_obj function are correct and that the arguments going into the Pattern->new call are also correct (borne out by the stack traced arguments). The _create_pattern_obj function looks like this:

sub _create_pattern_obj {
    my ($self, $name, $mask_data, $tag_data) = validated_list(\@_,
                                                              name => {isa => Str},
                                                              mask_data => {isa => Str, optional => 1},
                                                              tag_data => {isa => Str, optional => 1});

    $mask_data = '' if !defined $mask_data;

    my $tags = defined $tag_data ? map {$_ => 1} split(',', $tag_data) : {};

    my $pattern_obj = Program::Plist::Pl::Pattern->new(name => $name,
                                                       mask_data => $mask_data,
                                                       tags => $tags);
    $self->_add_pattern($pattern_obj);
}

The function is dieing on the Program::Plist::Pl::Pattern->new call, which is the line 61 in file Pl.pm file referenced in the above call stack where the TypeDecorator::new call is claiming to come from.

The Pattern class is:

package Program::Plist::Pl::Pattern;

use 5.012002;
our $VERSION = sprintf "2.%03d", q($Revision: 473 $) =~ /: (\d+)/;

use Moose;
use namespace::autoclean;

use MooseX::Types::Moose qw(Str Num Int HashRef);
use MooseX::Params::Validate;

has 'name' => (isa => Str,
               is => 'ro',
               required => 1);

has 'tuple' => (isa => Int,
                is => 'ro');

has 'tid' => (isa => Int,
              is => 'ro');

has 'weight' => (isa => Num,
                 is => 'ro');

has 'tags' => (isa => HashRef[Str],
               is => 'ro',
               default => sub {{}});

has 'mask_data' => (isa => Str,
                    is => 'rw',
                    default => '',
                    writer => '_set_mask_data');

sub has_tag {
    my ($self, $tag) = (shift,
                        pos_validated_list(\@_, {isa => Str}));

    exists $self->{tags}->{$tag} ? return 1 : return 0;
}

sub _add_tag {
    my ($self, $tag) = (shift,
                        pos_validated_list(\@_, {isa => Str}));
    $self->{tags}->{$tag} = 1;
}

sub BUILDARGS {
    print STDERR 'CALLED '.__PACKAGE__."BUILDARGS\n";
    print STDERR 'ARGUMENTS:'.join(',', @_)."\n";
}

sub BUILD {
    my ($self) = @_;
    print STDERR 'CALLED '.__PACKAGE__."::BUILD\n";
}

__PACKAGE__->meta->make_immutable;

1;

Somehow, judging from the arguments in the call stack, my arguments to the Pattern->new call are ending up being passed to the TypeDecorator::new call and it's choking on them. I've verified that a good call to this subroutine (from and earlier stack trace) looks like this (note the two arguments):


DB<1> T

$ = MooseX::Types::TypeDecorator::new('MooseX::Types::TypeDecorator', ref(Moose::Meta::TypeConstraint)) called from file `/nfs/pdx/disks/nehalem.pde.077/perl/lib64/site_perl/MooseX/Types.pm' line 464


The problem is that I can't figure out how to debug what's going on. When stepping through the code, execution passes directly from the Pattern->new call to the TypeDecorator code. This is occurring prior to any of my class code executing. I know Moose is creating the new method for me, but I can't figure out how to debug code I can't look at.

I've looked through documentation on Moose, but that's all on how to use it as opposed to what's going on under the hood. I did read through the Class::MOP documentation, but I'm unclear as to exactly where this code is being created and w开发者_开发问答hen. While I've learned a fair bit from all my research, none of it has directly help me with my problem :)

First off, any ideas as to what's occurring would be appreciated. Second, how do I debug into this issue? All my usual debug tools have failed me! The execution is jumping directly from my new call to the problem code and I can't seem to trace where the TypeDecorator::new arguments are actually being passed from. Lastly, are there any good writeups out there on exactly how Moose does what it does? Or Class::MOP?

Edit - Here are my type definitions. I might add this is my first foray into Moose, so if you see anything I'm doing that's odd feel free to point it out.

package Program::Types;

use 5.012002;
use strict;
use warnings;

our $VERSION = sprintf "2.%03d", q($Revision: 473 $) =~ /: (\d+)/;

# predeclare types
use MooseX::Types
-declare => [qw(NonemptyStr FilePath DirectoryPath FilePathThatExists DirectoryPathThatExists
                TwoDigNum Pl LocalPl Pattern Program_Env Program_Whichload Program_Tpl
                Program_Plist Program_Bmfc Program_Tpl_Test Program_Tpl_Flow
                Program_Tpl_Flow_Item Program_Tpl_Flow_Item_Result Word)];

# import some MooseX builtin types that will be built on
use MooseX::Types::Moose qw(Str Int Object);

# types base on some objects that I use
class_type Pl, {class => 'Program::Plist::Pl'};

class_type LocalPl, {class => 'Program::Plist::LocalPl'};

class_type Pattern, {class => 'Program::Plist::Pl::Pattern'};

class_type Program_Env, {class => 'Program::Env'};

class_type Program_Whichload, {class => 'Program::Whichload'};

class_type Program_Tpl, {class => 'Program::Tpl'};

class_type Program_Tpl_Test, {class => 'Program::Tpl::Test'};

class_type Program_Tpl_Flow, {class => 'Program::Tpl::Flow'};

class_type Program_Tpl_Flow_Item, {class => 'Program::Tpl::Flow::Item'};

class_type Program_Tpl_Flow_Item_Result, {class => 'Program::Tpl::Flow::Item::Result'};

class_type Program_Plist, {class => 'Program::Plist'};

class_type Program_Bmfc, {class => 'Program::Bmfc'};

subtype Word,
    as Str,
    where {$_ =~ /^\w*$/};

coerce Word,
    from Str,
    via {$_};

subtype NonemptyStr,
  as Str,
  where {$_ ne ''};

coerce NonemptyStr,
  from Str,
  via {$_};

subtype TwoDigNum,
  as Int,
  where {$_ =~ /^\d\d\z/},
  message {'TwoDigNum must be made of two digits.'};

coerce TwoDigNum,
  from Int,
  via {$_};

subtype FilePath,
  as Str,
  where {!($_ =~ /\0/)},
  message {'FilePath cannot contain a null character'};

coerce FilePath,
  from Str,
  via {$_};

subtype DirectoryPath,
  as Str,
  where {!($_ =~ /\0/)},
  message {'DirectoryPath cannot contain a null character'};

coerce DirectoryPath,
  from Str,
  via {$_};

subtype FilePathThatExists,
  as Str,
  where {(!($_ =~ /\0/) and -e $_)},
  message {'FilePathThatExists must reference a path to a valid existing file.'.
           "Path ($_)"};

coerce FilePathThatExists,
  from Str,
  via {$_};

coerce FilePathThatExists,
  from FilePath,
  via {$_};

subtype DirectoryPathThatExists,
  as FilePath,
  where {(!($_ =~ /\0/) and -d $_)},
  message {'DirectoryPathThatExists must reference a path to a valid existing '.
           "directory.  Path ($_)"};

coerce DirectoryPathThatExists,
  from Str,
  via {$_};

coerce DirectoryPathThatExists,
  from DirectoryPath,
  via {$_};

1;

Edit2 -- Removed due to obvious operator error :) Note that I am using BUILDARGS in the Pattern class without returning the argument list. I have removed this in current code with no change to the error.

Phaylon, Here's the Program::Plist::Pl class.

package Program::Plist::Pl;

use 5.012002;
our $VERSION = sprintf "2.%03d", q($Revision: 473 $) =~ /: (\d+)/;

use Moose;
use namespace::autoclean;

use Program::Plist::Pl::Pattern;
use Program::Types qw(Pl LocalPl TwoDigNum Pattern);
use Program::Utils qw(rchomp);

use MooseX::Types::Moose qw(HashRef GlobRef Str);
use MooseX::Params::Validate;

with 'Program::Roles::PlHandler';

has 'name' => (isa => Str,
               is => 'ro',
               required => 1);

has 'parents' => (isa => HashRef[Pl|LocalPl],
                  is => 'ro',
                  required => 1);

has 'children' => (isa => HashRef[Pl|LocalPl],
                   is => 'ro');

has 'prefixes' => (isa => HashRef[TwoDigNum],
                   is => 'ro',
                   default => sub{{}});

has 'patterns' => (isa => HashRef[Pattern],
                   is => 'ro',
                   default => sub{{}});

sub _add_child {
    my ($self, $obj) = (shift,
                        pos_validated_list(\@_, {isa => Pl|LocalPl}));
    $self->{children}->{$obj->name} = $obj;
}

sub _add_pattern {
    my ($self, $obj) = (shift,
                        pos_validated_list(\@_, {isa => Pattern}));
    $self->{patterns}->{$obj->name} = $obj;
}

sub _create_pattern_obj {
    $DB::single = 1;
    my ($self, $name, $mask_data, $tag_data) = validated_list(\@_,
                                                              name => {isa => Str},
                                                              mask_data => {isa => Str, optional => 1},
                                                              tag_data => {isa => Str, optional => 1});

    $mask_data = '' if !defined $mask_data;

    my $tags = defined $tag_data ? map {$_ => 1} split(',', $tag_data) : {};

    $DB::single = 1;
    my $pattern_obj = Program::Plist::Pl::Pattern->new(name => $name,
                                                       mask_data => $mask_data,
                                                       tags => $tags);
    $self->_add_pattern($pattern_obj);
}

sub BUILD {
    my ($self, $fh) = (shift,
                       pos_validated_list([$_[0]->{fh}], {isa => GlobRef}));

    while (<$fh>) {
        # skip empty or commented lines
        rchomp;
        next if ((/^\s*#/) or (/^\s*$/));

        # handle global plist declarations
        if (my @m = /^\s*GlobalPList\s+(\w+)/) {
            # creating new object and adding it to our data print STDERR
            #                    "SELF($self)\n".join("\n",sort keys
            #                    %Program::Plist::Pl::)."\n"; 
            $self->_create_global_pl_obj(name => $m[0],
                                         parents => {%{$self->parents},
                                                     $self->name => $self},
                                         fh => $fh);
        }

        # handle local referenced plist declarations
        elsif (@m = /^\s*PList\s+(\w+):(\w+)/) {
            $self->_create_local_pl_obj(file => $m[0],
                                        name => $m[1]);
        }
        # handling pattern lines
        elsif (@m = /^\s*Pat\s+(\w+)\s*(\[.*\])?\s*;\s*(#([\w,])#)?\s*$/) {
            $self->_create_pattern_obj(name => $m[0],
                                       mask_data => do {defined $m[1] ? $m[1] : ''},
                                       tag_data => do {defined $m[2] ? $m[2] : ''});
        }
        # handling our patlist closure
        elsif (/^\s*\}/) {
            last;
        }
    }

    # need to populate our hash of child plists
    for (@{$self->data}) {
        if (($_->isa('Pl')) or ($_->isa('LocalPl'))) {
            $self->_add_child($_);
        }
    }
}

__PACKAGE__->meta->make_immutable;

1;


The problem is I believe here.

use Program::Types qw(Pl LocalPl TwoDigNum Pattern);

You're importing a function named Pattern into your Program::Plist::Pl class. You then call this function (unintentionally) here:

    my $pattern_obj = Program::Plist::Pl::Pattern->new(name => $name,
                                                   mask_data => $mask_data,
                                                   tags => $tags);

Specifically Program::Plist::Pl::Pattern resolves to your fully qualified function name rather than to the Class (technically package name) you're expecting. This function returns a TypeObject which you then call new() on.

Note: This is exactly what phaylon suggests in the comments above.

There really is no way to debug this except to know that you can always call a function with it's fully qualified name, and thus should never have a MooseX::Type and a valid Class name collide.

If it were me I'd start writing a very simple test case and add code to replicate the original file until it breaks. I'd probably start with the call to new. Then slowly add back assumptions until I found the one that breaks. Hopefully you add the MooseX::Types call early enough in that process that it triggers the "oh duh obviously that is it" moment.

0

精彩评论

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