why the variable become local?

A

ai2003lian

I'm new with perl. Sorry if this question is too naive. I have the
follwing piece of code, and I don't know why the variable $login apear
local:

#!/usr/local/bin/perl
package downlaod;

use Net::FTP;
use vars qw($ftp_server, $login, $password);

# Some settings

$idx = 0;
$next_arg = $ARGV[idx];
if(!$next_arg){
printUsage();
exit;
}

while($next_arg){
if($next_arg == "-h"){
$ftp_server=$ARGV[++$idx]; #the host name of the ftp
server
if (!$ftp_server){
printUsage();
exit;
}
print "$ftp_server \n";
}elsif($next_arg == "-u"){
$login=$ARGV[++$idx]; #user name
if (!$login){
printUsage();
exit;
}
print "$login \n";
}elsif($next_arg == "-p"){
$password=$ARGV[++$idx];
if(!$password ){
printUsage();
exit;
}
print "$password\n";
}
$next_arg = $ARGV[++$idx];
}

print "login= $login \n"; #problem here

sub printUsage(){
print "\aUsage: downlaod_zip_file -h <ftp server> -u <user
name> -p password \n";
}

when I try to print $login after the while loop, $login is empty! Why
is that? Please help!

Thanks in advance.
 
M

Matt

use Getopt::Std;

Get you command line options this way.

I'm new with perl. Sorry if this question is too naive. I have the
follwing piece of code, and I don't know why the variable $login apear

#!/usr/local/bin/perl
package downlaod;

use Net::FTP;
use vars qw($ftp_server, $login, $password);

# Some settings

$idx = 0;
$next_arg = $ARGV[idx];
if(!$next_arg){
printUsage();
exit;
}

while($next_arg){
if($next_arg == "-h"){
$ftp_server=$ARGV[++$idx]; #the host name of the ftp
server
if (!$ftp_server){
printUsage();
exit;
}
print "$ftp_server \n";
}elsif($next_arg == "-u"){
$login=$ARGV[++$idx]; #user name
if (!$login){
printUsage();
exit;
}
print "$login \n";
}elsif($next_arg == "-p"){
$password=$ARGV[++$idx];
if(!$password ){
printUsage();
exit;
}
print "$password\n";
}
$next_arg = $ARGV[++$idx];
}

print "login= $login \n"; #problem here

sub printUsage(){
print "\aUsage: downlaod_zip_file -h <ftp server> -u <user
name> -p password \n";
}

when I try to print $login after the while loop, $login is empty! Why
is that? Please help!

Thanks in advance.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @l41g2000cwc.googlegroups.com:
I'm new with perl. Sorry if this question is too naive.

You should take the time to read the posting guidelines for
comp.lang.perl.misc and follow the advice given there.
follwing piece of code, and I don't know why the variable $login apear
local:

I don't know what it means for a variable to appear local.
#!/usr/local/bin/perl
package downlaod;

Is that a typo?
use Net::FTP;
use vars qw($ftp_server, $login, $password);

Now, these are other typos.

If you had asked perl, it would have told you:

Possible attempt to separate words with commas at s__t.pl line 9.
'$ftp_server,' is not a valid variable name under strict vars at s__t.pl
line 9
BEGIN failed--compilation aborted at s__t.pl line 9.

where line 9 refers the use vars statement.

You should always have

use strict;
use warnings;

at the beginning of your script. In case you prefer to live without those
two, then you should not post your code here.

So, add those to your script, get rid of the globals, then post here if
you encounter any problems.

Sinan.
 
A

A. Sinan Unur

@z14g2000cwz.googlegroups.com:

[ Please take the time to read the posting guidelines for this group.
use Getopt::Std;

Get you command line options this way.

How does that show the OP what he is doing wrong? Did you really have to
quote his whole script without responding to any specific part of it?

Sinan
 
A

A. Sinan Unur

(e-mail address removed) wrote in @l41g2000cwc.googlegroups.com:
# Some settings

$idx = 0;
$next_arg = $ARGV[idx];
if(!$next_arg){
printUsage();
exit;
}

print_usage() unless @ARGV;

while($next_arg){
if($next_arg == "-h"){

You can replace this whole craziness with:

#!/usr/local/bin/perl

use strict;
use warnings;

use constant USAGE =><<USAGE
Usage: download_zip_file -h <ftp server> -u <user name> -p <password>
USAGE
;


die USAGE unless @ARGV;

my %args = @ARGV;

die USAGE unless(
defined $args{-h}
and defined $args{-u}
and defined $args{-p}
);

login_to_ftp_server_and_download(\%args)
or die "Error logging on to $args{-h} for $args{-u}\n";

sub login_to_ftp_server_and_download {
return;
}

__END__

or use one of the modules available on CPAN:

http://search.cpan.org/modlist/Option_Parameter_Config_Processing

Sinan
 
M

Martin Kissner

A. Sinan Unur wrote :
(e-mail address removed) wrote in @l41g2000cwc.googlegroups.com:


print_usage() unless @ARGV;

Just for my own understanding:
Shouldn't that be:

print_usage() and exit unless @ARGV;

Regards
Martin
 
A

A. Sinan Unur

A. Sinan Unur wrote :

Just for my own understanding:
Shouldn't that be:

print_usage() and exit unless @ARGV;

Of course ... Thanks for catching that.

Sinan.
 
B

Big and Blue

Martin said:
Just for my own understanding:
Shouldn't that be:

print_usage() and exit unless @ARGV;

Not if your print_usage() function exits, which it usually should,
given that it is unlikely you wish to do anything else having shown the usage.
 
M

Martin Kissner

Big and Blue wrote :
Martin Kissner wrote:
Not if your print_usage() function exits, which it usually should,
given that it is unlikely you wish to do anything else having shown the usage.

That's probablly true.
You could do without the function as well.

@ARGV or die "usage: do it the right way!";

This way a reader of the code would have the usage information in the
upper part of the script.

Regards
Martin
 
A

A. Sinan Unur

Big and Blue wrote :


That's probablly true.
You could do without the function as well.

@ARGV or die "usage: do it the right way!";

It seems like you did not read all of my message:
 
M

Martin Kissner

A. Sinan Unur wrote :
It seems like you did not read all of my message:

You are right.
I was not so much interested in the download part of the script since I
use curl and .netrc for stuff like this.
Sorry.
[...]
 
B

Bill Smith

--snip
}elsif($next_arg == "-u"){
$login=$ARGV[++$idx]; #user name
if (!$login){
printUsage();
exit;
}
print "$login \n";
-- snip --

when I try to print $login after the while loop, $login is empty! Why
is that? Please help!



The symptom has nothing to do with the scope of the variable.
The variable $login is overwritten due to the use of the "==" operator
rather than the "eq" operator in the fragment above. You would have
found this and several other serious errors by using "use warnings".


In this program, the warnings related to prototypes are the only ones
that can be ignored. I recommend against trying to use prototypes
without
a compelling reason.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top