Hello Aldebaran,

I thought that I would emulate the syntax in source listing for Translate.pm. (Is this well-written?)

Not entirely — see below.

With this syntax, I believe that $param_hr is to be understood as an existing reference to a hash of parameters. I believe that this is the main data structure for this object.

Correct in both cases.

I've been looking at the rest of the code for new here. What does this do?

The for loop iterates through the keys of %self (in no particular order) — i.e., "key", "format", "model", etc. — assigning each in turn to $property. The if condition checks whether the current property is an entry in the user-supplied hash referenced by $param_hr:

But first, there is a check to ensure that the data type of the user-supplied entry matches the type expected by new(). This check is performed using the built-in function ref, which is documented as follows:

ref EXPR
...
Examines the value of EXPR, expecting it to be a reference, and returns a string giving information about the reference and the type of referent....

If the operand is not a reference, then the empty string will be returned. An empty string will only be returned in this situation. ref is often useful to just test whether a value is a reference, which can be done by comparing the result to the empty string. It is a common mistake to use the result of ref directly as a truth value: this goes wrong because 0 (which is false) can be returned for a reference.

— which is why I said above that the code is not entirely well-written: instead of testing directly against the empty string, the code first changes an empty string into the string 'String', which is not only unnecessary, but in fact is the “common mistake” mentioned in the documentation.

It would seem to check whether certain values are strings, but I don't understand the right hand side with
ref something || something else

No, it’s not checking whether values are strings, it’s checking whether values are references. (In this case, only the value associated with the headers key will actually be a reference.) The || (logical OR) operator checks its LHS first and returns it if it is true; otherwise, it returns the RHS. So in the lines:

my $type = ref $param_hr->{$property} || 'String'; my $expected_type = ref $self{$property} || 'String';

the first line assigns 'String' to $type if, and only if, the expression ref $param_hr->{$property} evaluates to a value which Perl considers “false.” If $param_hr->{$property} is not a reference, ref returns the empty string (""), which Perl does consider false, so the || operator evaluates to 'String'.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: using Getopt::Long to modify the default values in new by Athanasius
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.