Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

agent (5836)

agent
  agentzh@yahoo.cn
http://agentzh.spaces.live.com/

Agent Zhang (章亦春) is a happy Yahoo! China guy who loves Perl more than anything else.

Journal of agent (5836)

Tuesday November 29, 2005
09:23 AM

Tried out Perl6::Attributes

[ #27788 ]

If you have ever written any Perl OO code, I recommend you to take a look at Luke Palmer's CPAN module Perl6::Attributes, which is extremely simple and handy.

Instead of writing something like

    sub method {
        my $self = shift;
        $self->{attr} = '...';
    }

we can now write

    sub method {
        my $self = shift;
        $.attr = '...';
    }

in Perl5.

Another example is replacing the ugly syntax

    sub method {
        my $self = shift;
        @{$self->{attrs}} = qw(a b c);
    }

with the Perl6-ish syntax

    sub method {
        my $self = shift;
        @.attrs = qw(a b c);
    }

You see, it is pretty cool!

I've already had a try in my recent Perl module developements. Perl6::Attributes makes the source of my Perl OO modules amazingly neat.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Or you can use Lexical::Attributes and you can write:
    use Lexical::Attributes;

    class MyClass;
    has ($.foo, @.bar, %.baz) is rw;
    sub new {bless \do {my $v} => shift}
    method something {
        # Look, no '$self = shift' needed.
        $self -> whatever ();
        return $.foo + $.bar [1]
    }
    • Is Lexical::Attribute more like Perl6 attributes than Perl6::Attributes is?
      --
      Bill
      # I had a sig when sigs were cool
      use Sig;
      • I don't know. To be perl6ish was only a minor issue for Lexical::Attributes. And in certain ways, it deviates on purpose (private attributes also use a dot as a secondary sigil - I don't share Larry's opinion that it's important to have a visual difference between private and non-private attributes)
    • Thank you for your advice, Abigail.

      Yep, Lexical::Attributes seems to be more Perl6-ish and hence even more revolutionary.
      But it also deserves more effort to get used to the syntax in Perl5 modules. :=)