#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246

    Can't get Moose builder / default to work?


    Hi I have the following in my class...

    Code:
    package Sql;
    use DBI;
    use Moose;
    use Data::Dumper;
    ####################
    # Class Attributes #
    ####################
    
    has 'server' => (
        is       => 'ro',
        isa      => 'Str',
        required => 1,
        );
    
    has 'dsn' => (
        is       =>  'ro',
        isa      =>  'Str',
        required =>  1,
        );
          
    
    has 'user'   => (
        is       => 'ro',
        isa      => 'Str',
        required => 1,
        );
        
    
    has 'password'  => (
        is       => 'ro',
        isa      => 'Str',
        required => 1,
        );
            
    
    has 'dbh'   => (
        is      => 'ro',
        builder => '_build_dbh',
        lazy    => 1,
        );
    
    
    sub _build_dbh {
        my $self = shift; 
        return DBI->connect($self->{dsn} . ';UID=' . $self->{user} . ';PWD=' . $self->{password},{'RaiseError' => 1}) || die("Error Connecting $self->{server}: $DBI::errstr");    
    }
    But when my class tries to access $self->{dbh}->prepare , it doesn't exist and causes the error...

    "Can't call method "prepare" on unblessed reference
    Which seems to imply 'builder' isn't working?

    I've tried
    Code:
    has 'dbh'   => (
        is      => 'ro',
        default => sub {my $self = shift; 
        return DBI->connect($self->{dsn} . ';UID=' . $self->{user} . ';PWD=' . $self->{password},{'RaiseError' => 1}) || die("Error Connecting $self->{server}: $DBI::errstr");    }
        lazy    => 1,
        );
    But that doesn't work either?

    What am I doing wrong?

    Thanks,
    1DMF
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    Shouldn't you be creating an instance of the class?
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    Sorry you've lost me, instance of what class?

    DBI->connect , creates the the DBI dbh handle?

    Which I'm trying to set as the default to the attribute 'dbh' and my Sql class is being instantiated via Catalyst::Model::Adaptor

    Can you elaborate please.

    1DMF
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  6. #4
  7. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    Code:
    sub _build_dbh {
        my $self = shift; 
        print Dumper $self;
        return DBI->connect($self->{dsn} . ';UID=' . $self->{user} . ';PWD=' . $self->{password},{'RaiseError' => 1}) || die("Error Connecting $self->{server}: $DBI::errstr");    
    }
    Sorry, I thought your code was all there was, not aware of Catalyst, but try printing out the value of $self, just after it's shifted
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    I tried that I even put
    Code:
    die Dumper $self;
    nothing gets outputted which is why I get the feeling the builder '_build_dbh' isn't being called?
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  10. #6
  11. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    Have you a working package that we can compare to yours?
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    nope, 1st attempt as moose-ifying a script.

    This is my 'learn how to do it' module before I develop using it or not as the case may be!
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Shouldn't it be $self->dbh->prepare not $self->{dbh}->prepare? (And ditto for any other property accesses)
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    Thanks OmegaZero,

    That did the trick.

    Can you clarify ->xyz vs ->{xyz} as thought $self->xyz , is saying call method xyz and $self->{xyz} is get attribute xyz?

    If I use
    Code:
    $self->dbh->LongReadLen = 99999;
    It errors with unable to locate method LongReadLen ?

    So have to use
    Code:
        $self->dbh->{LongReadLen} = 99999;
    Cheers,
    1DMF
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!

IMN logo majestic logo threadwatch logo seochat tools logo