title: Perl - Using a Variable that is inside an if statement
body: <p>I've been putting together a Perl script that defines a variable, and then via an <code>if</code> statement will assign a new value to the variable.</p>
  
  <p>I want to use the last assigned value from the <code>if</code> statement and reference it somewhere else outside the <code>if</code> altogether.</p>
  
  <p>It appears that when referencing it that it uses the original value.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>## VARIABLES
  my $FILENAME = $input[0];
  
  open my $info, $DATAFILE or die "can't open &lt;$DATAFILE&gt; for reading $!";
  
  {
    while (my $line = &lt;$info&gt;) {
  
      chomp $line;
      my @input = split(':', $line);
      chomp(@input);
  
      $FILENAME     = $input[0];
      $PERMIT       = $input[1];
      $FILESIZE     = -s "$TIFF_DL_LOCATION/$PERMIT\_$FILENAME";
      $SHORT_PERMIT = substr($PERMIT, 0, 2);
  
      ### DEBUG ONLY ###
      print "$FILENAME / $PERMIT / $FTPBASE/$SHORT_PERMIT/$PERMIT/$FILENAME\n";
  
      my $ftp = Net::FTP::Throttle-&gt;new(
        "example.com",
        MegabitsPerSecond =&gt; $THROTTLELVL,
        Debug             =&gt; $DEBUGLVL
      ) or die "Cannot connect: $@";
  
      $ftp-&gt;login("anonymous", 'anonymous') or die "Cannot login ", $ftp-&gt;message;
      $ftp-&gt;binary or die "Unable to set binary mode ", $ftp-&gt;message;
  
      if ($PROGRESSBAR eq 1) {
  
        print "\n[$./$LINE_COUNT] Downloading $FILENAME\n";
        my $REMOTE_FILESIZE = $ftp-&gt;size("/PATH/TO/DATA/$SHORT_PERMIT/$PERMIT/$FILENAME");
  
        if (!defined($REMOTE_FILESIZE)) {
  
          print "=&gt; FILE DOES NOT APPEAR TO EXIST ON FTP SERVER\n";
  
          if ($FILENAME =~ m/_\s/) {
  
            print "=&gt; ATTEMPTING TO FIX NOW\n";
            $FILENAME =~ s/_\s/, /g;
            $REMOTE_FILESIZE = $ftp-&gt;size("/PATH/TO/DATA/$SHORT_PERMIT/$PERMIT/$FILENAME");
  
            if (!defined($REMOTE_FILESIZE)) {
              print "=&gt; FAILED!\n";
            }
          }
          elsif ($FILENAME =~ m/_\s\s/) {
  
            print "=&gt; ATTEMPTING TO FIX NOW\n";
            $FILENAME =~ s/_\s\s/, /g;
            $REMOTE_FILESIZE = $ftp-&gt;size("/PATH/TO/DATA/$SHORT_PERMIT/$PERMIT/$FILENAME");
  
            if (!defined($REMOTE_FILESIZE)) {
              print "$FILENAME\n";
              print "=&gt; FAILED!\n";
            }
          }
          else {
            print "=&gt; ALL ATTEMPTS TO RESOLVE THE ISSUE HAVE FAILED.\n";
            next;
          }
        }
  
        $REMOTE_FILESIZE = $ftp-&gt;size("/PATH/TO/DATA/$SHORT_PERMIT/$PERMIT/$FILENAME");
        print "FILENAME: $FILENAME\n";
  
        --- SNIP SNIP - MORE DATA, NOT RELEVANT--
  </code></pre>
  
  <p>The output I get is the name of the file that was originally opened, not the value after modification by the substitutions in the <code>if</code> statements.</p>
  
  <p>This is error correction: it checks the filename that it gets from the file, and if it matches something, it corrects it, and in the end, I have an old variable <code>$FILENAME</code> with a new value that I want to use outside the <code>if</code>.</p>