in reply to Perl variable scoping between functions

For those who teased out the various pitfalls and solutions i thank you! I have solved it, my biggest pitfall was incorrectly passing data to the sub and trying to call the fLoadModules sub from another sub and main. As seen in the thread, indeed the most convenient way is to dump the data into an array and return it like so.
sub fLoadModules { my ($modules) = @_; my @array; open my $fh, "<", $modules or die "Couldn't open module file: $mod +ules"; while(<$fh>) { chomp; my ($module_id) = split /;/; push @array, $module_id; } close $fh; return @array; }
And in main() I have taken the argument via command line for the path to the folder containing the file needed.
my $modules_path = $opts{e};
Next I pass $module_path to the sub which will call the other sub (to retrieve the list from the file)
fCompose $dbh, $modules_path;
Finally from within that sub I can call fLoadModules to obtain the data I need.
sub fCompose { my($dbh, $modules_path) = @_; my @retstr = fLoadModules($modules_path); .... }
So for clarity, this was the issue and how it was rectified. Thank you once again to the contributors, it is as always a big help when its needed!

Replies are listed 'Best First'.
Re^2: Perl variable scoping between functions
by tobyink (Canon) on Jul 20, 2018 at 13:04 UTC

    This is why I asked "why are you calling the sub twice?"

    You appeared to be calling the sub once to pass arguments to it, then again (without passing any arguments) to retrieve the result. That's not how subs work.