CUFP
Discipulus
another CUFP or better WICDWMBP (what i can do with my baby Perl)..<BR>
Reading [id://1080706|this post] i'm started wondering if there was a way to wrap an existing script and grab modules it uses without exucuting it.<BR>
Obviously the answer or part of it was in the monastery: [id://681911|here]<BR>
I very liked the
<c>
perl -d:Modlist=options script.pl
perl -MDevel::Modlist=options script.pl # equivalent
</c>
part but, unfortunately it executes the script.pl<br>
Also liked the [tachyon-II] hack, but you have to edit the script.pl and i'm too lazy.<br>
No hope to use <c>$^C = 1</c> as pointed wisely by [shmem]<BR><BR>
The <c>UNITCHECK</c> do the trick! Never known it seems quite usefull for this task: [href://http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END|read about it]<BR><BR>
<CODE>
#!perl
#use strict; #commented to not pollute %INC
#use warnings;#commented to not pollute %INC
my $file = $ARGV[0];
my $content = do { open my $fh, '<', $file or die $!; local $/; <$fh> };
my $begin =<<'THEEND';
UNITCHECK {
no strict; # access $VERSION by symbolic reference
no warnings qw (uninitialized);
print map {
s!/!::!g;
s!.pm$!!;
sprintf "%-20s %s\n", $_, ${"${_}::VERSION"}
} sort keys %INC;
exit;
};
THEEND
eval $begin.$content;
print $@ if $@;
</CODE>
Enjoy the results!<BR>
HtH<BR>
L*
<BR><BR>
<b>UPDATE 9 april 2014:</b> BE CAREFULL, as stated by [davido] and also by [LanX] in other post, <c>BEGIN</c> blocks are executed anyway. In fact <c>BEGIN</c> blocks come first, in order of definition, then come <c>UNITCHECK</c> blocks and, being that block prepended to the original code in the above program, it will be executed just after the last <c>BEGIN</c> block and just before any <c>UNITCHECK</c> defined in the original program passed in via <c>@ARGV</c>. In the case of <c>perl -c -d:TraceUse script.pl</c> all the <c>BEGIN UNITCHECK CHECK</c> blocks are executed.<BR><BR>
Here two examples to demonstrate where the <c>-c</c> ends his operations (a simplified version of [href://http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END|16 pillars of wisdom]):
<c>
perl -c -e "BEGIN{print qq(1-begin\n)};
UNITCHECK {print qq(2-unitcheck\n)};
CHECK {print qq(3-check\n)};
INIT {print qq(4-init\n)};
print qq(5-main\n);
END{print qq(6-end\n)}"
__OUTPUT__
1-begin
2-unitcheck
3-check
-e syntax OK
# the same code without -c
__OUTPUT__
1-begin
2-unitcheck
3-check
4-init
5-main
6-end
</c>
L*
<div class="pmsig"><div class="pmsig-174111">
There are no rules, there are no thumbs..<BR>
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
</div></div>