On Monday 16 December 2002 08:50 am, Jenda Krynicky wrote:
> > i know what i really should do is this:
> > sub myfunc()        {
> >  my @array;
> >  if ( some_cond )   {
> >   return undef;
> >  }
> >  else       {
> >   #populate @array
> >   return [ @array ];
> >  }
> > }
>
> Well ... I would use
>
>       return \@array;
>
> it's a bit more Perlish and actually I expect it to be quicker.
> (Benchmarking is left to the reader)

hmm, something is going off in my head that says this is scary.  @array is 
local to the function (lexically scoped as you would say in perl??) and you 
are returning a reference to it.  well when the function goes out of scope, 
isn't @array deallocated (popped off the stack) and now that reference you 
returned is pointing to garbage?

or is perl's garbage collection sophisticated enough to realize the difference 
between:
myfunc();
and
my $ref = myfunc();
and not deallocate @array in the second example?

also, i do understand that [ @array ] constructs and anonymous array, copies 
@array into it, then returns a reference to to the new anonymous array.  as 
opposed to \@array which merely returns a reference to something that already 
exists.  but i guess my questions is...well, i asked it above...about whether 
or not @array will still be in existance when the function ends...

thanks,
christopher

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to