in reply to Finding dependancies of a script

Same basic idea, but saves some of the futzing.

Update: Changed die to exit. Changed it back, as exit doesn't prevent the script from continuing passed the CHECK sub?

package Dependancies; sub CHECK { print $_, $/ for values %INC; die; } 1;

Then just add it to the command line with the -M switch.

P:\test>perl58 -MDependancies p1.pl8 d:/Perl/lib/warnings/register.pm d:/Perl/lib/bytes.pm d:/Perl/lib/XSLoader.pm d:/Perl/lib/Carp.pm d:/Perl/lib/Exporter.pm d:/Perl/lib/strict.pm d:/Perl/lib/warnings.pm d:/Perl/lib/overload.pm d:/Perl/lib/Shell.pm d:/Perl/lib/Data/Dumper.pm Dependancies.pm Died at Dependancies.pm line 5. CHECK failed--call queue aborted.

It probably needs a better name. Maybe something in the Devel::* namespace?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Re: Finding dependancies of a script
by sauoq (Abbot) on Jul 25, 2003 at 20:32 UTC

    First, you don't need sub there. Second, you don't need die or exit. Third, you want to run it with -c on the command line.

    I think. I didn't actually test any of that... :-)

    Update: Ok, I've tested it now. The code:

    package Dependencies; CHECK { print $_, $/ for values %INC; } 1;
    And a test run (on a test script that consisted of a single line, "use CGI;"):
    $ perl -MDependencies -c ./test.pl /usr/lib/perl5/5.8.0/warnings/register.pm /usr/lib/perl5/5.8.0/Carp.pm Dependencies.pm /usr/lib/perl5/5.8.0/vars.pm /usr/lib/perl5/5.8.0/strict.pm /usr/lib/perl5/5.8.0/Exporter.pm /usr/lib/perl5/5.8.0/constant.pm /usr/lib/perl5/5.8.0/warnings.pm /usr/lib/perl5/5.8.0/CGI/Util.pm /usr/lib/perl5/5.8.0/overload.pm /usr/lib/perl5/5.8.0/CGI.pm ./test.pl syntax OK

    -sauoq
    "My two cents aren't worth a dime.";
    

      you don't need sub there

      I know, but I always do with BEGIN/INIT/CHECK/END these days.

      Basically, I find the discrepancy between not having to use sub with those, but having to with TIEHASH/FETCH/STORE etc. inconsistant, and one thathad me taering my hair out for a while a couple of months back, so now I just avoid the inconsistancy. Call it an indulgance :)

      I hadn't thought of combining the module with -c, that works really nicely++.

      If you reverse the order of the switches you can almost read it as "perl check dependancies in test.pl", which works as a nice syntax reminder.

      (I was going to use that new monic word, but I think I better stay clear of that:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

        Call it an indulgance :)

        I call it "confusing" because, well, it confuses me. :-)

        Afterall, they aren't really subs, right? You can have more than one BEGIN (et al) block without concern for redefining previous ones. And, my $arg = shift; will shift off @ARGV rather than @_ as you would expect in a sub. Heck, you can't even explicitly call it!¹ And in those ways, they differ significantly from TIE* and family.

        Frankly, and maybe I shouldn't admit this, I was surprised putting sub in front even worked. I always think of BEGIN, INIT, CHECK, and END as "special blocks" in a category of their own. I think I've been led that way by the documentation which uses the term "block" to describe them.

        1. And trying to opened up a real can of worms. Apparently, even

        sub main::BEGIN { print "X\n" }
        is treated as a BEGIN block. You can jump through hoops to get a sub called BEGIN if you try:
        *main::BEGIN = sub { print "X\n" }; &BEGIN;
        but try
        *main::BEGIN = sub { print "X\n" }; BEGIN();
        Prototype mismatch? Ok, so put the prototype in...
        *main::BEGIN = sub () { print "X\n" }; BEGIN();
        That clears the complaint up just fine but our sub is never called. And while we are trying dumb things, take a look at this:
        perl -e 'BEGIN' # Syntax error. perl -e 'BEGIN; # No error. perl -e 'BEGIN()' # Syntax error. perl -e 'BEGIN();' # No error.
        Blech! Blech! BLECH!

        After all this, I'm inclined to think that sub BEGIN should be treated as an error if not a regular subroutine. (Of course, given the current state of affairs, we couldn't exactly start treating it as a regular subroutine now, could we? So I think it should be an error.)

        -sauoq
        "My two cents aren't worth a dime.";