Skip to content

Instantly share code, notes, and snippets.

@choroba
Last active March 26, 2018 14:21
Show Gist options
  • Save choroba/57c0d38d185f2f96d87963833c7d316e to your computer and use it in GitHub Desktop.
Save choroba/57c0d38d185f2f96d87963833c7d316e to your computer and use it in GitHub Desktop.
required moose attributes
#!/usr/bin/perl
use strict;
use feature qw{ say };
use warnings;
{ package My;
use Moose;
my $m = 1;
has req => ( is => 'ro', default => sub { $m }, required => 1, predicate => 'has_req' );
has nreq => ( is => 'ro', default => sub { $m }, required => 0, predicate => 'has_nreq' );
has laz => ( is => 'ro', default => sub { $m }, lazy => 1, predicate => 'has_laz' );
has buil => ( is => 'ro', required => 1, lazy => 1, builder => 'get_m', predicate => 'has_buil' );
sub get_m { $m }
sub inc { $m++ }
no Moose;
'My'->meta->make_immutable;
}
my $o = 'My'->new;
my $m;
$m = "has_$_", say "$m ", $o->$m for qw( req nreq laz buil );
$o->inc;
say "$_ ", $o->$_ for qw( req nreq laz buil );
$m = "has_$_", say "$m ", $o->$m for qw( req nreq laz buil );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment