#$self{$property} = delete $param_hr->{$property};

The way I read the delete function in perldoc.perl.org listing for delete, the RHS populates the left.

From delete:

In list context, usually returns the value or values deleted, or the last such element in scalar context. The return list's length corresponds to that of the argument list: deleting non-existent elements returns the undefined value in their corresponding positions.

Perhaps it's not a good idea to delete the keys you set in $self from your input parameters. A simple $self{$property} = $param_hr->{$property}; suffices. You realise that by deleting, you modify a data structure created by the caller (%param_hr).

The Use of uninitialized value $akey in concatenation (.) or string at ./6.MY.translate.pl line 62. can be avoided if you move say "akey is  $akey "; after you check it is defined

Validating your input is great. But personally I would not validate the data types of params passed in a module's constructor. When you have a script and users run it from command line it is wise to (edit:over-)validate because a user may not have read the manual or is confused about input parameters. This is the normal scenario - judging from how I make such mistakes. However, I would categorise the programmer/user/caller of an API/module in slightly higher level and I would trust this user more. So I would still validate input params for right input values but checking for type, well that may cause me and the CPU too much extra work and just I do not do it. Practical reason: I sometimes set default params in $self to be undef. In which case no data type can be deduced. Saying in the pod that "this module does not validate input types, please observe the parameters' types stated" is enough for me. But may not be for others.

Note that if ( exists $param_hr->{$property} ) { passes if key exists, but its value may be undefined. e.g. for this input: $param_hr{'key'} = undef; edit: clearly 'key' exists, so exists passes, but its value is undef which !perhaps! misses what author intended.


In reply to Re^3: using Getopt::Long to modify the default values in new by bliako
in thread using Getopt::Long to modify the default values in new by Aldebaran

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.