Hello everyone,
I'm doing some comparing between the usage of class and module. I declare a class named Foo with a instance method called hello as the following:
1 package Foo;
2
3 __PACKAGE__->hello('test1');
4 hello('test2');
5
6 sub hello {
7 my ($self, $arg) = @_;
8 print "hello=". $arg. "\n";
9 }
10 1;
And the result is like this:
arg=test1
Use of uninitialized value $arg in concatenation (.) or string at line
+ 8
I am a bit confused by the usage of __PACKAGE__. In terms of the result, I guess line3 is calling a object method but don't see any new() syntax. Looks like line4 is calling a regular subroutine and that's why the error happens. Am I right about this?
Under which kind of situation should we use __PACKAGE__ to create an object instead of calling its new() in the caller program?
Thank.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.