Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Inserting Code Before an -n/-p Loop

by Eily (Monsignor)
on Dec 18, 2018 at 10:03 UTC ( [id://1227384]=note: print w/replies, xml ) Need Help??


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'; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227384]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2025-07-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.