title: Print email addresses to a file in Perl
body: <p>I have been scouring this site and others to find the best way to do what I need to do but to no avail. Basically I have a text file with some names and email addresses. Each name and email address is on its own line. I need to get the email addresses and print them to another text file. So far all I have been able to print is the "no email addresses found" message. Any thoughts? Thanks!!</p>
  
  <pre><code>#!/usr/bin/perl
  
  open(IN, "&lt;contacts.txt") || die("file not found");
  #chooses the file to read
  open(OUT, "&gt;emailaddresses.txt");
  #prints file
  $none = "No emails found!";
  $line = &lt;IN&gt;;
  
  for ($line)
  {
      if ($line =~ /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/g)
      {
          print (OUT $line);
      }
      else
      {
          print (OUT $none);
      }
  }
  
  close(IN);
  close(OUT);
  </code></pre>