#!/usr/bin/perl use strict; use Benchmark; sub using_aref{ my $aref=[];
@$aref=(1,2,3,4,5); return $aref; } sub using_array{ my @array=(); @array=(1,2,3,4,5); return \@array; } And the winner is : timethese (1000000,{ using_aref => \&using_aref,using_array => \&using_array}); Benchmark: timing 1000000 iterations of using_aref, using_array... using_aref: 9 wallclock secs ( 7.99 usr + 0.00 sys = 7.99 CPU) @ 125125.13/s (n=1000000) using_array: 5 wallclock secs ( 5.20 usr + 0.00 sys = 5.20 CPU) @ 192381.69/s (n=1000000) José. > -----Original Message----- > From: Jenda Krynicky [mailto:[EMAIL PROTECTED]] > Sent: Monday, December 16, 2002 3:51 PM > To: [EMAIL PROTECTED] > Subject: Re: couple questions about refs > > > From: christopher j bottaro <[EMAIL PROTECTED]> > > hello again, > > all my c instincts tell me i shouldn't write a function > like this: sub > > myfunc() { > > my $aref = []; > > if ( some_cond ) { > > return undef; > > } > > else { > > #populate @{$aref} > > return $aref; > > } > > } > > but its cool, right? > > Yes this is fine in Perl. Perl simply does the right thing :-) > > > 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) > > > anyways, second question: how do i create/invoke a function ref? > > > > i wanna do something like this: > > sub myfunc() { > > #blah blah > > } > > my $fref = \&myfunc(); # is this right? > > without the (): > > my $fref = \&myfunc; > > > &{$fref}($arg1, $arg2); #is this right? are there any > shortcuts like > > $fref->($arg1, $arg2)? i know this is the syntax for invoking a > > method, but i don't wanna go thru the hassle of making a class > > Both these syntaxes are OK. No need for any classes or whatever. > > Jenda > > ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== > When it comes to wine, women and song, wizards are allowed > to get drunk and croon as much as they like. > -- Terry Pratchett in Sourcery > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > **** DISCLAIMER **** "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer". Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]