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


In reply to Re: Inserting Code Before an -n/-p Loop by Eily
in thread Inserting Code Before an -n/-p Loop by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.