When you call a sub in Perl (a subroutine - a named block of code), you pass in a list of parameters in the list called @_. It's easy and straightforward ... but as the length of your list increases, it starts to become less that efficient.
Say - for example - I had a 35 Mbyte log file (and I really did yesterday, when I was looking at our web server log) ... then passing it across to a sub would duplicate the list into @_, and if I copied it into a named list in the sub that would be a third copy - over 100 Mbytes of memory swallowed up. Here's how that code might look:
sub mygrep {
($look4,@within) = @_;
@send_back = reverse(grep(/$look4/i,@within));
return @send_back;
}
open (FH,"shortie");
@stuff = <FH>;
@interesting = mygrep("horse",@stuff);
print @interesting;
But if you pass a REFERENCE to the list across to your sub, you can save yourself all the duplication - change
@stuff to
\@stuff in the call, collect the parameter in
$within rather than
@within inside the sub, then reference it via
@$within rather than
@within when you search through it. The code does not look much different:
sub mygrep {
($look4,$within) = @_;
@send_back = reverse(grep(/$look4/i,@$within));
return @send_back;
}
open (FH,"shortie");
@stuff = <FH>;
@interesting = mygrep("horse",\@stuff);
print @interesting;
but the performance certain does change dramatically!
If you're a newcomer to Perl you may find this sort of thing perplexing .. but help is to hand! Subjects such as this are covered on our
Perl Programming course. And if you're already somewhat into Perl, but want to go a little further, we run a
Perl for larger projects course which covers handling of large data sets, object orientation, databases and more.
"Let me show you how that works ...."
Copying the list - lots of duplication
 | Passing a reference - only the one list
 |
(written 2009-03-07 05:41:45)
Associated topics are indexed under
P209 - Subroutines in Perl [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[1921] Romeo and Julie - (2008-12-04)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[969] Perl - $_ and @_ - (2006-12-07)
[775] Do not duplicate your code - (2006-06-23)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[531] Packages in packages in Perl - (2005-12-16)
[357] Where do Perl modules load from - (2005-06-24)
[308] Call by name v call by value - (2005-05-11)
[96] Variable Scope - (2004-10-22)
Some other Articles
Extra PHP ExamplesCopyright, Portability and other nontechnical web site issuesSetting up a MySQL database from PHPConverting to Perl - the sort of programs you will writeEfficient calls to subs in Perl - avoid duplication, gain speedPlaying CatchupPerl - lists do so much more than arraysMelksham Industrial Static mirroring through HTTrack, wget and othersEast of Melksham Countryside