Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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:
Here is the script:

# taken from: 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 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/ line 20. BEGIN failed--compilation aborted at obj line 16.

My system: DWIM Perl 5.14.2 on Windows 7.

Your help will be appreciated

Many TIA - Helen

Replies are listed 'Best First'.
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,, or 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 (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023615]
Approved by vinoth.ree
[choroba]: 20 months? :-o
[Discipulus]: up to 20 months.. but you'll be sure to remain unemployed

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2018-05-23 20:58 GMT
Find Nodes?
    Voting Booth?