in reply to Re: Moose Attribute Default Dependency
in thread Moose Attribute Default Dependency

Thanks superdoc. I'll go away, read and test some more.
  • Comment on Re^2: Moose Attribute Default Dependency

Replies are listed 'Best First'.
Re^3: Moose Attribute Default Dependency
by boftx (Deacon) on Nov 12, 2013 at 23:26 UTC

    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; } }

        What do you expect this to do?

        my $self;

        What do you expect this to do?

                             default   => $self->{Dbase}.'/come/on/now',

        Improve your skills with Modern Perl: the free book.

        Use a builder here instead of a default, $self doesn't exist yet when this is being executed:

        has 'Dbin' => ( is => 'rw', isa => 'Str', default => $self->{Dbase}.'/come/on/now', lazy => 1, predicate => 'has_Dbin' );

        Getting used to just how Moose builds an object, as well as the method modifiers, might lead to loss of hair at times, but groking this leads to solid code design with objects. On the plus side, Moose formalizes things to some extent that many of us had to improvise in the bad days.

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