My understaning is that Foo->hello & hello are basically the same except the former is calling class method and passing 'Foo' to hello as the first arg, but the later is calling a subroutine and will not pass 'Foo'. How do you think?
I think you understand, but your wording leaves to be desired. The same sub is called in both cases; the difference is in how it's called:
Foo->hello & hello are basically the same except the former is calling hello as a class method and passing 'Foo' (or whatever's left of the arrow) as the first arg, but the latter is calling hello as a subroutine and will not pass 'Foo'.
One difference that wasn't mentioned is that inheritance will come into play when hello is called as a method.
{ package Foo; sub hello { ... } }
{ package Bar; our @ISA = 'Foo'; }
Foo->hello('world'); # Foo::hello('Foo', 'world')
Bar->hello('world'); # Foo::hello('Bar', 'world')
Foo::hello('world'); # Foo::hello('world')
Bar::hello('world'); # hello not found in Bar
I think the purpose of these class method calls is to initialize some class variables
And maybe even create subs, yes.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|