in reply to Re^2: Wrong Error Message When Testing for Failure of Constructor
in thread Wrong Error Message When Testing for Failure of Constructor

I suspect the reason is fundamentally the same. I missed that you have references among the values of %default_values, meaning that you need a deep copy (not the shallow copy originally proposed). Try this quick fix (just to confirm the hypothesis):

my %default = %default_values; # shallow copy $default{ AUTHOR } = +{ %{ $default_values{ AUTHOR } } }; # copy next level my $self = ref($class) ? bless( \%default, ref($class) ) : bless( \%default, $class );
Ultimately, my preferred code for this would be something like
my $self = bless deep_copy( \%default_values ), $class;
where deep_copy takes a reference as argument and returns a reference to a deep copy of it.

I know that there are CPAN modules for doing deep copying, but I have no direct experience with any of them, so I can't recommend any one in particular. Maybe some other monk can.

the lowliest monk

Replies are listed 'Best First'.
Re^4: Wrong Error Message When Testing for Failure of Constructor
by jkeenan1 (Deacon) on Jul 23, 2005 at 00:02 UTC
    tlm wrote:

    Ultimately, my preferred code for this would be something like

    my $self = bless deep_copy( \%default_values ), $class;

    where deep_copy takes a reference as argument and returns a reference to a deep copy of it.

    I struggled with this some more but, as long as my default values were stored in a hash referenced by the testing subroutine, kept coming up with the same problem. Here's a little program I wrote which takes all the OO stuff out and demonstrates that, contrary to my hope/expectation, %default_values does indeed get transformed over a series of code blocks.

    Note that what is dumped for $dvref the second time around differs from what the first time.

    I have, however, found a solution: Localize %default_values within a subroutine and have that sub return a reference to that hash. The above program gets transformed into:

    The constructor is transformed in this manner:

    my $self = ref($class) ? bless( default_values(), ref($class) ) : bless( default_values(), $class );

    So, instead of the constructor blessing a reference to a hash of default values, it blesses the return value of a subroutine where that value is in turn a reference to the hash of default values.

    As I said at the outset, this example was a stripped-down version of a problem I was facing with the ongoing development of a module I maintain. Ironically, the solution above is the solution which the original module author (Geoff Avery) came up with in earlier versions of the module (ExtUtils::ModuleMaker). Plus ça change ....

    Thanks again.

      Even though I'm convinced that proper copying would have solved the problem, I think that the idea of having subroutine return a brand-new lexical every time the defaults are needed is far preferable; I wish I had proposed it myself.

      That said, this code has some unwarranted inefficiencies:

      sub default_values { my %default_values = ( SUBJECT => 'Subject (<= 44 characters) goes here', AUTHOR => { NAME => 'A. U. Thor', CPANID => 'AUTHOR', WEBSITE => 'http://a.galaxy.far.far.away/modules' +, EMAIL => 'a.u.thor@a.galaxy.far.far.away', }, ); return { %default_values }; }
      Here the sub default_values generates a new hash, but then it returns a copy of it. That's doing twice the work. The assignment to the variable is also unnecessary work. I.e., just do this:
      sub default_values { return +{ SUBJECT => 'Subject (<= 44 characters) goes here', AUTHOR => { NAME => 'A. U. Thor', CPANID => 'AUTHOR', WEBSITE => 'http://a.galaxy.far.far.away/modules' +, EMAIL => 'a.u.thor@a.galaxy.far.far.away', }, }; }

      the lowliest monk

        tlm wrote:

        ...just do this:
        sub default_values { return +{ SUBJECT => 'Subject (<= 44 characters) goes here', AUTHOR => { NAME => 'A. U. Thor', CPANID => 'AUTHOR', WEBSITE => 'http://a.galaxy.far.far.away/modules' +, EMAIL => 'a.u.thor@a.galaxy.far.far.away', }, }; }

        Suggestion noted and implemented. However, that's the sort of tidying up I do at a later stage. I've tried putting %default_values in 3 or 4 different places so far, so having it as a named variable has so far been acceptable. Thanks.

Re^4: Wrong Error Message When Testing for Failure of Constructor
by jkeenan1 (Deacon) on Jul 22, 2005 at 19:45 UTC
    I will check this out over the weekend and report back. Thanks.