Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Simple OO

by lzcd (Pilgrim)
on Feb 12, 2002 at 06:47 UTC ( [id://144793]=perlquestion: print w/replies, xml ) Need Help??

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

Ignoring complaints of cargo cult programming, can anybody shed light on why Perl doesn't seem to like this bit of code? It keeps looking for sheep.pm in the @INC and obviously not finding it.
package sheep; use strict; sub new { my $proto=shift; my $class=ref{$proto} || $proto; my $self={}; bless($self,$class); return $self; } sub bleet { my $self=shift; return 'Baaaa!'; } package main; use strict; use sheep; my wolf=new sheep; print wolf->bleet()."\n";

Replies are listed 'Best First'.
Re: Simple OO
by dws (Chancellor) on Feb 12, 2002 at 06:56 UTC
    If this code fragment is really in a single file, then you don't need   use sheep; Since the 'sheep' package is already in the symbol table. use is a bit confusing that way. The argument refers to a file, not a package. You have to trust that the file being use'd actually defines a package of the same name.

      Without the use statement I get:
      No such class wolf at demo.pl line 22, near "my wolf" syntax error at demo.pl line 22, near "my wolf="

        You're missing some $'s ...

        my $wolf=new sheep;
        print $wolf->bleet()."\n";

            --k.


        Without the use statement I get:
        No such class wolf at demo.pl line 22, near "my wolf" syntax error at demo.pl line 22, near "my wolf="
        The reason for this error message is a bit more obscure. (It has to do with something called "indirect object syntax".) However, the error goes away if turn the bareword wolf into the scalar $wolf.
        my $wolf=new sheep; ^
        I suspect this is what you had in mind.

Re: Simple OO
by Ryszard (Priest) on Feb 12, 2002 at 07:15 UTC
    Add this to the top of your test script (before you "use sheep")
    BEGIN {push @INC, <path to sheep.pm>}

    Interestingly enuff I had what seems to be a similar recently.

    HTH
Re: Simple OO
by Biker (Priest) on Feb 12, 2002 at 08:03 UTC

    Do you use warnings?
    Do you use strict?

    Update: Of course you use strict. It's in the code snippet. Doh.
    What I really wanted to ask was about the warnings. The question about strict just went in there by habit. ;-)


    Everything will go worng!

      Errr... yep to both.
Re: Simple OO
by lzcd (Pilgrim) on Feb 12, 2002 at 07:31 UTC
    Once again, thank you all. I now have working sheep... err... yes.
Re: Simple OO
by uwevoelker (Pilgrim) on Feb 12, 2002 at 17:13 UTC
    Shouldn't you use
    my $class = ref($proto) || $proto;
    Doesn't ref{$proto} return 'HASH'?
Re: Simple OO
by mortis (Pilgrim) on Feb 13, 2002 at 05:46 UTC
    Minor point: don't use lowercase package names.

    Rationale: pragmas are lower case - your package name might class with a future pragma. Which is one of the reasons all (most) of the modules on CPAN start with uppercase characters .

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found