Someone (Maddingue) in #perlfr mentioned that they couldn't understand the following test code in bleadperl/t/op/incfilter.t:

do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;

After looking at it, I gave up, so I was wondering if someone could explain it in gory detail. Thanks.

I'll explain what I figured out.

First, what does do [...] do? Does it use something like "ARRAY(0x814bc28)" as the filename to open? If you look at the test file, at the top is:

unshift @INC, sub { no warnings 'uninitialized'; ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1]; };

If you look in perldoc perlvar for the docs on @INC, you'll find that it says

You can also insert hooks into the file inclusion system by putting Perl code directly into @INC. Those hooks may be subroutine references, array references or blessed objects. See "require" in perlfunc for details.

So look also in perldoc perlfunc, grep for require VERSION followed by hooks. By this point my head is already spinning, but then note that before the do comes a 3-arg open of a reference to a heredoc:

open $fh, "<", \<<'EOC'; BEGIN {prepend_block_counting_filter}; pas("SSS make s fast SSS"); EOC

The docs for that are in perldoc -f open:

open file handles to "in memory" files held in Perl scalars via: open($fh, ’>’, \$variable)

Then note that \"foo" makes a reference to a string, so \<<'EOC';... is just a funny reference to a string that happens to've been made by a heredoc.

Now's where I get lost. Anyone fill in the details? (What's the $fh doing in do [$fh, sub {...}], for example?)


In reply to do [...], @INC subs, 3-args open to heredoc refs (oh my) by ForgotPasswordAgain

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.