Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Example from perlobj failing?

by HelenCr (Monk)
on Mar 15, 2013 at 04:23 UTC ( #1023615=perlquestion: print w/ replies, xml ) Need Help??
HelenCr has asked for the wisdom of the Perl Monks concerning the following question:

Dear esteemed PerlMonks

I am trying to run an example from perlobj (it's titled: "How SUPER is Resolved", about a third into the page here: http://perldoc.perl.org/perlobj.html).
Here is the script:

# taken from: http://perldoc.perl.org/perlobj.html use strict; use warnings; use 5.014; package A; sub new { return bless {}, shift; } sub speak { my $self = shift; $self->SUPER::speak(); say 'A'; } package B; use parent 'A'; sub speak { my $self = shift; $self->SUPER::speak(); say 'B'; } package C; use parent 'B'; sub speak { my $self = shift; $self->SUPER::speak(); say 'C'; } my $c = C->new(); $c->speak();

then compilation fails with:

Can't locate A.pm in @INC (@INC contains: E:\My Documents\Technical\Perl\Eclipse workspace\C files E:\My Documents\Technical\Perl\Eclipse workspace\OCR F:/Win7programs/Dwimperl/perl/site/lib F:/Win7programs/Dwimperl/perl/vendor/lib F:/Win7programs/Dwimperl/perl/lib .) at F:/Win7programs/Dwimperl/perl/lib/parent.pm line 20. BEGIN failed--compilation aborted at obj test.pl line 16.

My system: DWIM Perl 5.14.2 on Windows 7.

Your help will be appreciated

Many TIA - Helen

Comment on Example from perlobj failing?
Select or Download Code
Re: Example from perlobj failing?
by chromatic (Archbishop) on Mar 15, 2013 at 05:35 UTC

    This is a tricky error the first time you see it.

    The parent module tries to load parent classes from disk by default. Because all of your classes are in the same file, there are no A.pm, B.pm, or C.pm files to load, and that require fails. You can get around this with:

    use parent -norequire, 'A'; # and later use parent -norequire, 'B';

    ... or you could put each class in a separate file with the appropriate name, but that's more work for this example.


    Improve your skills with Modern Perl: the free book.

Re: Example from perlobj failing?
by NetWallah (Abbot) on Mar 15, 2013 at 05:43 UTC
    Its a bad example , but it works with the following tweaks:
    • Comment out "#$self->SUPER::speak();" in the "speak" method of A (It is a Base class - has no SUPER)
    • In Package B, Instead of "use parent A", do "use parent -norequire, 'A';"
    • In Package C, Instead of "Use parent B", use "use parent -norequire, 'B';"

    The reasons for the "-norequire" are explained in the parent module documentation.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Or use the deprecated module base instead, which "just works" in this situation.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Chromatic, NetWallah, tobyink: thank you for your explanations. Makes sense.

      For the benefit of PerlMonks users: here is a corrected example, with added printouts, which are cool.

      Many thanks - Helen

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-12-19 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls