Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Class::MethodMaker initialization of default values

by justanyone (Acolyte)
on Jul 01, 2002 at 22:32 UTC ( #178712=categorized question: print w/replies, xml ) Need Help??
Contributed by justanyone on Jul 01, 2002 at 22:32 UTC
Q&A  > object-oriented programming



I'm using Class::MethodMaker to construct a simple class with two vars. I want to initialize one var and leave the second as a defaulted value.

I think this means I need to do this:

use Class::MethodMaker '-sugar'; package Simple; make methods get_set => [qw(a b)], new_with_init => 'new'; sub init { my ($self, %args) = @_; #... what goes here? } ------ my $simp = new LogSimple(a => 4); print("a=" . $simp->a() . ", b = " $simp->b() . "\n"); ----------
How do I default the value of b to something? Class::MethodMaker has somewhat documentation on this, and I'm confused. My wife seems to think I'm not stupid (despite my opinion)... in which case this confusion might be something someone else has encountered...

Any hints? The perldoc / module documentation is somewhat limited.

Thanks for any assistance here,
Cordially yours,
-- Kevin J. Rice or email is kevin at also.

Answer: Class::MethodMaker initialization of default values
contributed by vishi83

Hi !!

i think this would be better , if you want to make b's value to be default ..

use constant SOMENAME ( a => 10, b=> 'DEFAULT');

The rest is yours.. the code you've given will work fine..

Answer: Class::MethodMaker initialization of default values
contributed by samtregar

How about (untested):

package Simple; use Class::MethodMaker get_set => [qw(a b)], new_hash_init => 'hash_init'; sub new { my $pkg = shift; my %args = ( b => "default", @_ ); return $pkg->hash_init(%args); }


Answer: Class::MethodMaker initialization of default values
contributed by justanyone

Okay, So this helped but it didn't solve it. I finally played with this enough - the real answer is (tested as correct):

package Simple; use Class::MethodMaker get_set => [qw(a b)], new_with_init => 'new'; sub init { my $self = shift; my %in_args = @_; my %default_args = ( b => "default"); my %final_args = (%default_args, %in_args); foreach my $thiskey (keys(%final_args)) { $self->{$thiskey} = $final_args{$thiskey}; } return; } -------------- in --------- use Simple; my $test = new Simple (a=>1); # now a==1, b==default. my $test = new Simple (a=>1, b=>2); # now a==1, b==2.
I've reported the contents of this posting to the Class::MethodMaker author to request that this information be included in the readme, or a modification to the module be made so it's easier to do.

Thanks for the assistance, samtregar, you started me on the right path. I appreciate it.

Cordially, -- Kevin

Please (register and) log in if you wish to add an answer

  • 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
    [Corion]: A good daystart to everybody!
    [Corion]: Just a quick poll - is anybody actively relying on https://perlmonks. I plan to retire that URL in favour of moving all our servers onto the same HTTPS certificate for perlmonks.{com, net,org}
    [Corion]: Actually bsd_glob '{www.,}perlmonks .{com,net,org}', plus I think
    [Corion]: Sad that Let's Encrypt does not allow wildcard certificates, but they could be somewhat of a hassle to verify

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2017-09-26 07:45 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (292 votes). Check out past polls.