A few nodes dealing with attribute issues over the last little while prompted me to learn a little about this rather under used feature of Perl. A need to validate parameter lists to object constructors and option setting members inspired me to rope attributes into the task. Consider:

package Entity; use strict; use warnings; my %okParams; my %paramBase; sub FETCH_CODE_ATTRIBUTES { my ($package, $referrent) = @_; return () unless exists $paramBase{$package}; return @{$paramBase{$package}}[1 .. $#{$paramBase{$package}}]; } sub MODIFY_CODE_ATTRIBUTES { my ($package, $referrent, @attrs) = @_; @attrs = map {"-$_"} @attrs; $okParams{$_} = $package for @attrs; $paramBase{$package} = \@attrs; return (); } sub new: Id { my ($class, %params) = @_; my %self; @self{keys %okParams} = @params{keys %okParams}; my @badParams = grep {! exists $self{$_}} keys %params; warn "Class $class doesn't accept the following parameters: @badPa +rams" if @badParams; return bless \%self, $class; } 1; package Member; use base qw(Entity); sub new: Name Nick Age Gender { my ($class, %params) = @_; return Entity::new ($class, %params); } 1; package main; my $member = Member->new (-Name => 'fred', -Id => 1, -Wibble => 1);

generates the warning:

Class Member doesn't accept the following parameters: -Wibble at nonam +e.pl line 42.

DWIM is Perl's answer to Gödel

In reply to Attributes with new to manage parameters by GrandFather

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.