Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Perl variable scoping between functions

by hippo (Bishop)
on Jul 18, 2018 at 10:02 UTC ( [id://1218731]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found