in reply to Moose RegexpRef

Hello kdjantzen,

As tobyink says, you’re better off not setting a default value — especially as you overwrite it immediately in sub BUILD anyway. However, in the unlikely event that you really do have a use case for this, follow the advice of the error message generated when setting default => \qr//:

References are not allowed as default values, you must wrap the defaul +t of 're' in a CODE reference (ex: sub { [] } and not []) at ...

For example, this works:

#! perl use strict; use warnings; package Test { use Moose; has re => (is => 'rw', isa => 'RegexpRef', default => sub { qr/abc +/; }); sub BUILD { my $self = shift; $self->re(qr/^[0-9]+$/); } sub match { my ($self, $string) = @_; return $string =~ $self->re ? 'matches' : 'does not match'; } } my $t = Test->new(); print "$_ ", $t->match($_), "\n" for 'abc123def', '456';

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Moose RegexpRef
by kdjantzen (Acolyte) on Dec 16, 2014 at 09:29 UTC
    Hello,
    thank you for the quick reply.
    It works!!
    K.D.J.