Named subs are also used as class or object methods. The only difference between a method and a named subroutine is the first argument passed: if you call methods on an object the first argument passed will be the object and if you call a method on a class the first argument will be that class (i.e. the name of the package).
See also perlsub and perltoot.package MyPackage; sub new { return bless {},shift; # create an empty object of class MyPackage } sub called { my (@args) = @_; print "called with args @args\n"; } package main; MyPackage::called(1,2,3); # call as a normal subroutine MyPackage->called(1,2,3); # call as a class method my $object = MyPackage->new(); # create object $object->called(1,2,3); # call as an object method.
Update: and subroutines always return whatever is passed to return() or failing that, the value of the last expression.
In reply to Re: Global Functions? sent and returned values
by Joost
in thread Global Functions? sent and returned values
by barrycarlyon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |