ForgotPasswordAgain has asked for the wisdom of the Perl Monks concerning the following question:
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?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: do [...], @INC subs, 3-args open to heredoc refs (oh my)
by Maddingue (Sexton) on May 01, 2006 at 16:48 UTC | |
by vkon (Curate) on May 01, 2006 at 18:13 UTC | |
by ForgotPasswordAgain (Vicar) on May 03, 2006 at 11:40 UTC |