Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. Subroutines > Item 46. Pass references instead of copies

Item 46. Pass references instead of copies

Two disadvantages of the “plain old” method of subroutine argument passing are that (1) even though you can modify its elements, you can’t modify an array or hash argument itself, and (2) copying an array or hash into @_ takes time. You can overcome both of these disadvantages with references (Item 58).

Passing reference arguments

When you pass arguments to a subroutine, Perl aliases @_ to them. It does this for efficiency. When you store them in variables, Perl then makes a copy. The more arguments you pass, the more work Perl has to do. For instance, you can write a subroutine to sum a list of numbers. It takes the list directly from the arguments:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial