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 45. Use wantarray to write subroutines returning ...

Item 45. Use wantarray to write subroutines returning lists

You probably know already that subroutines can return either scalar or list values. Perhaps you have written both kinds of subroutine. You also probably understand the significance of scalar and list contexts (Item 12). In an idle moment, you may have even wondered how something like the following works:

sub sorted_text_files {
  my $dir = shift;
  opendir my ($dh), $dir or die "eh?: $!";

  # not relevant to wantarray, but we have to
  # add the directory prefix here to make this work
  my @files = grep { -T } map { "$dir/$_" } readdir $dh;
  sort @files;
}


  

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