Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Moose: problem with accessor

by saintex (Scribe)
on Mar 31, 2011 at 15:29 UTC ( [id://896651]=perlquestion: print w/replies, xml ) Need Help??

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

hello Monks,
I have a problem with accessors in Moose.

I have:

has mode =>(is => 'ro',accessor=>'creaLog', isa => 'Str',required => 1 +);

But if I try:
sub creaLog { my $self = shift; $self->mode=>'changed' ; }


I have this error:
Cannot define an accessor name on a read-only attribute, accessors are + read/write at /usr/local/lib/perl/5.10.1/Moose/Meta/Attribute.pm lin +e 251


then If I try:
has mode =>(is => 'ro',writer=>'creaLog', isa => 'Str',required => 1);

(changed from accessor to writer). I have:

You are overwriting a locally defined method (creaLog) with an accesso +r at /usr/local/lib/perl/5.10.1/Moose/Meta/Attribute.pm line 663
Where I'm wrong?

Thank you in advance for answers

Replies are listed 'Best First'.
Re: Moose: problem with accessor
by ikegami (Patriarch) on Mar 31, 2011 at 17:14 UTC

    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'); }
      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».

Re: Moose: problem with accessor
by Anonymous Monk on Mar 31, 2011 at 17:00 UTC
    Looking for a valid method call?
    sub creaLog { my $self = shift; $self->mode( 'changed' ); }
Re: Moose: problem with accessor
by Fletch (Bishop) on Mar 31, 2011 at 17:13 UTC

    You're trying to define a writer for a read-only attribute.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://896651]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found