Perl6 one liner execution. How is the topic updated?

When you add -n it adds a for loop around your code.

You think it adds one like this:

for lines() {
  # Your code here
}

The compiler just adds the abstract syntax tree nodes for looping without actually adding a block.

(
   # Your code here
) for lines()

(It could potentially be construed as a bug.)

To get it to work like the first one:

(             # -n adds this

  -> $_ {     # <-- add this

              # Your code here

  }( $_ )     # <-- add this

) for lines() # -n adds this

I tried just adding a bare block, but the way the compiler adds the loop causes that to not work.


In general ENTER and LEAVE are scoped to a block {}, but they are also scoped to the “file” if there isn't a block.

ENTER say 'ENTER file';
LEAVE say 'LEAVE file';
{
  ENTER say '  ENTER block';
  LEAVE say '  LEAVE block';
}
ENTER file
  ENTER block
  LEAVE block
LEAVE file

Since there is no block in your code, everything is scoped to the “file”.


The -n command line argument puts a loop around your program,

for $*ARGFILES.lines {
    # Program block given on command line
}

whereas the program execution phasers you used (BEGIN and END), are run once either at compile time or after the program block has finished, so they will not be part of the loop at run time.

The ENTER block phaser will run at every block entry time, whereas the the LEAVE block phaser will run at every block exit time. So these phasers will be run for each line read in the for loop.