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

I am getting the following error when I run the code,strangely if set the variable as $8k_m,if I remove the number "8" and change the variable as "$k_m",I see the error is gone,what am I doing wrong?

Error:- Bareword found where operator expected at ftp.pl line 15, near "$8k_m" (Missing operator before k_m?) Can't use global $8 in "my" at ftp.pl line 15, near "my $8" syntax error at ftp.pl line 15, near "$8k_m" Execution of ftp.pl aborted due to compilation errors. #!/usr/bin/perl -w use strict; use warnings; use Cwd qw(getcwd); use File::Copy; use File::Find; use Net::Telnet; use Net::FTP; use Getopt::Std; my %options=(); getopt("b",\%options); my $cwd = getcwd; my $8k_m= $options{b}; my ($input,$output); my %files = map {$_ => 1} qw(amss.mbn dsp.elf); find(sub { copy($File::Find::name, $cwd) or die "Can't cp $File::Find::name: +$!" if (delete $files{$_}); }, $8k_m); print "\nFTP.ing Modem and DSP images...\n"; my $ftp = Net::FTP->new("10.42.9.119", Debug => 0) or die "Cannot connect to the target: $@"; $ftp->login("user","user") or die "Cannot login ", $ftp->message; $ftp->cwd("/mnt/data") or die "Cannot change working directory ", $ftp->message; $ftp->binary(); $ftp->put("data.mbn") or die "put failed ", $ftp->message;; $ftp->put("file.elf") or die "put failed ", $ftp->message; $ftp->quit();

Replies are listed 'Best First'.
Re: Error when adding a number to a variable
by toolic (Bishop) on Apr 21, 2011 at 20:43 UTC
    A variable identifier can not begin with a number. From Variable names
    Usually this name is a single identifier, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits.
Re: Error when adding a number to a variable
by kennethk (Abbot) on Apr 21, 2011 at 20:43 UTC
    The issue is that $8k_m is not a valid variable name. As it says in Variable names in perldata:
    Usually this name is a single identifier, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits.

    The interpreter sees that as the variable $8 (the eighth regex capture buffer) followed by the bareword k_m.

Re: Error when adding a number to a variable
by Anonymous Monk on Apr 21, 2011 at 20:46 UTC

    Don't start your variable names with a number.

    Whenever you think you want to put a number in a variable name at all, you probably want either an array or a more generic name that won't become obsolete after things change.