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


in reply to Inserting Code Before an -n/-p Loop

Neat trick indeed! When I read your post I immediatly thought about a problem I had a few weeks ago where I wished such a construct existed (BEGIN worked alright, but it makes the command a bit long and I like them to fit on one line :) ). Turns out my problem is exactly the one mentioned in the linked post. And Discipulus has a good point about being able to glob your arguments :)

I was a little confused by the deparsed code because the print 'foo' doesn't come right after the 5; But it's just that -M'5; print "foo"' is actually turned into use 5; print 'foo'; and the first statement is turned into an equivalent BEGIN + require block :)

It also works if you are already including a module without parameters

perl -MO=Deparse -M"Data::Dump; print 'Hi'" -ne "" use Data::Dump; print 'Hi'; LINE: while (defined($_ = readline ARGV)) { (); } -e syntax OK
It technically works with strict, but if you're going to use that kind of constructs you probably don't need strictures :P

Also, if you want to say something in that BEGIN-like code, since the feature would only be added by -E after the inserted code, you'll have to do something like: -M'v5.10; say "just saying"'

Edit: who needs O::Deparse anyway?

perl -M"5.01;say q&" -Mstrict -anE " print 'Hi'; }&;{" ;use strict;use feature ':5.26';LINE: while (<>) {our @F=split(' '); print 'Hi'; }