As others have said, the code provides a default anonymous sub for the 'Post' attribute of $self. Since the anonymous sub aspect of this has been well covered, I'll comment on the default value side of things, with a slight detour into 'or' vs '||';

The code snippet you posted threw me off balance for half a second--setting default values is usually done using '||'--the high precedence logical or operator.

Perl has two logical or operators, '||' and 'or'. They identical except in terms of precedence. '||' has a high precedence and 'or' has a low precedence.

$foo = $bar || 'baz'; # is identical to $foo = ( $bar or 'baz');

Common usage for each operator:

open( $fh, '>', $filename ) or die "Unable to open file: $!"; my $foo = $bar || 'baz';

Now, lets look at a couple ways to set a default value:

# Original code $self->{'Post'} = ($args{'Post'} or sub {1;}); # High precedence or $self->{'Post'} = $args{'Post'} || sub {1}; # Ternary operator $self->{'Post'} = $args{'Post'} ? $args{'Post') : sub {1}; # Using '||' for a numeric value. $self->{'Number'} = $args{'Number'} || 10;

This idiom is a common, compact, and readable way to set default values. However there is a trap for the unwary here. If the value you are testing may legitimately evaluate to false, your default value will be spuriously assigned. Take a look at the code below for an example of this bug.

foo(); foo(23); foo(0); sub foo { my $number = shift || 10; print "My number is $number\n"; } __END__ #OUTPUT: My number is 10 My number is 23 My number is 10

So, if you use this idiom, make sure you understand how your acceptable values are treated in boolean context (whether they evaluate to TRUE or FALSE).

Update: as DrHyde points out, if you are using Perl 5.10, the truly lovely defined-or operator solves this problem very nicely.

foo(); foo(23); foo(0); sub foo { my $number = shift // 10; print "My number is $number\n"; } __END__ #OUTPUT: My number is 10 My number is 23 My number is 0


TGI says moo


In reply to Re: what is sub {1;} by TGI
in thread what is sub {1;} by mlgvalt

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.