vit has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible and does it make sense to create constructors with empty $self? Say I do not know parameters so I cannot even assign "undef".
sub new { my $class = shift; my $self = { ## I do not know parameters ?? }; bless $self, $class; return $self; }

Replies are listed 'Best First'.
Re: Creating consructor new with empty $self
by jethro (Monsignor) on Oct 27, 2010 at 17:27 UTC
    Does it make sense? Yes, for example for a shopping cart object, shopping carts usually start empty.
Re: Creating consructor new with empty $self
by Corion (Patriarch) on Oct 27, 2010 at 16:56 UTC

    What happened when you tried?

      Nothing happened. So $self will be just an empty hash reference?
Re: Creating consructor new with empty $self
by locked_user sundialsvc4 (Abbot) on Oct 27, 2010 at 18:46 UTC

    This notion doesn’t make any sense to me.   Anytime you use the classname -> method syntax, the first parameter to method will always be classname.   And, the constructor (new) must always initialize and return a blessed object-instance.

    It is perfectly okay for the “blessed object” to be made from an empty-hash.   You do not have to have any instance-variables.

Re: Creating consructor new with empty $self
by Anonymous Monk on Oct 27, 2010 at 20:07 UTC
    Your object doesn't have to be a hash reference, much less contain elements.
Re: Creating consructor new with empty $self
by pajout (Curate) on Oct 27, 2010 at 17:48 UTC
    I think it makes sense. For instance, consider your constructor and following getter/setter method:

    sub temperature { my ($self, $t) = @_; if (1 < @_) { $$self{temperature} = $t; } return $$self{temperature}; }

    -> I do not see any technical reason for initial filling. Of course, sometimes you really want to initialize it because application logic.

Re: Creating consructor new with empty $self
by Arunbear (Prior) on Oct 27, 2010 at 17:32 UTC
    What is the purpose of these objects? That should give an idea of what attributes might be needed.