Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Defining classes and inheritance using packages within a single .pl file, without creating modules

by bart (Canon)
on Nov 29, 2006 at 11:33 UTC ( [id://586658]=note: print w/replies, xml ) Need Help??


in reply to Re: Defining classes and inheritance using packages within a single .pl file, without creating modules
in thread Defining classes and inheritance using packages within a single .pl file, without creating modules

In general, I agree with your excellent advice, jbert, except for this minor point:
However, I'd personally do neither of those. The best way these days to declare inheritance is use base, which also has the handly property of being a compile-time construct.
IMO base is a crap module, it tries to handle both loading external files and inline packages, but it tries Too Much Magic™ and in some cases it may fail. It's failed many times for me in the past. I don't trust it any more.

As a result, you may have to reorder the definition for your packages and possible even add a $VERSION variable (in an inline package!). All that to please base. Yuck.

I wish we could bypass the magic, and explicitely tell it to load a module, or just skip that phase.

If it wasn't for that annoyance, base would indeed work very well, for the reasons you give.

  • Comment on Re^2: Defining classes and inheritance using packages within a single .pl file, without creating modules
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Defining classes and inheritance using packages within a single .pl file, without creating modules
by jbert (Priest) on Nov 29, 2006 at 12:06 UTC
    That's interesting - and serves as an additional data point that consensus on current perl OO "best practice" is a bit limited at the moment. Yes, TIMTOWDI, but that can be confusing for newcomers.

    Do you think that these tutorials are still current? (Actually, I should have linked to these at first, sorry about that).

    By the way, would you care to elaborate on base's failure modes? Since you've caused me to look more closely, I can see how it might hurt if you're defining a module inline with the same name as one available as a seperate file via require - is that the main issue?

      I'm not sure how base has evolved over the years, but I've had problems where it died complaining it couldn't locate a module where the package was inline in the script further down, and it may indeed load an unwanted module of the same name (B is a favourite example) under similar circumstances. Fun for the whole family.

      Checking $VERSION in a package, to check if the package exists, is not the best way to tackle the problems, IMO.

        Don't blame base. If you're going to inline modules that use base, do it properly.
Re^3: Defining classes and inheritance using packages within a single .pl file, without creating modules
by adrianh (Chancellor) on Nov 30, 2006 at 01:15 UTC
    IMO base is a crap module, it tries to handle both loading external files and inline packages, but it tries Too Much Magic™ and in some cases it may fail. It's failed many times for me in the past. I don't trust it any more.

    Use it all the time. Never had a problem with it :-)

    I don't really think it's that magical. Two rules:

    • If the package is already loaded - use it
    • Otherwise, try and use it.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-16 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found