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 | |
by stevieb (Canon) on Apr 19, 2016 at 20:16 UTC | |
by Anonymous Monk on Apr 19, 2016 at 20:54 UTC |