title: Convert to a subroutine in Perl
body: <p>I have this code, to extract in this case names, using a given list: </p>
  
  <p>I want to convert this code in a subroutine, I have tried in a different ways but it just doesn't give me what I want.  And the other question is, how to add all the results (in this case: Barney Rubble, Wilma Flinstone) to the @match array without using a <code>foreach (@match){ push (@extract, $_)</code>?</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  my @array1 = ( "Fred Flinstone", "Wilma Flinstone", "Barney Rubble", "Betty Rubble" );
  my @list = ("Ba", "Wil");
  my (@match, @extract);
  
  for (@list){
      my $unit = $_;
      chomp $unit;
  
      @match = grep (/$unit/, @array1);
  
      foreach (@match){
          push (@extract, $_);
      }
  }
  
  foreach (@extract){
      print "$_\n";
  }
  </code></pre>