Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

OO Perl Problems

by Elgon (Curate)
on Sep 13, 2002 at 14:43 UTC ( #197600=perlquestion: print w/ replies, xml ) Need Help??
Elgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

I'm trying to teach myself the ins and outs of OO Perl and I'm having problems. In reference (excuse the pun) to my Texas Hold'ems thread, I'm trying to write a class which will allow me to reuse some of the code and I'm getting problems.

We have the following code...

#!/usr/bin/perl -w use strict; package Texas; sub New { my $type = shift; my $class = ref($type) || $type; my $game = { hands => [], flop => [], deck => [] }; bless ($game, $class); return $game; }
But when I say...

my $object = New Texas;

...I tend to get an error which says that Texas.pm doesn't return a true value. Please point out where I am going wrong.

For the record, I have tried looking this up in perltoot and similar but to no avail. I am using Perl 5.005_03.

TIA, 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

Comment on OO Perl Problems
Select or Download Code
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

      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

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;
      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....

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.
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....

      write
      my $obj= new Class::;
      and the trailing button operator removes any ambiguity in the indirect-object form.
        Blech.
        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....

Re: OO Perl Problems
by mojotoad (Monsignor) on Sep 13, 2002 at 16:09 UTC
    Another observation: names of procedures and methods are generally all lower case. There's nothing syntactically incorrect about mixing case in method names, it's just not common practice.

    See the Style Guide for more info.

    Matt

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.
      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

        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

        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-08-29 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls