Wednesday, February 13, 2008

Usage of perl attributes in functional programming in perl

started 13.02.2008|22:08

The idea of this article is to present basic usage of perl function attributes when programming in perl.

Official doc on attributes is in


perldoc perlsub
perldoc attributes


Two perldoc's above describe syntax of attributes. However, for the moment, they lack description of how they can be used. It seems from the results achieved from the test written, that they can be very useful for functional programming in perl.

Simple example below, presents "Hello World" in perl with attributes.

Perl 5.8.8 was used.

"attribute" for function, is basically something that can be added to function to modify it.

You can define following subs:


-------------%<--------------
sub _good ...

sub _hello ...
{
...
print "Hello World from $package\:\:$name \n";
$owner_of_attr->( @_ );
...
}

...
sub greatcode : _hello, _good
{ print "I am great code."; }

...
------------->%--------------


Attribute functions then modify code of the 'greatcode' function at the moment of the function definition.

After running the full code in perl (just store it and run in your perl without arguments) You will get:
...
Hello World from main::greatcode.
I am great code.
...

among other results.

<------>

finished for publish 14.02.2008|01:30
(this notes in italic just for my time-management procedures; blog eats a lot of time; however, without blog a lot will be lost in the mind.)

No comments: