user-pic

Tom Molesworth

  • Commented on Do not use each
    Not using each() seems like good advice in general. I think that second problem unfairly singles out Data::Dumper, though - the bug is in the example code, not Data::Dumper itself. The iterator would also be reset if Dumper() used keys()...
  • Commented on POE::Session object_states: handlers are sub names not CODEREFs
    That's nothing to do with POE, though - you're just calling _poll_start / _poll_work etc. without parameters? perl -e'sub _poll_start { print "Called with @_\n" } my $thing = { start => _poll_start };' passing sub { $self->_poll_start } or...
  • Commented on Perl and Me, Part 2: The Power of OOP
    Interesting. It sounded like it was the boilerplate that you were calling out in particular, apologies if I misunderstood. Anyway, seems my experiences are pretty much the opposite - it's the Perl OO code which Just Works. Sure, a typo...
  • Commented on Perl and Me, Part 2: The Power of OOP
    You've mentioned several times that writing objects/classes in C++ is easier than in Perl. Would you be able to provide examples? Generally I find it to be the opposite, to the extent that I have Perl write my C++ code...
  • Commented on MooseStyle: Self-made inline vs cpan-based inline
    > Is your issue about Mo or about non-core dependencies? Neither - it's about modules which use Mo::Inline. Mo itself seems fine, if a module uses it and works, that's great. Non-core dependencies... the more the merrier. I have no...
  • Commented on MooseStyle: Self-made inline vs cpan-based inline
    An upgrade to Mo::Inline appears to be behind this issue for YAML: https://rt.cpan.org/Ticket/Display.html?id=90817 and I think a similar issue was behind this: https://rt.cpan.org/Ticket/Display.html?id=76664 It's not the most pleasant code to be debugging either. It's an interesting idea, but I'm treating...
  • Commented on Arrow Operator Shenanigans
    It's documented in 'Subroutine References as Methods' in perldoc perlobj: You can also use a subroutine reference as a method: $file->$sub(); This is exactly equivalent to writing "$sub->($file)". so I think it's fine to rely on this behaviour. Might be...
Subscribe to feed Recent Actions from Tom Molesworth

  • Steven Haryanto commented on Do not use each

    I've also avoided 'each' since a few years ago. Is there a Perl::Critic policy to forbid using each? There should be. And perhaps perl can emit experimental warning for usage of 'each' in version 5.2x? :-)

  • Damian Conway commented on Do not use each
    ...provided you don't modify the scoped hash while you're looping, don't nest loops over the same hash, and (if you're looping through the hash more than once in the scope) that you don't terminate one of the loops prematurely (i.e. last out of it) leaving the iterator half way through the hash at the start of the next loop.

    All in all, each seems so brittle that it's just much safer to avoid …

  • martin.j.evans commented on Do not use each

    I only hit a problem with each recently (see http://www.martin-evans.me.uk/node/159). Using a hash in list context will use the iterator internally so simply assigning the hash you are iterating over to another hash will cause an infinite loop.

    I now never use each. Even if you find the narrow safe cases someone else can come along and easily break your code.

  • Timm Murray commented on Do not use each

    I've also seen situations where each was called in a loop that short-circuits:

    while( my ($key, $val) = each %hash ) {
        last if $key =~ /foo/;
    }
    

    Then later on, each is used again without resetting the internal state, even though the programmer expected to iterate over the entire hash. With a hash stored globally (yes, this is bad in itself, but it happens) in a persistent environment like mod_perl, this can even happen across different requests in the same process.

  • Tim Bunce commented on Do not use each

    I wonder if there's some way to make the each op warn if the iterator isn't where it's expected to be.

Subscribe to feed Responses to Comments from Tom Molesworth

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl and offering the modern features you’ve come to expect in blog platforms, the site is run by Dave Cross and Aaron Crane, with a design donated by Six Apart, Ltd.