in reply to Re: RFC: Simulating Python's @decorators in Perl with Attributes
in thread RFC: Simulating Python's @decorators in Perl with Attributes

I think the essential point about attributes is to DRY when marking subs and variables.

so this code might be explicit but repeats the subname hello as a string.

around 'hello' => \&makebold;

Please note that attributes are not necessarily wrapping code.

One use-case for instance could be to set a breakpoint for the debugger

sub tst :break { ... }

Another to restrict the type to variables

my $y :Int;

which might be very helpful when trying to translate Perl code to a typed language like C.

All use cases could be achieved by adding tst or $y to a hash somewhere, but the possibility to do it right at the point of interest w/o name repetition can be very handy and avoids headaches when refactoring.

Just imagine the overhead for a refactoring tool to find all hellos after sub hello was renamed.

Cheers Rolf

( addicted to the Perl Programming Language)

update

I'm no expert of Moose's compile phases, but a DRY notation of around using attributes would look like

sub hello :around(makebold) { return "ohai"; }

Replies are listed 'Best First'.
Re^3: RFC: Simulating Python's @decorators in Perl with Attributes
by pokki (Monk) on May 28, 2013 at 19:54 UTC

    All those other uses for attributes are useful and interesting, but I thought we were talking about method decoration?

    Decorators as code rather than syntax also allow you to decorate someone else's methods, or to compose some advice into some class through roles, even at runtime. This is not possible with attributes.

      Could it be that you are confusing the meaning of "decorator"?

      The term "decorator" in the Python universe has not much to do with the "decorator pattern".

      see also SO:Whats the difference between Python decorators and Decorator Pattern?

      The initial motivation to look again into Perl attributes was a discussion about Python decorators.

      While Perl attributes are more powerful, they are also more difficult to handle.

      Cheers Rolf

      ( addicted to the Perl Programming Language)