Emeka wrote:
Hello All,
Hello,
What is the purpose of colon here ?
sub pop : method {
my $self = shift;
my ($list) = $self->_prepare(@_);
pop @$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(@_);
my $result = [sort { $a cmp $iterator->($b) } @$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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/