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.

In reply to Moose: Can't call method "insertcount" on an undefined value by timtowtdi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.