Duh v D'oh

Abigail abigail at abigail.be
Fri Nov 7 15:41:47 GMT 2008

On Fri, Nov 07, 2008 at 09:45:34AM -0300, Eden Cardim wrote:
> On Thu, Nov 6, 2008 at 1:33 PM, Paul Makepeace <paulm at paulm.com> wrote:
> > Following on the theme from a conversation with Mark at Dim Sum today*, spot
> > the mistake I just wasted an hour on,
> >
> >  my $publication_stats = [['Source', 'ID Publication', 'Article count']];
> >  foreach my $given_source (%publication_map_by_name) {
> >    push @$publication_stats, [$given_source,
> >                               $publication_map_by_name{$given_source},
> >                               $article_count_by_source{$given_source}];
> >  }
> >
> > If it's not staring out at you, you're possibly also a python programmer.
> > Python here arguably DWIM better than perl.
> >
> > Paul
> >
> > * there's some mistakes where you at the end go "D'oh!" because perl is
> > messing you around with its weirdness or otherwise non-intuitive behavior.
> > "Duh" when you're messing around and just not paying attention.
> I wouldn't call that non-intuitive, it's a matter of culture. Perl
> coders are used to being verbose about whether they want keys/values
> from a hash. If someone changed the behavior to make a hash in list
> context return it's keys, you would confuse a lot of Perl programmers
> out there too.  And I'm particularly fond of perl's behaviour because
> it allows interesting cookbook tricks such as
> %config = (%default, %user_provided)

I don't think the Python behaviour excludes the above trick. It is as if
Python has a context that Perl doesn't have [1]: iterator context. In
such a context, just the keys are returned.

No doubt there will be Perl code that will break if Perl was changed such
that 'for(EXPR)' provides iterator context to EXPR, and a hash in such a
context acted as if it was 'scalar each %hash', but I think it would not
break that much. 'for (%hash)' isn't all that useful.

Now I think that it isn't worth breaking Perl to introduce this, but it
would have been nice if it was there from the start. Then people could
written 'for (<>)' without slurping in the entire file, and the 
'while (<>)' construct wouldn't have needed fixing around 5.004.

[1] There are some constructs in Perl that behave like an iterator,
    <> in scalar context, //g in scalar context, for (EXPR .. EXPR) to
    name a view.


More information about the london.pm mailing list