in reply to Moose Attribute Default Dependency

I would say that your declaration for 'x' is fine as is. It is your declaration for 'y' that needs to be lazy and have a builder because you can't depend upon 'x' to have been defined before 'y' is.

has 'y' => ( is => 'rw', isa => 'str', lazy => 1, builder => '_build_y' ); sub _build_y { my $self = shift; return $self->{x}.'y' ); }

Update:

You should bear in mind that while this would okay for when the object is being constructed, you should also consider the case if 'x' can be changed later on. At that point 'y' would probably not hold what you want, so you should consider reading about predicates and clearers, as well as triggers.

The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.

Replies are listed 'Best First'.
Re^2: Moose Attribute Default Dependency
by dpath2o (Acolyte) on Nov 12, 2013 at 23:14 UTC
    Thanks superdoc. I'll go away, read and test some more.

      As an aside, you should always use object accessors instead of raw object elements unless you have a very good reason not to, especially with Moose objects, so that you take full advantage of any modifiers such as 'before', 'around' and 'after' as well as triggers that may be present. Direct access to the element bypasses all of those features which will probably result in undesired side-effects.

      # use this: $obj->x . 'y'; # and not this: $obj->{x} . 'y';
      The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.

        I went away and now I'm back ...

        I read up on predicate, clearer and trigger attribute options and have revised the code. Though it bombs with the following error:

        Can't call method "Dbase" on an undefined value at pizzle.pm line 29.
        Compilation failed in require at test_pizzle.pl line 4.

        Let me know if you have any further comments, before I begin to start to hit head here
        XXXXXXXXXXXX
        XXXXXXXXXXXX
        XXXXXXXXXXXX
        XXXXXXXXXXXX
        XXXXXXXXXXXX
        XXXXXXXXXXXX

        package pizzle; use Moose; my $self; # Package Attributes has 'class_ver' => ( is => 'rw', isa => 'Str', default => '10.5.1' ); # Required Attributes has 'site' => ( is => 'rw', isa => 'Str', required => 1 ); # Directory Attributes has 'Dbase' => ( is => 'rw', isa => 'Str', default => '/willy/wonka', lazy => 1, predicate => 'has_Dbase' ); has 'Dbin' => ( is => 'rw', isa => 'Str', default => $self->{Dbase}.'/come/on/now', lazy => 1, predicate => 'has_Dbin' );

        More directory attributes ...

        # Diagnostic Attributes has 'diagCCS' => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_diagCCS', predicate => 'has_diagCCS' ); has 'diagCFR' => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_diagCFR', predicate => 'has_diagCFR' );

        More diagnostic attributes ...

        # Diagnostic Builders sub _build_diagCCS { my $self = shift; return `"$self->{Dbin}"/GetParameter "$self->{Fverbose}" 1 1 0 1`; } sub _build_diagCFR { my $self = shift; return `"$self->{Dbin}"/GetParameter "$self->{Fverbose}" 2 1 7 1`; }

        More diagnostic builders ...

        # Methods sub CleanDproc { my $self = shift; unlink $self->{Dproc}.'LLUVToCombine.list'; unlink $self->{Dproc}.'MergeTotalsInfo.ctf'; unlink $self->{Dproc}.'XYUVToCombine.list'; unlink $self->{Dproc}.'TotalProcessed.txt'; unlink $self->{Dproc}.'TotalTime.txt'; unlink $self->{Dproc}.'TotalOutput.txt'; unlink $self->{Dproc}.'Tot_XXXX_00_00_00_0000.tv'; unlink $self->{Dproc}.'TotxXXXX_00_00_00_0000.tv'; } 1;

        End of pizzle.pm

        Then test_pizzle.pl

        #!/usr/bin/perl use strict; use pizzle; my $keepgoing = 1; #infinite loop while ($keepgoing eq 1) { my $piz = pizzle->new( site => 'binky' ); $piz->DiagCCS; $piz->DiagCCG; if ($piz->DiagCCS) { $keepgoing = 0; } }