in reply to Perl variable scoping between functions

it doesn't seem to work correctly ... it runs into difficulty

These aren't really useful descriptions of the problem, sorry. Since you haven't shown how you are calling the sub, much less provided an SSCCE, it's pretty hard to help.

Update: That said, your sub doesn't return anything, perhaps that's the problem? Without an SSCCE it's still just a guess however.

  • Comment on Re: Perl variable scoping between functions

Replies are listed 'Best First'.
Re: Perl variable scoping between functions
by Bryan882 (Novice) on Jul 18, 2018 at 10:10 UTC
    Aha, That would be useful i guess. From another function I have been calling it this way. I am trying to put the file into an array which I can later make use of.
    my @retarr = fLoadModules($modules);

      Thanks. Your sub as written does not return anything so the @retarr array will be empty. You need to return a list like this:

      #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @retarr = (); print "At init " . Dumper (\@retarr); @retarr = void (); print "After void " . Dumper (\@retarr); @retarr = getlist (); print "After getlist " . Dumper (\@retarr); exit; sub void { # doesn't return anything } sub getlist { return (0, 1, 2); }

      That is an SSCCE. See also return. HTH.

        This is where an issue of scope gets raised in my head, how is it possible to return a value contained within the while loop and also return it from the sub which contains the while loop. I didn't think that it was possible.