Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Moose, Class::DBI, and builder

by perldiverx (Beadle)
on Jul 10, 2014 at 18:59 UTC ( [id://1093105]=perlquestion: print w/replies, xml ) Need Help??

perldiverx has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm having a problem getting a builder to run in a subclass. Whenever I try and run a script that calls $obj->{'manual_phone'} it comes back undefined and I'm not sure why. I thought this was supposed to be built when the accessor was called.
Anyways, here's what I have.
package Transaction; use Moose; use MyDB::Merchid; use Win32; has 'transaction_type' => ( is => 'rw', isa => 'Str', required => 1, ); has merch_info => ( is => 'ro', isa => 'Any', builder => '_build_merch_info', ); sub _build_merch_info { my $self = shift; my $snum = substr(Win32::NodeName, 5); return MyDB::Merchid->retrieve($snum); } 1; package MyDB; use strict; use warnings; use base 'Class::DBI'; MyDB->connection('dbi:ODBC:mydbsqldsn') || die "Couldn't connect to da +tabase: " . $DBI::errstr . "\n"; 1; package Transaction::CC; use Moose; extends(qw/Transaction/); has 'manual_phone' => ( is => 'ro', isa => 'Str', lazy_build => 1, ); sub _build_manual_phone { my $self = shift; if (! defined $self->{merch_info}) { die "ERROR: merch_info not defined\n"; } return $self->{merch_info}->MANUAL_NUMBER; } 1; package MyDB::Merchid; use strict; use warnings; use base 'MyDB'; MyDB::Merchid->table('MERCHID'); MyDB::Merchid->columns(All => qw/ COMPANY_NUMBER MERCHID_SIC_CODE MERCHID_QUAL_CODE MANUAL_NUMBER /); 1;
A snippet from the script:
my $obj = Transaction::CC->new( 'transaction_type' => 'Visa', ); print "\$obj->{'manual_phone'}: " . $obj->{'manual_phone'} . "\n";

Replies are listed 'Best First'.
Re: Moose, Class::DBI, and builder
by boftx (Deacon) on Jul 10, 2014 at 19:28 UTC

    Yes, it should be built when the method is called. You are accessing the attribute element directly instead of calling the method on the object.

    print "\$obj->{'manual_pnone'} " . $obj->{'manual_phone'} . "\n"; # wr +ong # as opposed to print "\$obj->manual_pnone " . $obj->manual_phone . "\n"; # correct
    Which ultimately fails because of the same mistake here:
    sub _build_manual_phone { my $self = shift; if (! defined $self->{merch_info}) { die "ERROR: merch_info not defined\n"; } return $self->{merch_info}->MANUAL_NUMBER; }
    Always use accessors unless you are in the package that defines the element. (And many would argue you should do so even there.)

    Update: I would also double check the case of the column names. Are they really being stored in "merch_info" as upper case?

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      Thanks, that got it! I must have misunderstood the Moose docs. As for the column names, yes, they are stored in all caps. I didn't create the DB though, just working with it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1093105]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found