Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

use base 'Foo' or die!

by jeffa (Bishop)
on Feb 12, 2003 at 15:59 UTC ( [id://234716]=perlmeditation: print w/replies, xml ) Need Help??

The first time i saw a snippet like this:
use Foo qw(bar);
i was confused by the qw() operator. Why not just
use Foo 'bar';
instead? Because (as i later learned) using qw() is slightly more programmer-friendly by allowing you to add more exported methods:
use Foo qw(bar baz qux);
Which brings us to my meditation/rhetorical question. If you happen to be one of those OOP 'purists' who believes that multiple inheritance is bad or should be avoided (and i am not, by the way), shouldn't you always:
use base 'Foo';
instead of
use base qw(Foo);
? Silly question, but it is Wednesday after all. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: use base 'Foo' or die!
by Abigail-II (Bishop) on Feb 12, 2003 at 16:14 UTC
    Of course, qw isn't the only way of passing more than one argument to a module. The use Module statement takes a list. So,
    use Foo 'bar', 'baz';

    is just fine. The qw is just something "everyone" does. But not all the time. There is at least one module (which takes more than one argument) where "noone" uses qw for the argument list.

    I'll make it a quiz question: can you guess which module that is?

    Abigail


      strict? People tend to do:
      use strict 'refs'; no strict 'refs';

      As a (mild) style rule I try not to write use Module qw(foo) when I mean use Module 'foo' but I'm sure I've done it in the past. :-)

      --
      John.

        Just shows how fluid definitions can be. Constant is a common module for me, since I use it it about 75 percent of my production code.


        I just realized that I was using the same sig for nearly three years.

        Maybe, but that wasn't the module I was thinking of. The module I'm thinking of is used more commonly.

        Abigail

        Indeed. I was thinking of constant.

        Abigail

        When I said that the module takes more than argument, I meant that you were supposed to give at least two arguments. 'strict' and 'warnings' are often used without any arguments.

        Abigail

Re: use base 'Foo' or die!
by dws (Chancellor) on Feb 12, 2003 at 16:51 UTC
    If you happen to be one of those OOP 'purists' who believes that multiple inheritance is bad or should be avoided (and i am not, by the way), shouldn't you ...

    Because by writing

    use base qw(Foo);
    I'm saying that I know multiple inheritance is possible, but that I choose not not use it! Get thee behind me, Satan! Or maybe it's, uh, just muscle memory or something.

Re: use base 'Foo' or die!
by jryan (Vicar) on Feb 12, 2003 at 16:14 UTC

    And what if you're not an OO purist? :)

    And what if the module in question does not happen to be OO? I think that qw() became popular for use in module import lists. For instance, consider Storable, which exports a number of handy functions. It is common to see:

    use Storable qw(freeze thaw);
Re: use base 'Foo' or die!
by ihb (Deacon) on Feb 12, 2003 at 17:15 UTC

    I've asked myself that question too. OO-purist or not: why use qw// when you just want to quote one value? How many do print qw(Hello)? Not many, I'd guess. "Unnecessary" qw() seem to be most frequent where lists usually are passed, like array initializations, the vars pragma, import lists, etc, and there it's handy when you want to add something to the list. But as you say, most people do use it with the base pragma too, even though having a list of more than one element there would be the exception, not the rule. I can see a couple of possible explanations, in order of what I think is most likely:

    1. Cargo-cult. It's used in the pod for base.pm, perlboot, everyone else uses it, so people just copy it.
    2. Habit. People quite often use qw() with use() statements. I've even seen use Foo qw();.
    3. Some might have a twisted idea of what a list is. They think that you perhaps must "create" a list somehow, and since the documentation says use Module LIST then people "create" a list with qw().
    Update: I don't use qw() myself when I don't plan to have more than one element in the list. If I see use Base qw(Foo) in my own code I suspect I might be planning for MI.

    ihb

      I don't think it's any of those. I tend to use qw() for the same reason I tend to add a , after the last element of a long array. It's less work if I have to add something else at a later date.

        So tell me, how often do you have to inherit from another class at a later time?

        You seem to have missed the whole point of my post. Let me extract the key sentences in my already short reply: "... and there [with imports lists etc] it's handy when you want to add something to the list /.../ though having a list of more than one element there [with base.pm] would be the exception, not the rule"

        I tend to use qw() for the same reason I tend to add a , after the last element of a long array. It's less work if I have to add something else at a later date.

        Hmmm, is it more work to add the comma when you append to the list? It seems like it's the opposite. It's more work to add commas everywhere that I might not use. However, I use tailing commas too. And too out of laziness. But not because I want to add stuff later. Instead it's because if I want to reorder the list it's quite smooth if all lines can be treated equally when cutting and pasting.

        ihb
Re: use base 'Foo' or die!
by Hofmator (Curate) on Feb 12, 2003 at 16:56 UTC
    If you happen to be one of those OOP 'purists' who believes that multiple inheritance is bad

    I'm not so sure OO purists would agree with that statement. After all, e.g. Bertrand Meyer's Eiffel - a pure OO language - supports multiple inheritance.

    -- Hofmator

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found