in reply to Moose: problem with accessor

I'm guessing you don't want to the mode to be changed except by methods of the class. If so, make a "private" writer.

has mode => ( reader => 'mode', writer => '_set_mode', isa => 'Str', required => 1, ); sub creaLog { my $self = shift; $self->_set_mode('changed'); }

Replies are listed 'Best First'.
Re^2: Moose: problem with accessor
by saintex (Scribe) on Mar 31, 2011 at 19:32 UTC
    Hello,
    thank you for your reply.

    What is the difference between
    $self->method=>'changed';
    and
    $self->method('changed');
    ?
    Is it a private call?
    How can I generally define private methods or attributes?

    Don't I need to define:
    sub _set_mode { ... }
    in some part of the code?

      '=>' is a (slightly special) comma, so

      $self->method => 'changed'
      is
      $self->method, 'changed'
      which is
      $self->method(), 'changed'

      How can I generally define private methods or attributes?

      "Perl doesn't have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun" — Larry Wall

      You can't force something to be private (and it would be useless to do so). However, naming something with a leading "_" is a well known convention to indicate something is private.

      Don't I need to define [_set_mode]

      No. When you use 'reader', 'writer' and 'accessor', you are asking Moose to create the a function for you.

      Keep in mind that «is => 'ro'» is a shortcut for «reader => $att_name» and «is => 'rw'» is a shortcut for «accessor => $att_name».

        hello,
        thank you for your excellent answer.

        At this point, what is the difference between:
        $self->method(), 'changed' and  $self->method('changed') ?