jmClifford has asked for the wisdom of the Perl Monks concerning the following question:
Hi. wrt the heading, is there anything to be said for passing a scalar like a string with a reference. I expect this should be considered better since it implies no coping process is used. Possibly it is of no concern for small strings (and small numbers)??
------------------------------------------------ code from web tutorial and added to by me ----------------------------------------- #!/usr/bin/perl use warnings; use strict; my @a = (1,3,2,6,8,4,9); my $m = &max(\@a); # passing an array by reference print "The max of @a is $m\n"; # prints max is 9 sub max{ my $aref = $_[0]; my $k = $aref->[0]; for(@$aref){ $k = $_ if($k < $_); } return $k; } my $string = "This is a string"; printWthStrRef(\$string); # passing string by reference sub printWthStrRef { my $aref = $_[0]; print ${$aref}; #prints the string }
Regards JC.....
|
|---|