in reply to Thoughts on naming loop LABEL:

I'm curious as to why you use a singular noun for a hash, which is a collection of things? I use a plural.
   $users{fred} and %users
sound better to me than:
   $user{fred} and %user

The exception to this is when I'm using a hash to represent attributes of an object. ie:
   $user{ADDRESS}, $user{PHONE}

-pete
"Pain heals. Chicks dig scars. Glory lasts forever."

Replies are listed 'Best First'.
Re: (2) Thoughts on naming loop LABEL: (Why I name hash vars singular. Why label loops?)
by ybiC (Prior) on May 02, 2002 at 14:41 UTC
    Good question, dreadpiratepeter - I suppose it's largely a matter of taste.   I only meant to preface my question with examples of my currently preferred naming style, not say that mine is necessarily better than any others.   I'm sure there are other threads on the matter, but only have time for a superficial Search, which turned up Sinister's recent Plural variable naming (or not?).

    I forgot to ask the probably greater question that AM points out below - *why* would one choose to LABEL: a loop?

        cheers,
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")

    Update: Now that I've a bit of time, I'll try to es'plain my naming preference of singular-for-a-hash-var:

    It seems to boil down to what looks more clear in the context of how I most commonly employ the variable types in my code.   In the following bogo-code, each $hash{key} is dealt with as an individual (singular) item.

    my %param = ( srcdir => '/home/me/perl/', destdir => '/root/perl/', moduledir => 'bar-foo', tarball => 'bar-foo.tgz', ); cd $param{srcdir}; system /usr/bin/tar $param{tarball} &param{moduledir} -zcvf and die $! +; system /bin/mv $param{tarball} $param{destdir} and die $!; cd $param{destdir}; system /usr/bin/tar $param{tarball} -zxvf and dir $!

    Then in this next kind of hash usage, I still think of each $hash{key} as an individual item.   And in the loop, the "keys" conveys the plurality of $hash{key}'s within the greater %hash.   At least to me.   Heh.

    my %info = ( ' script' => "$0", ' executable' => "$^X $]", ' hostOS' => "$^O", ' starttime' => "$^T", ); for (keys %info) { print $_ $info{$key}, "\n"; }

      Labeling can be very useful. Consider this:

      @array = (# some values that could contain an error) MAIN:while(1) { # something to keep this non-existant # program running foreach(@array) { last MAIN if($_ eq 'some error'); } }

      I hope thats a good example. (I know there are better ways to do what I just did but just wanted to show how LABELs can be used). They can also be useful for just seeing what loop you are in if you have several nested

      Some clever or funny quote here.
      Chris

      Actually I disagree with AM below (So why am I replying to you? TIMTOPTPI ;-) I think judicious use of labels helps code legibility a great deal, even when the loops aren't nested.

      For example, when I'm processing a result set from a database, I often label the loop RECORD: or ROW:. That way, after some long block of code, I can write next ROW; to make it clear I'm short-circuiting the rest of the logic. It's the same reason I choose variable names carefully: it helps me (five years from now when I'm re-reading it) keep track of what's going on.

        Do you label all such blocks or loops to make it clear that you are short circuiting the loop when you have a next, last, or redo?

        LINE: while(<>) { # ... next LINE if ... # ... } SEARCH: for (@list) { # ... last SEARCH if ... }

        Do you do so only in long blocks? Complicated blocks? Do you have a consistent style for when and how to label loops or is it more of a "whatever seems good at the time" approach? I ask not to be difficult but because in a single non-nested loop I just don't see any additional clarity being added by loop labels. If you see a benefit can you describe the situations where you find it beneficial versus those where it is ok to not label the loop?