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

I am making my first steps with Object Oriented Perl, and I am afraid a have a pretty newbie question. Using Moose, I made the following class:
package MSOOMoose; use Moose; has 'query' => ( is => 'rw', isa => 'Str' ); has 'commit' => ( is => 'rw', isa => 'Int' ); has 'insertcount' => ( is => 'rw', isa => 'Int' ); sub setQuery { my $self = shift; my $value = shift; $self->{query} = $value; } sub committoDB { my $self = shift; print "Dit is subroutine: committoDB\n"; # RESET COUNTER #$self->{insertcount} = 0; print $self->insertcount, "***\n"; } sub insertQuery { my $self = shift; my $commitvalue = shift; $self->{insertcount} += 1; my $counter = $self->insertcount; print "Count: $counter\tInsertQuery: ", $self->query, "\n"; # ADD TO HASH: FOR LATER if (defined($commitvalue) && ($commitvalue == 1)) { print "Yes, commit! Commitvalue = $commitvalue\n"; committoDB; } elsif ($counter == $self->commit) { committoDB; } } no Moose; 1;
And here's my program calling it:
#!/usr/bin/perl -w use strict; use warnings; use MSOOMoose; my $sqlsyntax; my $msoo = MSOOMoose->new( commit => 5 ); $msoo->setQuery("INSERT INTO groupinsert (number) VALUES ('2')"); $msoo->insertQuery(1); # 1 means: force commit for ($a = 0; $a < 10; $a += 1) { $sqlsyntax = qq{INSERT INTO groupinsert (number) VALUES ('$a')}; $msoo->setQuery($sqlsyntax); $msoo->insertQuery(); } exit(0);

I get an error saying: "Can't call method "insertcount" on an undefined value at MSOOMoose.pm line 45." This is the last line in the sub committoDB.

How can I get the value of insertcount in this methode? It looks like 'insertcount' is not in my namespace here.

The objective is to create a database module in where I can force commits to the database, or after a certain amount of inserts. But mostly, I am just trying to understand OO in Perl.

Any help will be much appreciated!

And a link to something like Moose for dummies too. :-)

The great mistake is to anticipate the outcome of the engagement; Let nature take its course, and your tools will strike at the right moment.

Replies are listed 'Best First'.
Re: Moose: Can't call method "insertcount" on an undefined value
by choroba (Cardinal) on Sep 25, 2016 at 19:56 UTC
    You forgot to call the committoDB method as a method:
    if (defined($commitvalue) && ($commitvalue == 1)) { print "Yes, commit! Commitvalue = $commitvalue\n"; $self->committoDB; # <- HERE } elsif ($counter == $self->commit) { $self->committoDB; # <- and HERE! }

    $self was undef because of this, and $self->insertcount had to fail.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you for the clear answer. Problem solved!
      The great mistake is to anticipate the outcome of the engagement; Let nature take its course, and your tools will strike at the right moment.
Re: Moose: Can't call method "insertcount" on an undefined value
by stevieb (Canon) on Sep 25, 2016 at 19:55 UTC

    You forgot to pass in $self in your calls :)

    Change these lines:

    committoDB;

    to:

    $self->commitoDB;
Re: Moose: Can't call method "insertcount" on an undefined value
by chacham (Prior) on Sep 26, 2016 at 14:00 UTC

    $sqlsyntax = qq{INSERT INTO groupinsert (number) VALUES ('$a')};

    Side note: Variables in statements is known as dynamic sql, and is generally considered insecure. Instead, please use a placeholder (a question mark: ?) and pass the value when executing the query.