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

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

Replies are listed 'Best First'.
Re^6: Wrong Error Message When Testing for Failure of Constructor
by jkeenan1 (Deacon) on Jul 24, 2005 at 01:40 UTC
    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.