in reply to use base 'Foo' or die!

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

Replies are listed 'Best First'.
Re^2: use base 'Foo' or die!
by adrianh (Chancellor) on Feb 12, 2003 at 17:19 UTC

    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
        So tell me, how often do you have to inherit from another class at a later time?

        A lot more often than it seems I should, actually. I do a lot of rapid OO prototyping, often before I want to propose a new design. As such, I don't have a design laid out and agreed upon yet. So, I'll try different things, move things around, try and figure out where things really want to live vs. where I think they should live.

        How often have I used MI? Less than a handful. How often have I thought of using MI during rapid prototyping? Too often to count.

        That's why I use base qw(Foo);

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        I wouldn't characterize adding the comma at the end of the list to be work saving because it saves you adding the comma later.

        What it saves you is editing the file twice when you do a line copy in your favorite editor (Yp in vi, for example,) to add that most recently discovered forgotten item and wind up with:

        my %blah = ( one => 1, two => 2, three => 3 four => 4 );

        And back you go for a single comma. If the last line has a comma, you can yank any line you want and can be sure it is safe to paste a copy back in. Adding the comma is for us non-perl-parsing-robot types who suffer from excessive human flaws. :) :)

        See page 74 of the 3rd Camel, Larry and Tom thought to mention the benefit of null list items and optional terminating commas!

        Update: ihb points out I skimmed the last couple lines from his post and missed that he does tailing commas for reordering. Oops. Still, my point goes to reordering the same as his, the tailing comma is 2nd order laziness: microscopic extra work now can save you quite a bit more work later. Adding the comma is essentially cost-free, but leaving it out means editing multiple lines when you edit, add, reorder, or remove from any list.

        --
        $you = new YOU;
        honk() if $you->love(perl)

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

        Fairly often - that's why I said it ;-)

        I find that I add MI (usually only temporarily) during refactoring. A class pops out from another class, becomes an inherited subclass for a bit, and usually ends up in a has-a/implemented-with relationship in the end. So I can start out with single inheritence, move to multiple, and come back to single again.

        Using qw() consistantly makes things easier for me.