subroutines vs method vs function

J

Joost Diepenmaat

BH said:
Hi,

Can someone clarify the 3 terms wrt Perl providing with some examples?

they're all subroutines. functions and subroutines are the same thing in
perl.

methods are subroutines that are called using method resolution and get
the object passed as the first argument. IOW there methods as such, just
functions that get called using method call semantics.

package Bla;
sub something { print "my arguments are '@_'\n" }

Bla->something(); # call as (class) method

something(); # call as function

Bla::something(); # call as function with explicit package name

my $o = bless {},"Bla"; # make a "Bla" object
$o->something(); # call as object method
 
U

Uri Guttman

B> Can someone clarify the 3 terms wrt Perl providing with some examples?

put your question in the BODY of your message. then it reads properly
and it is easier to write replies.

subroutines vs method vs function

subs are perl level routines you code and call.

sub bar {
print "bar was called\n" ;
}


functions are things built into perl. read perldoc perlfunc for a list
and description of them all.

methods are just perl subs that are called via an object or a class
using a object oriented call. read perldoc perlobj for more on that.

uri
 
J

Joost Diepenmaat

Joost Diepenmaat said:
IOW there methods as such, just functions that get called using method
call semantics.

That should read "there are no methods as such"
 
X

xhoster

BH said:
Hi,

Can someone clarify the 3 terms wrt Perl providing with some examples?

"subroutine" and "function" are basically interchangeable. There is a
tendency to use "function" for built-in ones and "subroutine" for
user-defined ones, but that is a tendency, not a law.

Methods are subroutines invoked in an object-oriented way.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top