title: Perl: Format US phone number based on input
body: <p><strong><em><a href="https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation">This</a></em></strong> didn't answer my question.</p>
  
  <p>Possible inputs (may have whitespace): </p>
  
  <ul>
  <li><strong><em>full number</em></strong>: (XXX) XXX-XXXX</li>
  <li><strong><em>full number</em></strong>: XXX/XXX-XXXX</li>
  <li><strong><em>full number</em></strong>: XXXXXXXXXX</li>
  <li><strong><em>no area code</em></strong>: XXX-XXXX</li>
  <li><strong><em>no area code</em></strong>: XXXXXXX</li>
  <li><strong><em>extension only</em></strong>: XXXX</li>
  </ul>
  
  <p>So if you regex it for digits only <code>s/[^\d]//g</code>, you'll hopefully get 1 of 3 options:</p>
  
  <ul>
  <li>XXXXXXXXXX</li>
  <li>XXXXXXX</li>
  <li>XXXX</li>
  </ul>
  
  <p>Which I would like to format as so:</p>
  
  <ul>
  <li>XXX/XXX-XXXX</li>
  <li>XXX-XXXX</li>
  <li>XXXX</li>
  </ul>
  
  <p>Is the best way to do this to do <code>if</code> statements based on the length? Or is there a more one-liner approach?</p>