subroutines vs method vs function

Discussion in 'Perl Misc' started by BH, Feb 27, 2008.

  1. BH

    BH Guest

    Hi,

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

    Regards,

    BH
    BH, Feb 27, 2008
    #1
    1. Advertising

  2. BH <> writes:

    > 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

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Feb 27, 2008
    #2
    1. Advertising

  3. BH

    Uri Guttman Guest

    >>>>> "B" == BH <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 27, 2008
    #3
  4. Joost Diepenmaat <> writes:

    > IOW there methods as such, just functions that get called using method
    > call semantics.


    That should read "there are no methods as such"

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Feb 27, 2008
    #4
  5. BH

    Guest

    BH <> wrote:
    > 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.
    , Feb 28, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    436
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. MackS
    Replies:
    0
    Views:
    453
    MackS
    Mar 11, 2005
  3. tshad

    Global subroutines

    tshad, Jan 24, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    364
    IPGrunt
    Jan 26, 2005
  4. fd123456

    Global subroutines

    fd123456, Jan 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    390
    tshad
    Feb 4, 2005
  5. Wijaya Edward

    Benchmarking Python's subroutines/function

    Wijaya Edward, Oct 3, 2006, in forum: Python
    Replies:
    1
    Views:
    349
    Daniele Varrazzo
    Oct 3, 2006
Loading...

Share This Page