in reply to Re^3: Perl variable scoping between functions
in thread Perl variable scoping between functions

Okay, But attempting the call this way will result in the file opening handle to be replaced with the count of the array.
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; }
In the scope of this, when you call the sub via  my @other = fLoadModules($modules); it will try to open the array count instead of the file. This had come up in my previous investigation/testing before I turned to the good people of perl monks for answers.

Replies are listed 'Best First'.
Re^5: Perl variable scoping between functions
by Corion (Patriarch) on Jul 18, 2018 at 12:07 UTC

    Have you tested fLoadModules in isolation, by passing it a hardcoded string?

    I think the original problem comes from this line, which assigns $modules the number of found modules:

    my $modules = fLoadModules $modules_path;

    If you want to receive a list back instead of the number of modules, you need to have a list on the left hand side of the assignment:

    my @modules = fLoadModules $modules_path;
      Aha! I think that this is certainly a likely cause, passing the path to the file as a hardcoded string will yield the required result so I think you are right that it could be the assignment that is incorrect.