timtowtdi has asked for the wisdom of the Perl Monks concerning the following question:
And here's my program calling it: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;
#!/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. :-)
|
|---|
| 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 | |
by timtowtdi (Sexton) on Sep 27, 2016 at 10:54 UTC | |
|
Re: Moose: Can't call method "insertcount" on an undefined value
by stevieb (Canon) on Sep 25, 2016 at 19:55 UTC | |
|
Re: Moose: Can't call method "insertcount" on an undefined value
by chacham (Prior) on Sep 26, 2016 at 14:00 UTC |