Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

What I don't like with default argument setups like yours is that they too often throw away a 0 argument. I find this quite disturbing, especially when I sometimes just want to set something to false but the routine does my $foo = shift; $self->{foo} = $foo if defined $foo and I gave a false (undefined) value. But I digress.

To adress the real issue again, I'd like to provide some constructors I sometimes use. I'm not very consistant myself in this matter, and I'm lazy and strict from time to time.

An alternative way to write yours, that I find more attractive is

{ my %SELF = map { $_ => '' } qw( foo bar baz ); sub new { my $self = bless { %SELF } => shift; @$self{keys %SELF) = @{+shift}{keys %SELF}; return $self; } }
This still explicitly defines the parameters, yet honours false values. You can also easily set other default values by just extending the list assigned to %SELF.

Another way I've used is

{ my %SELF = ( foo => 0, bar => "", baz => [], ); sub new { my $self = bless {} => shift; my %p = @_; croak("...") if keys %p > keys %SELF; # If given a bad key. $self->$_($p{$_}) foreach keys %p; # Lazy. Damn lazy. return $self; } }
I can't say this is a routine I recommend for daily use, but it was a slick routine in its context.

Yet another...

sub new { my $self = bless { foo => 0, bar => "", baz => [], this_should_not_be_touched_here => {}, } => shift; my %p = @_; $self->{$_} = delete $p{$_} for grep exists $p{$_} => qw/ foo bar baz /; # Note, this list doesn't equal keys(%$self). croak("Unknown parameters: @{[keys %p]}") if %p; return $self; }
The benefit with this is that you don't expose some elements that perhaps shouldn't be set in the constructor. The other is that you easily detect typos as they'll show up as unknown parameters.

I really had no point besides the false value issue with this post. I just felt like elaborating and tossing around ideas. :-)


In reply to Re: Re: Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /} by ihb
in thread Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /} by Roger

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2020-09-24 02:56 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (132 votes). Check out past polls.