1

I write perl scripts that require other perl scripts. The "require"d scripts contain subroutine definitions. (I am not yet at the level of writing my own package or module; I just require files.) Is there an accepted set of terms by which one distinguishes between the "ancestor" script, i.e., the one that I invoke on the command line, and the "descendant" scripts, which are required and parsed at the moment that I invoke the "ancestor" script? For instance, in the following code snippet

use Cwd 'abs_path';
my $script=abs_path($0);

it does not matter whether this appears in the script that I invoked or in a file that one of my files/scripts required; the answer will be the same.

Is there a variable name other than "script" which would make more sense to people in the programming community? Perhaps a more specific term?

The following page https://perldoc.perl.org/variables/$0 seems to suggest that "program" is a better term than "script"? But that also is a very general term; it could mean just about anything. Seems to me, my entire set of required files constitute the program. I certainly have been programming all of them. And if one of them throws an exception or error, the entire thing dies.

Jacob Wegelin
  • 1,304
  • 11
  • 16
  • 1
    Hhm, "dependencies"? – sticky bit Jan 20 '22 at 20:55
  • 1
    Note that if the script is run from the the shell with a relative path, for example `perl p.pl`, `$0` will be relative to the current directory when the script was started. If some code changes the current directory before your `abs_path($0)` statement, the result of that statement will not be the path to your script – Håkon Hægland Jan 20 '22 at 21:23
  • 2
    (1) Since these scripts that your main one uses are independent programs, I wouldn't consider a special term for them; perhaps "called" scripts as opposed to the "caller" script (2) But, if the main script `require`s (evaluates) them, then they'd be "`require`-ed scripts" and then I'd say: do not do that. Writing a module (library) in Perl is absolutely trivial. And if those `required` scripts need be programs (as they get used on their own) then make them drivers for their respective libraries. – zdim Jan 20 '22 at 21:32
  • 1
    Also, if you use `require` from the main script `main.pl` to load another script `foo.pl`, the required script will be loaded into the default `main` package, so there will be no difference between the two – Håkon Hægland Jan 20 '22 at 21:35
  • 1
    Couldn't help it ... [basics](https://pastebin.com/pvPWtHHm) (excuse me if you know *that* but that answers it?) – zdim Jan 20 '22 at 21:46

2 Answers2

2

These terms aren't something particular to Perl, although some languages might have more specialized, particular definitions.

A program and a script are basically the same thing. Almost no one is going to get confused about using either of those terms. Some people think that the term "script" implies that it's somehow less than what it is, but that's not a huge deal. Typically though, either "does" something.

Things that you require are typically called "libraries" or "modules", and those tend to be other Perl files that supply something for you to use rather than doing something directly. Often these don't "do" anything other than provide things you can use in your main program.

It's possible for one program to require another program and have it do its work, but that's not a common practice.

You ask about use Cwd 'abs_path'; and where it should appear. Although it can work no matter where you put it, I tend to include it in any file that would use abs_path. The libraries you include may decide to remove it or go in a different direction. Don't rely on a library to pull in the parts you are going to use directly. You may get away with it for awhile, but it is more fragile.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
2

In the Perl 4 days, before we had namespaces, we called such files "libraries".

These days, there are no well-known name for them since there's no excuse for such a bad practice.

Let's fix your code.

In the script:

  1. Add use FindBin qw( $RealBin ); use lib $RealBin;. This tells Perl to look for module same dir as the script.

For the user of the module:

  1. Use use SomeName; instead of do("SomeName.pl") or die $@ // $!;" to load the required file.

For the module:

  1. Rename to SomeName.pm.
  2. Add package SomeName;.
  3. Add use Exporter qw( import ); @EXPORT = qw( sub1 sub2 ... );.
  4. You should already have a trailing 1. If not, add that.
ikegami
  • 367,544
  • 15
  • 269
  • 518