Hello crew,

given a rainy weekend I played a bit with a module of mines. All went well till night but then some tests failed in a weird way. I was, inside the main module restantiate some object via new() using parameters stores inside a container of the father object and I had all check done to validate arguments passed to new failed for childrens..

the situation is similar to the following Exp.pm

package Exp; use 5.006; use strict; use warnings; use Carp; sub new{ my $class = shift; my $validated = _validate( $_[0] ); return bless { validated => $validated, stored => [], },$class; } sub _validate{ croak "No arg!" unless $_[0]; return $_[0]; } sub store { my $self = shift; my $it = _validate( $_[0] ); push @{ $self->{ stored } }, new( $it ); # <---here NOT OK } 1;

If I use the module in the plain way all is well:

# croaks without arguments perl -I . -MData::Dump -MExp -e " $exp=Exp->new(); dd $exp " No arg! at -e line 1. # OK with args perl -I . -MData::Dump -MExp -e " $exp=Exp->new(2); dd $exp " bless({ stored => [], validated => 2 }, "Exp")

But if now I call the store methods, it fails:

perl -I . -MData::Dump -MExp -e " $exp=Exp->new(2); $exp->store(3) " No arg! at -e line 1.

By other hands i found the following workaround using bless{ validated => $it },__PACKAGE__ instead of the new call: see the ExpOk.pm module:

package ExpOk; use 5.006; use strict; use warnings; use Carp; sub new{ my $class = shift; my $validated = _validate( $_[0] ); return bless { validated => $validated, stored => [], },$class; } sub _validate{ croak "No arg!" unless $_[0]; return $_[0]; } sub store { my $self = shift; my $it = _validate( $_[0] ); push @{ $self->{ stored } }, bless{ validated => $it },__PACKAGE__ +; # <---here OK } 1;

it works as (I) expected:

perl -I . -MData::Dump -MExpOk -e " $exp=ExpOk->new(2); $exp->store(3) +; dd $exp " bless({ stored => [bless({ validated => 3 }, "ExpOk")], validated => 2 + }, "ExpOk")

What happens with the first version of the module? Is my workaround a viable solution? Note that my intention is not to store children objects inside the father: in my project i store a configuration for various jobs to be executed and foreach one i instantiate a new object and i run it

thanks

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to calling new inside the same module? by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.