I was poking around in yet another odd corner of Perl, attributes.pm, and I noticed an interesting possibility. According to the pod, the statements,

=pod package Canine; package Dog; my Canine $spot : Watchful ;
should have the effect,
use attributes (); attributes::->import(Canine => \$spot, "Watchful"); =cut
If all is well, &attributes::import calls Canine::MODIFY_SCALAR_ATTRIBUTES(Canine => \$spot, 'Watchful') to handle the attribute bookkeeping.

"Well," I thinks, "Don't that look just like a constructor?"

If MODIFY_SCALAR_ATTRIBUTES() is written as a constructor, then my Dog $spot; could call it on $spot and give us a dog with that purely declarative syntax. So here goes . . .

I'll simplify the example a little bit, making only one canid class,

use warnings; use strict; package Dog; sub MODIFY_SCALAR_ATTRIBUTES { my $class = shift; my $ref = shift; my %dog; @dog{@_} = (1) x @_; $$ref = \%dog; bless $$ref, $class; print "Woof!\n"; (); }
The bark is to let us know that the constructor has been called. This is an experiment, after all, and experiments need instruments.

For a demonstration, we don't need any more methods than that. Time to test drive:

package main; my Dog $spot :Watchful Muddy; my $izzie = ref $spot; print $izzie? "$izzie Spot is @{[keys %$spot]}": "No Spot", $/;
Which faithfully prints:

Woof!
Dog Spot is Muddy Watchful

This is nifty all right, but it turns out to have a problem. I'd like to call the constructor with just,

my Dog $rover;
Perl accepts that, but there's no "Woof!" when it runs. No Rover. Instrumenting a private copy of attributes.pm shows that &attributes::import is never called when the attribute list is empty. That's not what I expected, but I can't find any promises in the docs to say it should. In fact, that's very like the behavior of use with no import list.

The empty attribute list would be no problem for attributes.pm. Its import() function is structured to work with that.

Having that syntax available would make very happy those people who like strict typing, and I don't think it would rock the structure of Perl much. I haven't yet gone source diving to see what's needed to make that happen. Does anybody have a feel for that?

After Compline,
Zaxo


In reply to my Dog $spot; by Zaxo

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.