Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Executing FIRST before anything in the loop makes it pretty uninteresting. It would be more useful if it were run conditionally but on its place.

    About implementing it, how about maintaining a loop iteration counter in some internal variable as $?I.

    The same applies for LAST, but how do you know you are running the last iteration until after the loop check has failed?

    • Executing FIRST before anything in the loop makes it pretty uninteresting. It would be more useful if it were run conditionally but on its place.

      ...and that, to be clear, is how it's spec'd to work, and how I actually end up implementing it in the post.

      The same applies for LAST, but how do you know you are running the last iteration until after the loop check has failed?

      Also addressed in the blog post in whose comment thread you are commenting. :)

      • It seems I have not explained my opinion clearly, let me illustrate it with some code:

        for 1..3 { say "enter"; FIRST { say "first run" } LAST { say "last run" } say "leave" }

        I would consider FIRST and LAST to be interesting if that code printed enter, first run, leave, enter, leave, enter, last run, leave.

        • Then what you want is probably not a phaser. Happily, the "create this exact behavior with a home-made module" option is still available as a way out.

          A simple way to do what you want without any syntax modifications to the language would be to iterate over .kv of the list, and then check the .value part in if statements in the loop block.

          In general, it's not possible to know which iteration will become the last one until it's already finished. It is possible in your example, with a finite, statically introspectable literal list. But since looping works with iterators, the information required to answer the question "am I on the last iteration?" generally isn't available.