title: Perl - Command line input and STDIN
body: <p>I've created this script below for a assignment I have. It asks for a text file, checks the frequency of words, and lists the 10 words that appear the most times. Everything is working fine, but I need this script to be able to start via the command line as well as via the standard input.</p>
  <p>So I need to be able to write 'perl wfreq.pl example.txt' and that should start the script and not ask the question for a text file. I'm not sure how to accomplish this really. I think I might need a <em>while</em> loop at the start somewhere that skips the STDIN if you give it the text file on a terminal command line.</p>
  <p>How can I do it?</p>
  <h3>The script</h3>
  <pre><code>#! /usr/bin/perl
  
  use utf8;
  use warnings;
  
  print &quot;Please enter the name of the file: \n&quot; ;
  $file = &lt;STDIN&gt;;
  chop $file;
  
  
  open(my $DATA, &quot;&lt;:utf8&quot;, $file) or die &quot;Oops!!: $!&quot;;
  binmode STDOUT, &quot;:utf8&quot;;
  
  while(&lt;$DATA&gt;) {
      tr/A-Za-z//cs;
      s/[;:()&quot;.,!?]/ /gio;
      foreach $word (split(' ', lc $_)) {
          $freq{$word}++;
      }
  }
  
  foreach $word (sort { $freq{$b} &lt;=&gt; $freq{$a} } keys %freq) {
     @fr = (@fr, $freq{$word});
     @ord = (@ord, $word);
  }
  
  for ($v =0; $v &lt; 10; $v++) {
      print &quot; $fr[$v] | $ord[$v]\n&quot;;
  }
  </code></pre>