in reply to Re^2: OO Perl: Nested classes question
in thread OO Perl: Nested classes question

Perl classes work by what has been defined in the right package. By tradition you put one class per file, but Perl does nothing to enforce that tradition.

Looks like I learned something new again.

(But use won't work properly unless you do that, so it is a good tradition to follow.)

And that's probably why I was thinking the one class per package thing is required.

Also I note that your "preferred way to call constructors" comment is just cargo culting unless you can give some concrete reasons why that is preferred. I'm betting that you can't.

I used to know why :) As best as I remember it had something to do with inherited constructors not working properly. (But you're right, since I can't really remember why anymore it is a cargo-cult thing now.)

Also what is the reason that you're afraid of using unless?

I strongly dislike the action appearing before the condition. I find it to be a lot more prone to causing confusion (for me) and decreasing readability. (And yes I know there's plenty of people who argue it does exactly the opposite.)

Replies are listed 'Best First'.
Re^4: OO Perl: Nested classes question
by tilly (Archbishop) on Sep 15, 2006 at 02:19 UTC
    I think we're now on the same page on most of this, but there is one point we're not.

    Also what is the reason that you're afraid of using unless?
    I strongly dislike the action appearing before the condition. I find it to be a lot more prone to causing confusion (for me) and decreasing readability. (And yes I know there's plenty of people who argue it does exactly the opposite.)
    This isn't what I was talking about. I asking about writing
    if (! defined $self->{_condition}{$condition}) { $self->{_condition}{$condition} = Condition->new(); }
    versus
    unless (defined $self->{_condition}{$condition}) { $self->{_condition}{$condition} = Condition->new(); }
    There are arguments both ways, I was wondering what your arguments were. (Basically my belief is that if you're going to offer advice, you should be able to back it up.)

      Note that I never specifically gave advice about if versus unless (not intentionally anyway)

      Silly as it may seem, I've always assumed unless could only be used as a post-condition.

      That being said... even now that you've shown me, I'll probably still keep on using just if, for a few reasons:

      • if is more universal. If I start using unless in Perl, it's one more thing to remember not to use in bash or C. Now of course you can extend that argument and say "well then why do you use hashes, they aren't there in C either", but the difference there is that hashes are one of the core strengths of Perl, where unless is really no more than syntactic sugar.
      • The strong visual cue that ! provides. Maybe it's because I don't use unless much, but I always have to think twice when I see it to remind myself that the following condition is essentialy negated
      • English style. This may be a regional or native language thing (my native language is Dutch), but I don't think I've ever started an English sentence with "unless"; whenever I use it it's in the "<something> will happen unless <condition>" kind of format.

        For me ! is not a useful visual tool at all. The rest sounds very reasonable.

        Allow me to add to the list the observation that humans don't do a good job of doing De Morgan's Laws in practice. When you're writing it, it is easy to make the statement mean what you want it to mean. But when you are tracing through code later, it is surprisingly difficult to figure out whether you enter a condition that reads unless (A or B) {...}

        And yeah, when I was first told that I thought it was nonsense. It was not until I was having trouble debugging something at 11 PM that I realized it was true. Therefore I use unless very, very sparingly.