It is so much clearer to make the intent clear in the calling code,my $ref = [ $foo -> bar( $stuff) ];
And yet, it is so profligate. This way of obtain a reference to a million item array built within a sub:
C:\test>p1 sub x1{ my @a; $a[ $_ ] = $_ for 1 .. 1e6; return @a };; print time; $r = [ x1() ]; print time;; 1229112810.36413 1229112815.5985
Takes 5 seconds and uses 50 MB of RAM.
This way:
C:\test>p1 sub x2{ my @a; $a[ $_ ] = $_ for 1 .. 1e6; return \@a };; print time; $s = x2(); print time;; 1229112742.05163 1229112742.43096
Takes under half a second and uses 20MB of RAM.
If every time someone asked google to give them a reference to wikipedia, they transmitted the whole 4.4GB, people would rightly find the wasting of scarse resources distasteful.
If you going to go the "context is bad" route, always return the reference and let the caller expand it if they need to.
In reply to Re^2: Use of wantarray Considered Harmful (bad use)
by BrowserUk
in thread Use of wantarray Considered Harmful
by kyle
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |