Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

next in a post-fixed for

by Codon (Friar)
on Jul 06, 2007 at 17:59 UTC ( #625316=obfuscated: print w/replies, xml ) Need Help??

This may be better posted in Meditations. It sort of touches on both ponderings and obfuscations. I was prototyping an algorithm the other day when I developed a rather obfuscated method for building a hash from two lists. While this is not exactly a full "obfu", I certainly think the behavior could be incorporated into a full obfu. This seems to actually get in to the "what the heck is happening here?" as ysth, gryphon, and especially diotalevi have commented on perverseness of the construct. Without further ado:

push( @{ $y{ ($a = $_, ( grep {$a=~/$_/} @t )[0] || next)[1] } }, $_ ) + for @f;

The intent of this code is to take a list of patterns and a list of strings and group the strings based on pattern. It also needed to disregard any string that did not match any pattern.

In larger context, this line came from a bit of prototype code I was working on that was

use strict; my @t=qw(one two three); my @f=qw( one.ind.01 one.ind.02 two.ind.01 two.ind.02 three.ind.02 three.ind.01 four1.ind four2.ind four2.ind f1v.ind.31 f1v.ind.32 f1v.ind.32 ); my %y; push( @{ $y{ ($a = $_, ( grep {$a=~/\b$_\.ind/} @t )[0] || next)[1] } +}, $_ ) for @f;

All monks previously mentioned (as well as others), were immediately revolted by such "offensive" code. diotalevi went on to say "Please tell me that you have never committed any code that actually looked like that!" He then went off to try to decipher how it actually worked. I do not think he ever found a satisfactory answer other than the next is a near-no-op with stack manipulation.

The line in question can be re-written in a much more verbose way:

So, what are the thoughts of other monks regarding this obscure construct? Perhaps more importantly, what does the Perl development community say about the behavior (both internal and external)? Is this asking for a future incompatibility? Is it firmly grounded or did I stumble on something that should not work?

Ivan Heffner
Sr. Software Engineer, Inc.

Replies are listed 'Best First'.
Re: next in a post-fixed for
by diotalevi (Canon) on Jul 06, 2007 at 18:37 UTC

    I had two objections. First, I was mapping my concept blockless of postfix conditional onto postfix loops. Postfix loops' implementation is to create a block context. This was contrary to my experience with conditionals and I had to peek at some perlguts to realize this. My other concern was the debugging tools built into perl tell me that the next() was a static "goto" into a nonsensical location and was equivalent to not executing the next at all. It turns out the static "goto" information built into next is not used - it does its "goto" behind the scenes and in a place the debugging tools know nothing of. This exposes two perl bugs. One in the core is to populate this nonsensical op_next data and the other is in B::Concise where it reports the nonsense data.

    Here's a simpler implementation to show off the bug.

    push @out, $_ || next for @in;

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: next in a post-fixed for
by educated_foo (Vicar) on Jul 06, 2007 at 19:38 UTC
    FWIW #1: The thing that gets me isn't the "next", but all the nested dereferences, which are hard to parse.

    FWIW #2: It seems like you're doing things backwards. Wouldn't this be more straight-forward?

    for $t (@t) { $y{$t} = [grep /$t/, @f] }
    Update: c.f. jdalbec's response, I didn't read the OP's node carefully enough (probably because #1 is so true... ;).
      The above code was just prototype code; the actual code was getting the list of strings (@f) from a readdir. This was one of the original motivators for having the outer looping construct be over @f.

      Ivan Heffner
      Sr. Software Engineer, Inc.
      The OP's code associates each string only to the first pattern that it matches. Your simplification associates each string to each pattern that it matches. Also, if there are many patterns, the OP's code lends itself to optimization via study().

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://625316]
Approved by diotalevi
Front-paged by diotalevi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-11-18 12:19 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.