title: How to write elegant Code in Perl
body: <p>I’m a Perl beginner and I really love this language. I have written the following little program. This is one of the games on the website Codingame.com.</p>
  
  <pre><code>chomp($n=&lt;STDIN&gt;);
  if (!$n) {
     print 0;   
  } else {
     @t=split/ /,&lt;STDIN&gt;;
     foreach $i(0..$n-1) {
        $m=$t[$i]if(!$m)||(abs($t[$i])&lt;abs($m))||(($t[$i]==-$m)&amp;&amp;($t[$i]&gt;0));    
     }
  }print $m;
  </code></pre>
  
  <p>The goal of this code is to give the closest number to 0 of a list, and if there are <code>-4</code> and <code>4</code> for example, it will always return the positive number.</p>
  
  <ul>
  <li><code>$n</code> is the number of numbers in the list</li>
  <li><code>@t</code> is the list as an array</li>
  <li><code>$m</code> is the minimum</li>
  </ul>
  
  <p>This code seems to work, but it is difficult to read. I would like to learn some Perl secrets to make it shorter, more elegant, simple and maintainable.</p>
  
  <p>Could you give me advice to rewrite it? Maybe using the ternary operator or a one line <code>foreach</code> loop.</p>
  
  <p>I don't really want a perfect code as an answer, but some indications, and then I can search details and learn by myself thanks to your help.</p>