Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Subroutine and object

by perlsyntax (Pilgrim)
on Nov 23, 2007 at 20:18 UTC ( [id://652655]=perlquestion: print w/replies, xml ) Need Help??

perlsyntax has asked for the wisdom of the Perl Monks concerning the following question:

I don't understand the 3 rules start out a program. in the book call Object Oriented, I do understand rule 1.How do you create a method and write a subroutine? And how do you create an object? P.S rem take it easy on me i a newbie with perl.

Replies are listed 'Best First'.
Re: Subroutine and object
by chromatic (Archbishop) on Nov 23, 2007 at 20:56 UTC

    There's no difference in declaring a method and a subroutine. The only difference is how you call methods and what arguments they get.

    sub some_function { my ($first, $second) = @_; ... } sub some_method { my ($self, $first, $second) = @_; ... }

    You need to call a method with a class name or an object:

    SomeClass->some_method( $first, $second ); $some_object->some_method( $first, $second );

    To create an object, call a constructor (a method called with a class name). A constructor uses the bless operator to associate a reference with a class name:

    package SomeClass; sub new { my $class = shift; bless {}, $class; } ... my $some_object = SomeClass->new();
      Does it matter if we start out with a subroutine or object?

        I don't understand the question. Start out how, as in appearance in the code or in which you should use first to solve a problem?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://652655]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-26 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found