Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Where to declare?

by blackmateria (Chaplain)
on Oct 24, 2001 at 22:02 UTC ( #121216=note: print w/ replies, xml ) Need Help??


in reply to Where to declare?

Move the declaration of @dirlist after the nested sub (getAns), then pass it in as a hard reference:

sub getDirs() { sub getAns() { my ($dirlist) = @_; # new print qq(Enter a valid directory: ); chomp(my $ans = <STDIN>); if ($ans ne "") { if ($ans =~ m/foo/g) { push(@$dirlist, $ans); # note the @$dirlist &getAns; } else { print qq(That is not a valid directory!\n); &getAns; } } else { return @$dirlist; # note the @$dirlist } } my @dirlist; my @dirs = getAns(\@dirlist); # note the \@dirlist if (@dirs) { return @dirs; } else { die qq(No directories have been entered!\n); } }

For more information on hard references, take a look at perlref.

Just a stylistic note, you don't need to put the & on getAns to call if if you're already using () (the original has a line which read my @dirs = &getAns()). As a matter of fact, you should stay away from using & to call subroutines unless you understand how it's different from calling them with (). Calling with & makes @_ in the called subroutine an alias for @_ in the caller; () makes a copy instead. So changing the parameters in the subroutine could affect the variables used in the main program. This is probably not what you expect! Be careful. If I were you, I'd change the places in your code that call &getAns to getAns().

Anyway, hope this helps.


Comment on Re: Where to declare?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-12-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (162 votes), past polls