in reply to How to give input and collect output of a perl script using another perl script

I'd vote for modify compare.pl into compare.pm. Actually I'd copy compare.pl to Compare.pm and add:

package Compare; # modified original contents of compare.pl here 1;

The package line at the top and the (implicit return) 1 at the end are pretty much all the extra stuff you need. In your calling script you:

#!/usr/bin/perl use strict; use warnings; use Compare; ... Compare::SomeSubInTheCompareModule ();

Notice the way sub SomeSubInTheCompareModule is called by using the package name. Aside from that icing a sub in Compare is called and used in just the same way as a sub in the .pl file.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: How to give input and collect output of a perl script using another perl script
by chakreey (Acolyte) on Mar 23, 2011 at 13:16 UTC

    GF: Thank you for short intro to perl modules

    I am learning to use perl modules with a small example. Please go through below example, I have a couple of questions at the end

    test2.pl is a script that uses perl module test.pm

    Contents of test.pl

    #!/usr/bin/perl use strict; use warnings; use test; my @gaga = ("paras", "kansas"); my ($x, $y) = test::test(@gaga); print "$x\n$y\n";

    contents of test.pm

    package test; sub test { my $tag = $_[0]; $tag =~ s/p/n/; my $bag = $_[1]; $bag =~ s/k/p/; return $tag, $bag } 1;

    output

    napas panas

    above script and module work very well. What I don't understand is..

    Query-1: What is function of 1 at the end of test.pm ?

    To see whether module works without a subroutine, I modified test.pm, by removing subroutine (see code below)

    Contents of test2.pl

    #!/usr/bin/perl use strict; use warnings; use test; my @gaga = ("paras", "kansas"); my ($x, $y) = test(@gaga); print "$x\n$y\n";

    contents of test.pm

    package test; my $tag = $_[0]; $tag =~ s/p/n/; my $bag = $_[1]; $bag =~ s/k/p/; return $tag, $bag 1;

    Expected result is...

    napas panas

    But error message reads...

    test.pm did not return a true value at test2.pl line 4. BEGIN failed--compilation aborted at test2.pl line 4.

    Query-2: Is it must to have a subroutine inside a module ?

      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
        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.

        and the script doesn't work !

        What does that mean? How do I post a question effectively?

        I updated my reply. Hope, my query is more meaningful now. btw, thanks for directing me to those links !