I am going through github perl code

--000e0cd31332b261ca04aa3cef86
Content-Type: text/plain; charset=ISO-8859-1

Hello All,

What is the purpose of colon here ?

sub pop : method {
my $self = shift;
my ($list) = $self->_prepare( [at] _);

pop [at] $list;
my $result = $list;

return $self->_finalize($result);
}
Is this how to do function alias?

sub sortBy {&sort_by} #

sub sort_by {
my $self = shift;
my ($list, $iterator, $context) = $self->_prepare( [at] _);

my $result = [sort { $a cmp $iterator->($b) } [at] $list];

return $self->_finalize($result);
}



--
*Satajanus Nig. Ltd


*

--000e0cd31332b261ca04aa3cef86--
Emeka [ Do, 11 August 2011 18:03 ] [ ID #2063244 ]

Re: I am going through github perl code

Emeka wrote:
> Hello All,

Hello,

> What is the purpose of colon here ?
>
> sub pop : method {
> my $self = shift;
> my ($list) = $self->_prepare( [at] _);
>
> pop [at] $list;
> my $result = $list;
>
> return $self->_finalize($result);
> }

perldoc perlsub

SYNOPSIS
To declare subroutines:

sub NAME; # A "forward" declaration.
sub NAME(PROTO); # ditto, but with prototypes
sub NAME : ATTRS; # with attributes
sub NAME(PROTO) : ATTRS; # with attributes and prototypes

sub NAME BLOCK # A declaration and a definition.
sub NAME(PROTO) BLOCK # ditto, but with prototypes
sub NAME : ATTRS BLOCK # with attributes
sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes

To define an anonymous subroutine at runtime:

$subref = sub BLOCK; # no proto
$subref = sub (PROTO) BLOCK; # with proto
$subref = sub : ATTRS BLOCK; # with attributes
$subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes


The colon separates the sub name from the attributes.


> Is this how to do function alias?
>
> sub sortBy {&sort_by} #
>
> sub sort_by {
> my $self = shift;
> my ($list, $iterator, $context) = $self->_prepare( [at] _);
>
> my $result = [sort { $a cmp $iterator->($b) } [at] $list];
>
> return $self->_finalize($result);
> }

perldoc perlsub

[SNIP]

Because assignment of a reference to a typeglob creates an
alias, this can be used to create what is effectively a local
function, or at least, a local alias.

{
local *grow = \&shrink; # only until this block exists
grow(); # really calls shrink()
move(); # if move() grow()s, it
shrink()s too
}
grow(); # get the real grow() again

See "Function Templates" in perlref for more about manipulating
functions by name in this way.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Do, 11 August 2011 19:04 ] [ ID #2063245 ]
Perl » gmane.comp.lang.perl.beginners » I am going through github perl code

Vorheriges Thema: A beginners question regarding Perl on Windows 7
Nächstes Thema: Search and replace w/ ignoring arbitrary characters