Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

check modules used by a script and their version

by Discipulus (Canon)
on Apr 03, 2014 at 10:26 UTC ( [id://1080920]=CUFP: print w/replies, xml ) Need Help??

another CUFP or better WICDWMBP (what i can do with my baby Perl)..
Reading this post i'm started wondering if there was a way to wrap an existing script and grab modules it uses without exucuting it.
Obviously the answer or part of it was in the monastery: here
I very liked the
perl -d:Modlist=options script.pl perl -MDevel::Modlist=options script.pl # equivalent
part but, unfortunately it executes the script.pl
Also liked the tachyon-II hack, but you have to edit the script.pl and i'm too lazy.
No hope to use $^C = 1 as pointed wisely by shmem

The UNITCHECK do the trick! Never known it seems quite usefull for this task: read about it

#!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 $@;
Enjoy the results!
HtH
L*

UPDATE 9 april 2014: BE CAREFULL, as stated by davido and also by LanX in other post, BEGIN blocks are executed anyway. In fact BEGIN blocks come first, in order of definition, then come UNITCHECK blocks and, being that block prepended to the original code in the above program, it will be executed just after the last BEGIN block and just before any UNITCHECK defined in the original program passed in via @ARGV. In the case of perl -c -d:TraceUse script.pl all the BEGIN UNITCHECK CHECK blocks are executed.

Here two examples to demonstrate where the -c ends his operations (a simplified version of 16 pillars of wisdom):
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
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re: check modules used by a script and their version
by Tux (Canon) on Apr 03, 2014 at 10:35 UTC
      Obviously no! :=)
      Now that I know, you read my signature? ;=)

      L*
      UPDATE: Devel::TraceUse too seems to execute the code.(typo fixed, thanks to McA)
      UPDATE2: perl -c -d:TraceUse script.pl does not executes the script. thanks again

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        BEGIN {...} blocks are executed by perl -c. It's not easy to prevent that.


        Dave

Re: check modules used by a script and their version
by Discipulus (Canon) on Apr 09, 2014 at 07:02 UTC
    carlriz has asked clarification about this code here maybe someone will found it useful


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1080920]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-10-07 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (44 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.