in reply to calling new inside the same module?

What kind of object do you want to store? Note that running bless doesn't run the constructor, so in this case, the stored objects don't have the stored attribute (which is harmless, but for other classes, much more might be going on in the constructor).

You could call new in the package itself. To specify the class, it's better to use ref $self than __PACKAGE__, so if you later inherit from the class, objects of the new class respect the class of the storing object.

push @{ $self->{stored} }, ref($self)->new($it);

It might be cleaner to let the user specify what class to use, so they can use various classes according to their needs (see Dependency Injection). It's not clear how validation should work in such a case, though: maybe the classes themselves should define a validate method? Or once the object's been constructed, it's guaranteed to be valid? Give us more details so we can answer (or ask) more questions.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: calling new inside the same module?
by Discipulus (Canon) on Nov 26, 2018 at 18:10 UTC
    Thank you all monks,

    > 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

    So i have not to store children objects; i rather have an object that can do an action OR can load a serie of small configuration e instantiate children to do each one one action. Like:

    my $obj = new->( arg => 23); $obj->run(); # run will use 23 # OR INSTEAD: my $obj = new->( configuration => file ); # this will load [12, 23, 15 +] into $obj->{ container } $obj->run_all(); sub run_all{ my $self = shift; foreach my $it ( @{$obj->{ container }} ){ my $new_obj = ref($self)->new($it); $new_obj->run; } } # update: i had Class instead of new in the above: corrected

    It is that bad? I suppose no..

    I'd go with ref($self)->new($it) instead of __PACKAGE__->new($it) because seems more clear even if i have no intention to subclassing.. it is already a mess ;=)

    > Or once the object's been constructed, it's guaranteed to be valid?

    Yes, it is supposed to be like this; once instantiated the object is guaranteed to be valid.

    Anyway, now that the confusion is away from my mind, I know what i intended to write: infact new is a normal subroutine, but what i forgot is that it shift the package name!!

    What i intended to write was: new(__PACKAGE__,$it)

    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.