Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh Supreme Repositories of Perl Virtue,
I humbly beg you to demystify the following code I have just seen in Perl Catalyst. 12 years of programming in Perl has taken its toll, and my exhausted brain refuses to parse this code as Perl.
Kindly dispel my confusion, and enlighten me as to how this is Perl:
sub end : ActionClass('RenderView') {}

Replies are listed 'Best First'.
Re: How is this Perl?
by duelafn (Parson) on Aug 11, 2007 at 08:39 UTC

    As I understand it, attributes are syntactic sugar to do "stuff". Here's a random simple example:

    package Loud; use Attribute::Handlers; sub Loud :ATTR { my ($package, $symbol, $referent, $attr, $data, $phase) = @_; no strict 'refs'; # redefines subroutines given the "Loud" attribute *{$package.'::'.*{$symbol}{NAME}} = sub { # dup STDOUT and instead pipe things to another program open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: +$!"; open STDOUT, "| perl -pe 'tr/a-z/A-Z/'" or die "Can't redirect STD +OUT: $!"; $| = 1; # call the original subroutine $referent->(@_); # restore STDOUT close STDOUT; open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!"; } } # ... in another file or package package main; use base qw/Loud/; # We want this function to be loud sub foo : Loud { print "Purple is a nice color.\n"; } foo(); print "Don't yell!\n";

    The output of this program (it runs as shown above) is:

    PURPLE IS A NICE COLOR. Don't yell!

    Catalyst uses the ActionClass attribute here to append some action (provided by RenderView.pm) to your subroutine

    Good Day,
        Dean

      Is this like 'advice' in Lisp?

        Yeah, looks like it, though perl attributes can also be set on variables. Attributes may also be a bit more general/low-level since you get to (have to) perform your own function wrapping.

        Good Day,
            Dean

      Thank you very much! This was indeed educational. Living and learning. I do wish Perl would keep it simple, and not introduce too many esoteric constructs like this one. Let's stay true to the "P" in Perl!
        I do wish Perl would keep it simple...

        That depends on your definition of simplicity. Attributes offer amazing opportunities to simplify interfaces. Look at P5NCI::Declare for an example.

Re: How is this Perl?
by BrowserUk (Patriarch) on Aug 11, 2007 at 05:54 UTC

      [doc://perlsub#Subroutine-Attributes-attribute-subroutine,-attribute-attrs|Subroutine attributes] gives the link: Subroutine attributes.

      The easiest way to "discover" the link is to point your browser to perlsub, then click the TOC entry for the section you want to link to. The link can then be copied from the browser and the encoded characters cleaned up (maybe one day PM will handle the encoded chars so cleaning up's not needed).


      DWIM is Perl's answer to Gödel
      Well I've learned something new today. Never heard of attributes before. I read the perldoc, but I'm still confused about the benefit (or real function, for that matter) of attributes. Can somebody 'splain?

        Attributes is a convenient way to add additional semantics (meaning) to a subroutine or variable.

        For instance if you wanted to add function signatures to Perl subroutines you could do something like this.

        #subroutinge must always be called with to args: #a username and a password sub login_user : Signature( username, password ) { #do something to login a user. }

        At compile-time the subroutine Signature will be automatically called. This subroutine could basically do anything, but in the example it would probably parse the function signature and if it is valid, manipulate the symbol table by insertering a subrouting that does argument validation before calling login_user in place of the original login_user subroutine.

        A more thorough explanation of how to use attributes can be found here