Rabenschwinge has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am having a problem for which I don't quite understand the reason...

Perl complains that I am trying to redefine variables, even when I am just accessing them.

Could anyone explain this to me, please?

1 #!/usr/bin/env perl 2 use strict; 3 use warnings; 4 use Net::FTP; 5 6 my $host = 'ftp.example.com'; 7 my $user = 'smith'; 8 my $pass = 'secret'; 9 my $dir_name = '/'; 10 my $target_dir = '/var/tmp/ftp-download'; 11 my $tmp_dir = '/var/tmp'; 12 my $last_index_file = '/var/tmp/last-index'; (...) 42 my $ftp = Net::FTP->new($host); 43 $ftp->login($user, $pass); 44 $ftp->cwd($dir_name); 45 my @dir = $ftp->ls();
~/bin# ./test.pl "my" variable $host masks earlier declaration in same scope at ./ftp-t +est.pl line 42. "my" variable $ftp masks earlier declaration in same scope at ./ftp-te +st.pl line 43. "my" variable $user masks earlier declaration in same scope at ./ftp-t +est.pl line 43. "my" variable $pass masks earlier declaration in same scope at ./ftp-t +est.pl line 43. "my" variable $ftp masks earlier declaration in same scope at ./ftp-te +st.pl line 44.
FIXED. There were two lines wrong I didn't quote. Instead of !~ to see whether a regular expression does not match I wrote ~! twice in the lines above that. I don't know how this could lead to the error, but it seems fixed now.
# Append a '/' to directories if needed if ($target_dir ~! m{\/$}) { $target_dir .= '/'; } if ($tmp_dir ~! m{\/$}) { $tmp_dir .= '/'; }
# Append a '/' to directories if needed $target_dir .= '/' unless ($target_dir =~ m{\/$}); $tmp_dir .= '/' unless ($tmp_dir =~ m{\/$});

Replies are listed 'Best First'.
Re: Redifined variable?
by almut (Canon) on Sep 30, 2009 at 14:28 UTC

    Maybe something like this?  (not quite... but close)

    #!/usr/bin/env perl use strict; use warnings; my $host = 'ftp.example.com'; my $user = 'smith'; my $pass = 'secret'; my \ # <-- my $foo = $host; print $user, $pass; __END__ "my" variable $host masks earlier declaration in same scope at ./79833 +5.pl line 11. "my" variable $user masks earlier declaration in same scope at ./79833 +5.pl line 12. "my" variable $pass masks earlier declaration in same scope at ./79833 +5.pl line 12. syntax error at ./798335.pl line 9, near "my \" Execution of ./798335.pl aborted due to compilation errors.
Re: Redifined variable?
by moritz (Cardinal) on Sep 30, 2009 at 14:11 UTC
    Sometimes you get such warnings if there's a syntax error in your code, and even though that syntax error is reported later than the warnings, it's a good idea to ignore all other warnings or errors until you fixed the syntax error.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Redifined variable?
by JavaFan (Canon) on Sep 30, 2009 at 14:33 UTC
    Are these all the error you're getting? You're sure you're not missing a quote somewhere?
Re: Redifined variable?
by Anonymous Monk on Sep 30, 2009 at 13:59 UTC
    The code you have posted doesn't reproduce the warnings you've shown. diagnostics, splain
    "my" variable $host masks earlier declaration in same scope at ./ftp-t +est.pl line 42 (#1) (W misc) A "my", "our" or "state" variable has been redeclared in +the current scope or statement, effectively eliminating all access to the prev +ious instance. This is almost always a typographical error. Note that + the earlier variable will still exist until the end of the scope or un +til all closure referents to it are destroyed.
Re: Redifined variable?
by venkatesan_G02 (Sexton) on Sep 30, 2009 at 14:19 UTC