I know it's a bit evil, but you could probably hook into Perl magic (see perlguts):<br><br> foreach my $person ( $people ) {<br> ...<br> }<br><br>Here, $people is a magic perl variable which checks what context it is being used in.<br>
<br>However, it's evil so forget I said anything.<br><br>Alfie<br><br><div class="gmail_quote">On Fri, Feb 19, 2010 at 11:27 AM, Toby Corkindale <span dir="ltr"><<a href="mailto:toby.corkindale@strategicdata.com.au">toby.corkindale@strategicdata.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On 19/02/10 10:16, Sam Watkins wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
A quick question. Supposing I want to return an array from my_func(), I can do<br>
like:<br>
<br>
...<br>
return @foo;<br>
}<br>
<br>
@foo = my_func();<br>
<br>
but I normally do it like this because I feel like it should be more efficient:<br>
<br>
...<br>
return \@foo;<br>
}<br>
<br>
$foo = my_func();<br>
<br>
This has the unfortunate effect of converting my array to an array ref. If I<br>
wanted to reverse that, I think I could use:<br>
<br>
@foo = @{my_func()};<br>
<br>
which is getting ugly and inefficient too. The same thing happens when<br>
returning hash variables, except that I suppose it is even more inefficient to<br>
return it via a list return compared to a reference.<br>
<br>
My question is, does perl actually optimize this so it sucks less than I am<br>
naively supposing it does? Also, is there any way to make like an alias @foo<br>
for @$foo, so you can treat an array reference as a normal array without<br>
writing @$foo all the time? (and also for hashes)<br>
</blockquote>
<br></div>
No, Perl does not optimise it for you, so returning references is definitely better for performance.<br>
There isn't any language syntax for the aliasing you're talking about either.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
These concerns make me reluctant to use normal perl @array and %hash types at<br>
all. If I have to pass and return things by reference, and can't alias these<br>
back to normal @array and %hash types, I would prefer to use references for<br>
everything in order to be consistent and avoid having to rewrite code when I<br>
suddenly need to pass some variable to or from a sub. This would make the @foo<br>
%bar syntax useless for anything but the one-liners. So I'm hoping there is<br>
some workaround or optimization, and a way to alias them.<br>
</blockquote>
<br></div>
Luckily there is. Use the Moose. I think Drew mentioned that in the quoted but unrelated text I cut from the bottom of this email. Have you investigated Moose yet?<br>
<br>
Cheers,<br><font color="#888888">
Toby</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Melbourne-pm mailing list<br>
<a href="mailto:Melbourne-pm@pm.org" target="_blank">Melbourne-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/melbourne-pm" target="_blank">http://mail.pm.org/mailman/listinfo/melbourne-pm</a><br>
</div></div></blockquote></div><br>