in reply to Re^2: How to give input and collect output of a perl script using another perl script
in thread How to give input and collect output of a perl script using another perl script

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.

True laziness is hard work

Replies are listed 'Best First'.
Re^4: How to give input and collect output of a perl script using another perl script
by chakreey (Acolyte) on Apr 07, 2011 at 10:09 UTC
    thank you very much GF. Very nice explanation, now, I understand why module test.pm did not work !

    For the first time, I could group all my perl scripts into single package. With-in this package, I placed one main executable script. Any one who wants to use my scripts, has to run this main executable script. Depending upon user requirements, appropriate module will be called by that main script.