Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I suspect (but don't know for sure) that your "bombs my machine" problem is due to the cyclic nature of: run script ... load module ... run B::Xref ... run script ... load module ... run B::Xref ... ad infinitum.

There's some other (potential) issues, which include: you're currently finding all modules using @ARGV, not just the ones loaded before your module; splitting the output from B::Xref on whitespace when pathnames may contain whitespace; portability of /dev/null; assuming modules have a .pm extension.

I created module PM::1082204::Log::Reproducible in ~/tmp/PM/1082204/Log/. This is intended to mirror your Log::Reproducible. Here's some notes:

  • I've used a BEGIN block, instead of a subroutine called from import(), so that there's no reliance on import() being called and the @ARGV checks are done first.
  • I've taken the code from the beginning of the calling script (up to, but not including, the use Log::Reproducible) and saved to a File::Temp temporary file. B::Xref operates on this temporary file which fixes the cyclic/infinite loop issue mentioned above.
  • I've used IPC::Open3 to deal with the /dev/null issue: STDERR output is accessible from <CERR> but that filehandle isn't read.
  • B::Xref discards a leading './' in the filename path but retains instances of embedded '/./'. I've handled this (e.g. /^(?:\.[\\\/]?)?(.*)$/) but there may be better ways to do this.
  • There's quite a few tweaks I could envisage you making to this; the code here should at least provide a framework as a starting point.

Here's the PM::1082204::Log::Reproducible code:

package PM::1082204::Log::Reproducible; use 5.010; use strict; use warnings; use autodie; use File::Temp (); use IPC::Open3; BEGIN { my $code = do { open my $fh, '<', $0; local $/; <$fh> }; my ($code_to_test) = $code =~ /(\A .*?) use \s+ @{[__PACKAGE__]}/s +x; my ($temp_fh, $temp_filename) = File::Temp::tempfile(); print $temp_fh $code_to_test; local(*CIN, *COUT, *CERR); my $cmd = "$^X -MO=Xref,-r $temp_filename"; my $pid = open3(\*CIN, \*COUT, \*CERR, $cmd); my $re = '(?:' . join('|' => map { /^(?:\.[\\\/]?)?(.*)$/; "\Q$1" +} @INC) . ')[\\\/]?(\S+?)(?:\.\S+)?\s'; my %argv_modules; for (<COUT>) { next unless /\@\s+ARGV/; (my $module) = /$re/; $module =~ s{[\\\/]}{::}g; ++$argv_modules{$module}; } waitpid $pid, 0; my @warn_modules = sort keys %argv_modules; if (@warn_modules) { warn "WARNING:\n", "Modules using'\@ARGV' before " . __PACKAGE__ . " loaded:\ +n"; warn "\t$_\n" for @warn_modules; } } 1;

In addition, the Qwerty module, in ~/tmp/PM/1082204/Log/X X/, tests for pathnames with whitespace:

package Qwerty; use 5.010; use strict; use warnings; sub xxx { say "ARGV not empty." if @ARGV; } 1;

And, the ModuleWithoutPmExtension, in ~/tmp/, tests pretty much what it says:

package ModuleWithoutPmExtension; sub test { print "I'm ", __PACKAGE__, "\n"; print "\@ARGV has elements.\n" if @ARGV; } 1;

I ran the tests from ~/tmp. Here's

#!/usr/bin/env perl use strict; use warnings; BEGIN { require 'ModuleWithoutPmExtension'; ModuleWithoutPmExtension::test(); } use lib './../tmp/./PM/1082204/Log/X X'; use Qwerty; { use Getopt::Long; use PM::1082204::Log::Reproducible; } use Getopt::Std;


I'm ModuleWithoutPmExtension WARNING: Modules using'@ARGV' before PM::1082204::Log::Reproducible loaded: Getopt::Long ModuleWithoutPmExtension Qwerty

Getopt::Std isn't in the list. If you load it earlier in the code, it does appear in the list.

-- Ken

In reply to Re^3: What are (popular) modules that access/modify @ARGV? by kcott
in thread What are (popular) modules that access/modify @ARGV? by frozenwithjoy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2023-06-04 19:49 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.