http://www.perlmonks.org?node_id=147752

shotgunefx has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to determine what type of block a sub is called from?

I was working on a greplike operator called findone.

It needs to keep track of where it was called from to keep it's position in the @array in question.
while ( my ($val,$index) = findone { $_ > 1000 } @elements){ # Do something }

This work find and dandy until I realized a subtle bug.
my @tokens = (tokens here); while ($line =<>){ chomp($line); die "$line is not a valid token" unless findone { m/^$line/ } @tok +ens; }
Because it keeps track of where and what it's called with, it will never match past the first match which means it will fail on the second block. (Unless there are multiple matches in @tokens. Then it will fail on the N+1 iteration.)
If I move my @tokens into the while block, it will work ok because the ref is different which it will see.

I knowning about this could just take care not to do this, or I could make a version that doesn't keep track and call it find or findfirst / findany whatever, but in the spirit of DWIM and learning, is there a way to tell if it's being called from a control block? I realize this wouldn't help calls within bare blocks.
I have a feeling I could coke up something with a source filter but would like to avoid it if possible.

Thanks,

-Lee

"To be civilized is to deny one's nature."