in reply to Returning data from a sub routine in a module.

This really depends on how your files are set up. Here's an example using accounts.pm module file, and a script.pl file that uses it.

Both files are in the same directory, so I use use lib '.'; so the script knows to look in the current directory for the module file. Then in the module file, I use Exporter to set things up so you can then import the results() sub properly from the module:

accounts.pm module file

package accounts; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(results); sub account { my $file = shift; open my $fh, '<', $file or die $!; my @accounts = <$fh>; close $fh; chomp @accounts; return \@accounts; } sub results { my $file = shift; my $results = account($file); return $results; } 1;

script.pl script file

use strict; use warnings; use Data::Dumper; use lib '.'; use accounts qw(results); my $file = "accounts.txt"; my $newdata = results($file); print Dumper $newdata;

accounts.txt file:

a b c d e

Output:

$VAR1 = [ 'a', 'b', 'c', 'd', 'e' ];

Replies are listed 'Best First'.
Re^2: Returning data from a sub routine in a module.
by Anonymous Monk on Apr 19, 2016 at 20:08 UTC
    I see that you called account() from inside of results(), but I still have to pass my $file = "test.txt"; to account().

      I updated my post. The results() sub accepts a file name from the script, then it passes it to accounts() for processing, and then returns the results.

        The reason I posted was because I do not want to pass the my $file = "accounts.txt"; from the Perl script. I am trying to have it in the module:
        my $file = "test.txt"; my $accounts = account( file => $file, );