1/ When you use or require a module Perl loads it and compiles it. Perl then executes any "main line" code in the module and checks that the result is true. For most modules the "main line" code may be as simple as 1; which is equivalent to return 1; and returns the true result Perl expects. More interesting work done by a module during loading includes initialising variables and perhaps performing any other one time house keeping that the module may need (even including connecting to a database for example).
From 1/ above you may figure out that the load process will see return $tag, $bag 1; as the result from loading the module. If you had used strictures in the module (use strict; use warnings;), which you should always do, you would have received a number of "Use of uninitialized value" messages too. Because there are no parameters passed in $tag and $bag are undefined which is false so the module load fails. Module Test (note the upper case first letter - it's important) should look like:
use strict; use warnings; package Test; sub SubInTest { my $tag = $_[0]; $tag =~ s/p/n/; my $bag = $_[1]; $bag =~ s/k/p/; return $tag, $bag; } 1;
Note that strictly lower case module names are reserved by Perl for use as pragmata such as strict and warnings. You can use such names, but shouldn't in case Perl decides it wants to in some future version.
2/ By now it should be clear that, except for unusual modules (which only export data for example), it is futile to have a subroutine-less module because you can't execute any code in it after it's been loaded.
In reply to Re^3: How to give input and collect output of a perl script using another perl script
by GrandFather
in thread How to give input and collect output of a perl script using another perl script
by chakreey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |