Hi! Please consider following me on twitter: @hanekomu.
2008年09月18日
Using attributes for declarative programming
In declarative programming, you specify what to do, but not how to do it.
For example, in an SQL SELECT statement, you tell the database
engine what you want, not how it should lookup the information.
In jQuery as well, you bind events to elements; you don't have to write each basic handler for each element individually.
Perl 5 attributes can also help in making programs more declarative. Take
the :Overload attribute provided by Attribute::Overload. Normally you would use the
overload pragma like this:
package SomeThing; use overload '+' => \&myadd, '-' => \&mysub; sub myadd { ... } sub mysub { ... }
But if the subroutines that implement the overloads are further down in the module, you don't necessarily know that they are overloading an operation. That is because the declaration of which subroutines overloads what is separated from the implementations.
Using the :Overload attribute you can keep things together:
package SomeThing; use Attribute::Overload; sub myadd : Overload(+) { ... } sub mysub : Overload(-) { ... }
So declaring behaviour with attributes can keep definitions where they belong, that is, with the things they modify.
posted at: 14:17 | path: /dev | permalink | 0 comments | 0 trackbacks
Comments are closed for this story.
Trackbacks are closed for this story.