Re: OO Perl Problems
by broquaint (Abbot) on Sep 13, 2002 at 14:52 UTC
|
Files that are requireed or useed need to return true. So the last statement in your file should evaluate to true e.g
package foo;
# code here
1; # or anything else that evaluates to true
Also are you sure you know what your doing with ref($type) || $type as it severely indicates cargo cult programming which is rather a contentious issue around here. But if you know exactly what you're doing then best of luck to you :)
HTH
_________ broquaint | [reply] [d/l] |
|
|
broquaint,
Thanks for the help. I've been looking at the ref($type) || $type thing and I am probably guilty of cargo cult programming in this case!
"Rule #17 of Travel: Never try and score dope off Hassidic Jews while under the impression that they are Rastafarians."
- Pete McCarthy, McCarthy's Bar
| [reply] [d/l] |
Re: OO Perl Problems
by demerphq (Chancellor) on Sep 13, 2002 at 16:00 UTC
|
The standard is to name the sub "new" not "New".
And personally I dont think the || ref trick is bad, and the document you are studying explains it in detail. But broquaint is correct it is the subject of debate around here on occassion. And hes correct that it can bite you, but IMO normally doesnt.
update: Also, use the syntax that tadman shows, not the indirect object syntax my $obj=new Class as it will bite you eventually.
--- demerphq
my friends call me, usually because I'm late....
| [reply] [d/l] |
|
|
my $obj= new Class::;
and the trailing button operator removes any ambiguity in the indirect-object form. | [reply] [d/l] |
|
|
my $obj=Class->new;
Is much nicer to read IMO, especially when arguments are needed for the method call. (but thanks for pointing that out.... Learn somthing new every day.)
--- demerphq
my friends call me, usually because I'm late....
| [reply] [d/l] |
|
|
|
|
Re: OO Perl Problems
by tadman (Prior) on Sep 13, 2002 at 15:55 UTC
|
Like broquiant said, each module has to finish with a "success" value. Normally, you just put 1; at the end of the module to indicate everything is okay. You could, theoretically, return 0 if something awful happened.
I'd just like to point out that conventionally, functions like new() are specified in lower-case. The call would be made like this:
my $object = Texas->new();
You can call your subroutines anything you like, of course, but it can be frustrating to others trying to use your module if you choose a non-standard approach. | [reply] [d/l] |
Re: OO Perl Problems
by mojotoad (Monsignor) on Sep 13, 2002 at 16:09 UTC
|
| [reply] |
Re: OO Perl Problems
by BigD (Scribe) on Sep 13, 2002 at 14:54 UTC
|
Hi!
You are not wrong at all, just do what the message tells you: add a tru value at the end of your module.
All modules in Perl (*.pm files) must return a true value at the end, so in your module the last line of your code must be 1;
| [reply] [d/l] |
|
|
Actually the norm is to use '1' only because this will not produce warnings if you do
perl somepackage.pm
But you can use any true value you like, the warnings will not be generated when the line is encountered in the context of a use or require. For instance I sometimes put "by demerphq"; as the last statement in the file.
--- demerphq
my friends call me, usually because I'm late....
| [reply] [d/l] |
Re: OO Perl Problems
by Flexx (Pilgrim) on Sep 13, 2002 at 19:29 UTC
|
Hi,
just to make this complete, the place you would have had to read was perlmod. It mentions the return true thing. I think perltoot could use a "see also" pointer to perlmod. | [reply] |
|
|
Hi,
If you read perltoot carefully it is mentioned at the end of multiple code listings like this;
1; # so the require or use succeeds
Also, I failed to find reference to it in perlmod? Can someone point it out to me in there?
With respect,
Neil
| [reply] [d/l] |
|
|
Sorry, I should have in the first place, but I didn't know how to do that using [brakets]...
Outch! I just realized it's not in the onsite perlmod (which is ages old ;). I was actually looking at http://www.perldoc.com/perl5.6/pod/perlmod.html#Perl-Modules. At the end of the long code example/template in the Perl Modules section, you'll find the hint.
Indeed, I believe this should be mentioned in a much more prominent place. It should be in the text somewhere, not just in the examples. Thinking back, I can't remember how I learned about it...
Cheers,
Flexx
| [reply] |
|
|
Hi Nemp,
It does indeed say this, however I printed it out for bedtime reading and the main bit of code I was studying is spread over two pages so I missed it. On later examination after reading the above again, it got found.
Thanks, Elgon
"Rule #17 of Travel: Never try and score dope off Hassidic Jews while under the impression that they are Rastafarians."
- Pete McCarthy, McCarthy's Bar
| [reply] |