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."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.