Changing attribute access

2009-08-18 Thread Yuri Shtil

Hi,

Is there a way to change access to a lazy attribute to read-only once 
the value has been set in the builder?


--

Yuri



Re: Changing attribute access

2009-08-18 Thread Hans Dieter Pearcey
Excerpts from Yuri Shtil's message of Tue Aug 18 09:07:27 -0700 2009:
 Is there a way to change access to a lazy attribute to read-only once 
 the value has been set in the builder?

No, that would be horrible.  Just set it to readonly in the first place; that
won't stop a default or builder from generating a value.

hdp.


Re: Changing attribute access

2009-08-18 Thread Yuri Shtil

Chris Prather wrote:

On Tue, Aug 18, 2009 at 12:07 PM, Yuri Shtilyu...@juniper.net wrote:
  

Hi,

Is there a way to change access to a lazy attribute to read-only once the
value has been set in the builder?



This question doesn't exactly make sense. Builder / Default have
nothing to do with the access of the attribute:

package Example;
use Moose;

has name = ( is = 'ro', lazy = 1, builder = '_build_foo' );
sub _build_foo { __PACKAGE__ }

say Example-new-name; # prints __PACKAGE__ from _build_foo
say Example-new-name('bar') # this will blow up even though
_build_foo was never called

my $ex = Example-new;

say $ex-name; # prints __PACKAGE__
$ex-name('bar') # blows up

Is this the behavior you want? This works by default.

-Chris
  

Actually I realized I made a mistake in my question.
I want to be able to make an attribute read only after changing it in 
certain places of my code. The reason is that I want to control access 
similar to private in C++;


--

Yuri



Re: Changing attribute access

2009-08-18 Thread Hans Dieter Pearcey
Excerpts from Yuri Shtil's message of Tue Aug 18 12:15:03 -0700 2009:
 Actually I realized I made a mistake in my question.
 I want to be able to make an attribute read only after changing it in 
 certain places of my code. The reason is that I want to control access 
 similar to private in C++;

Don't change it to readonly; make the writer private ('_set_foo').

  is = 'ro'

is just a shortcut for

  reader = $attribute_name

so you would do something like

  has foo = (is = 'ro', writer = '_set_foo');

An attribute is actually never 'readonly' in some inherent sense -- the methods
attached to it generates may allow writing, or may not, but there's no security
layer behind those methods stopping you from setting the slot value.

hdp.