in reply to Re: sub running on use statement
in thread sub running on use statement

Spot-on. I've got a number of modules which contain sub-routines that are called, including calls to and from other sub-routines in other modules.

The one causing the problem is recursive and calls itself to fill an array with all the files found in a directory tree. Calling that sub-routine from another module is where I ran into the printing oddity ( odd to me ) when testing it out.

I'm still fuzzy on the evaluation part. I had thought that the evaluation was just checking the script for syntax and reference errors - essentially, "static" script problems. I didn't think the evaluation also includes running the sub-routines to check for run-time errors.

Is that what actually happens - that Perl does a "test-run" of the sub-routines in addition to checking for syntax and reference errors ?

Replies are listed 'Best First'.
Re^3: sub running on use statement
by AnomalousMonk (Archbishop) on Dec 02, 2014 at 19:32 UTC
    Is that what actually happens - that Perl does a "test-run" of the sub-routines in addition to checking for syntax and reference errors ?

    There is no "test-run" when a module is use-ed. Basically, Perl does what you use the module to tell it to do, but it does it at compile time. Usually you just tell Perl to define subroutines using sub, a fairly quiet process, or maybe define some global or lexical variables, likewise quiet. However, you can do other things, like building complex data structures — which, according to your other posts, you seem to be doing! You could call a built-in function like
        print 'string';
    and  string would be printed at compile time, as pointed out in other posts. You can call any (defined) function and it will execute.

    See "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod for more info on controlling when things get executed WRT compile versus run time.

Re^3: sub running on use statement
by LanX (Saint) on Dec 02, 2014 at 22:19 UTC
    Every module is evaluated at use time, but normally a module contains only sub definitions, so nothing is run then (except import() )

    BUT if you do unusual stuff like grandfather demonstrated in his first example ScrewyModule.pm , i.e. code is outside of any sub at top level then something like you described can happen.

    But that's speculation cause you are not showing us any code.

    So please either

    • play around with the examples given to see if it might happen to you
    • use Carp (see cluck() and confess() ) to force a stack trace at the beginning of the "weird" sub to track down the location of your problem.
    • shorten your problem manually to code isolating your problem and show.

    Anything else won't lead to results.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      > use Carp (see cluck() and confess() ) to force a stack trace at the beginning of the "weird" sub

      put Carp's cluck in the first line of the "strange" sub in the "weird" module

      lanx@lanx-1005HA:/tmp$ cat Weird.pm package Weird; use Carp qw/cluck/; sub tst { cluck "cluck tst entered"; print "\nnormal output in tst()...\n"; # ... } sub tst2 { tst(); } warn "\n--- using Weird.pm\n"; tst2(); # line 17 - executed at "use Weird" time sub import { warn "\n--- importing from Weird.pm\n"; tst2(); # line 22 - executed at Weird::import() time } 1;

      using Weird.pm

      lanx@lanx-1005HA:/tmp$ cat t_carp.pl warn "pre use\n"; use Weird; # ab'use warn "post use\n";

      Output with stacktrace showing all modules/lines leading to problem

      lanx@lanx-1005HA:/tmp$ perl -I. t_carp.pl --- using Weird.pm cluck tst entered at Weird.pm line 6 Weird::tst() called at Weird.pm line 13 Weird::tst2() called at Weird.pm line 17 <--- AHA !!! require Weird.pm called at t_carp.pl line 4 main::BEGIN() called at Weird.pm line 0 eval {...} called at Weird.pm line 0 normal output in tst()... --- importing from Weird.pm cluck tst entered at Weird.pm line 6 Weird::tst() called at Weird.pm line 13 Weird::tst2() called at Weird.pm line 22 <--- AHA !!! Weird::import('Weird') called at t_carp.pl line 4 main::BEGIN() called at t_carp.pl line 4 eval {...} called at t_carp.pl line 4 normal output in tst()... pre use post use

      HTH!

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)