Wiggins d Anconia wrote:
I have a number of functions in a program I'm writing that return a
reference to
an array or hash. In the functions, I declare the variable to return
with 'my'
which I'm finding out is bad. Should I declare variables to return
from a
function with 'our'? Do I need to make sure I don't have conflicting
variable
names from other functions? What pitfalls do I need to know about when
doing this?
Need to address why you think creating them with 'my' is bad when returning them first, it isn't (as long as you are truly returning them). Use 'my' until you know you don't :-).
See if this helps:
http://perl.plover.com/FAQs/Namespaces.html
It is *well* worth the read...
I think that 'my' is bad because I have something similar to:
my %tree;
sub return_an_arrayref() { my @array = ('thing1', 'thing2', 'thing3'); return [EMAIL PROTECTED]; }
sub build_tree() { foreach(@thing) { $tree{$_} = return_an_arrayref(); } }
use Data::Dumper; print Dumper(%tree);
The output shows a bunch of empty arrays or arrays with all undef
elements under
each key in %tree. I know return_an_arrayref() is returning data
because I can
print all the elements out in build_tree().
Ah, turn on strict and warnings. See if that helps, if not come back :-). This is why these two pragma are so critical.
For me your code will not run.
It was more pseudocode than anything. I can't seem to duplicate the problem with test code. One thing I forgot to mention is that these functions are called recursively as far as 15 levels deep. Would that cause issues when returning a ref to a 'my'ed array from a function?
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>