1 package App::FatPacker::Trace;
4 use warnings FATAL => 'all';
11 my (undef, $file, @extras) = @_;
13 $trace_file = $file || '>>fatpacker.trace';
14 # For filtering out our own deps later.
15 # (Not strictly required as these are core only and won't have packlists, but
19 # Use any extra modules specified
20 eval "use $_" for @extras;
26 return unless $trace_file; # not imported
28 open my $trace, $trace_file
29 or die "Couldn't open $trace_file to trace to: $!";
31 for my $inc (keys %INC) {
32 next if exists $initial_inc{$inc};
33 print $trace "$inc\n";
43 App::FatPacker::Trace - Tracing module usage using compilation checking
47 # open STDERR for writing
48 # will be like: open my $fh, '>', '&STDERR'...
49 perl -MApp::FatPacker::Trace=>&STDERR myscript.pl
51 # open a file for writing
52 # will be like: open my $fh, '>>', 'fatpacker.trace'
53 perl -MApp::FatPacker::Trace=>>fatpacker.trace myscript.pl
57 This module allows tracing the modules being used by your code. It does that
58 using clever trickery using the C<import> method, the C<CHECK> block and
59 L<B>'s C<minus_c> function.
61 When App::FatPacker::Trace is being used, the import() method will call
62 C<B::minus_c> in order to set up the global compilation-only flag perl
63 (the interpreter) has. This will prevent any other code from being run.
65 Then in the C<CHECK> block which is reached at the end of the compilation
66 phase (see L<perlmod>), it will gather all modules that have been loaded,
67 using C<%INC>, and will write it to a file or to STDERR, determined by
68 parameters sent to the C<import> method.
74 This method gets run when you just load L<App::FatPacker::Trace>. It will
75 note the current C<%INC> and will set up the output to be written to, and
76 raise the compilation-only flag, which will prevent anything from being
77 run past that point. This flag cannot be unset, so this is most easily run
78 from the command line as such:
80 perl -MApp::FatPacker::Trace [...]
82 You can control the parameters to the import using an equal sign, as such:
84 # send the parameter "hello"
85 perl -MApp::FatPacker::Trace=hello [...]
87 # send the parameter ">&STDERR"
88 perl -MApp::FatPacker::Trace=>&STDERR [...]
90 The import method accepts a first parameter telling it which output to open
91 and how. These are both sent in a single parameter.
93 # append to mytrace.txt
94 perl -MApp::FatPacker::Trace=>>mytrace.txt myscript.pl
97 perl -MApp::FatPacker::Trace=>&STDERR myscript.pl
99 The import method accepts additional parameters of extra modules to load.
100 It will then add these modules to the trace. This is helpful if you want
101 to explicitly indicate additional modules to trace, even if they aren't
102 used in your script. Perhaps you're conditionally using them, perhaps
103 they're for additional features, perhaps they're loaded lazily, whatever
106 # Add Moo to the trace, even if you don't trace it in myscript.pl
107 perl -MApp::FatPacker::Trace=>&STDERR,Moo myscript.pl