Perl - Using a Variable that is inside an if statement -
i've been putting perl script defines variable, , via if
statement assign new value variable.
i want use last assigned value if
statement , reference somewhere else outside if
altogether.
it appears when referencing it uses original value.
here's code:
## variables $filename = $input[0]; open $info, $datafile or die "can't open <$datafile> reading $!"; { while (my $line = <$info>) { chomp $line; @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 ### print "$filename / $permit / $ftpbase/$short_permit/$permit/$filename\n"; $ftp = net::ftp::throttle->new( "example.com", megabitspersecond => $throttlelvl, debug => $debuglvl ) or die "cannot connect: $@"; $ftp->login("anonymous", 'anonymous') or die "cannot login ", $ftp->message; $ftp->binary or die "unable set binary mode ", $ftp->message; if ($progressbar eq 1) { print "\n[$./$line_count] downloading $filename\n"; $remote_filesize = $ftp->size("/path/to/data/$short_permit/$permit/$filename"); if (!defined($remote_filesize)) { print "=> file not appear exist on ftp server\n"; if ($filename =~ m/_\s/) { print "=> attempting fix now\n"; $filename =~ s/_\s/, /g; $remote_filesize = $ftp->size("/path/to/data/$short_permit/$permit/$filename"); if (!defined($remote_filesize)) { print "=> failed!\n"; } } elsif ($filename =~ m/_\s\s/) { print "=> attempting fix now\n"; $filename =~ s/_\s\s/, /g; $remote_filesize = $ftp->size("/path/to/data/$short_permit/$permit/$filename"); if (!defined($remote_filesize)) { print "$filename\n"; print "=> failed!\n"; } } else { print "=> attempts resolve issue have failed.\n"; next; } } $remote_filesize = $ftp->size("/path/to/data/$short_permit/$permit/$filename"); print "filename: $filename\n"; --- snip snip - more data, not relevant--
the output name of file opened, not value after modification substitutions in if
statements.
this error correction: checks filename gets file, , if matches something, corrects it, , in end, have old variable $filename
new value want use outside if
.
i struggled understand code in question, because of upper case strings; both identifiers , comments
local identifiers in languages, including perl, written using lower-case letters, digits, , underscore _
. capital letters reserved global variables, , in perl's case package (class) names
identifiers of important variables use capital letters stand out rest; in example most identifiers all-capitals. diffifult understand structure of code if every identifier says it's important
i have looked @ code show, , have done best reorganise , simplify more legible , think intended. have added declarations of variables not declared within code sample, , have added think correct closing braces }
balance syntax compile
as others have noted, second conditional clause tests underscore followed two spaces never executed, because preceding test underscore followed one space have caught case. pointless use separate pattern match determine whether substitution s///
succeed, substitution alone nothing , returns false value if there no match
the intention write perl code you can understand, others may ask (like stack overflow) or may given job of maintaining software.
use strict; use warnings; ($progressbar, $line_count); ($datafile, $ftpbase, $short, $tiff_dl_location); ($throttlelvl, $debuglvl); @input; ## variables $filename = $input[0]; open $info, $datafile or die "can't open <$datafile> reading $!"; { while (<$info>) { chomp; ($filename, $permit) = split /:/; $filesize = -s "$tiff_dl_location/${permit}_${filename}"; $short_permit = substr $permit, 0, 2; ### debug ### print "$filename / $permit / $ftpbase/$short_permit/$permit/$filename\n"; $file_path = "/path/to/data/$short_permit/$permit/$filename"; $ftp = net::ftp::throttle->new( "example.com", megabitspersecond => $throttlelvl, debug => $debuglvl ) or die "cannot connect: $@"; $ftp->login(qw/ anonymous anonymous /) or die "cannot login: ", $ftp->message; $ftp->binary or die "unable set binary mode ", $ftp->message; if ($progressbar == 1) { print "\n[$./$line_count] downloading $filename\n"; $remote_filesize = $ftp->size($file_path); if (not defined $remote_filesize) { warn "=> file not appear exist on ftp server\n"; warn "=> attempting fix now\n"; $filename =~ s/_\s+/, /g; $remote_filesize = $ftp->size($file_path); if (not defined $remote_filesize) { warn "=> attempts resolve issue have failed.\n"; next; } } print "file name: $filename\n"; # --- snip snip - more data, not relevant --- } } }
Comments
Post a Comment