title: Is there a better way of writing this code to avoid redundancy?
body: <p>I have a segment of code in a program, which accepts GNU style input from a pipe (which is a list of file names). If STDIN does not contain data, I need to accept input from a predetermined text file containing file names.</p>
  
  <p>I find myself needing to write redundant code. Is it possible to simplify this bit of code to avoid redundancy? </p>
  
  <pre><code>sub downloadlinkgen {
      my $fh;
      print "Printing links\n";
      if ($getfilelist==1) {
          open $fh, '&lt;', "fuzzyfile" or die $!;
          while (&lt;$fh&gt;) {
              chomp ($_);     
              (my $fname,my $path, my $suffix) = fileparse($_);
              my ($name, $ext) = $fname =~ /(.*)\.(.*)/;      
              my $newfile=$path.$name.".$ext";
              $newfile =~ s/\s/%20/g; 
              $newfile =~ s/\/root/http:\/\/myip/;        
              print $newfile."\n";        
          }
      } else {        
          while (&lt;&gt;) {
              chomp ($_);     
              (my $fname,my $path, my $suffix) = fileparse($_);
              my ($name, $ext) = $fname =~ /(.*)\.(.*)/;      
              my $newfile=$path.$name.".$ext";
              $newfile =~ s/\s/%20/g; 
              $newfile =~ s/\/root/http:\/\/myip/;        
              print $newfile."\n";        
          }
      }
  }
  </code></pre>