in reply to Difference between executing and "using" a .pm file

Check caller.

print caller ? 'required/used' : 'running as script';

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Difference between executing and "using" a .pm file
by Marshall (Canon) on Mar 30, 2021 at 19:15 UTC
    Thank you!

    caller says "Returns the context of the current pure perl subroutine call. In scalar context, returns the caller's package name if there is a caller"

      The concept - like already mentioned by pryrt - is called "modulino" and was popularized by Brian D Foy IIRC.

      I'm using it regularly to run various tests when developing a module.

      Like this I can always hit F5 in my editor, no matter in which file of a project I am.

      And the current test-procedure is on top of my module in an

      unless (caller()) { BLOCK }

      Like this I don't need to always change a different make file to test the function I'm currently changing.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Thanks! I shall keep that in mind. The purpose of this validate() program is not to test my module. It is to help the user develop a consistent and accurate configuration file. Over the last 5 years of development, the syntax of this config file has become more complex. It winds up being possible to configure things that my SW doesn't consider an error, but nevertheless will could surprise the user. This is to make sure "that you've told the program to do what you want". So it spews out all sorts of explanations intended for a human to read. Updating this config file is serious business and is only typically done once per year.