writing to file fails

V

Vahid

Hi all;
I am trying to read two files and write the result into the third file
but keep getting "..undefined value as a symbol.." error. Can someone
please tell me what am I doing wrong? Thank you.

#!/usr/bin/perl -w
#
#
#use strict; # if uncomment this, I get many errors.
use warnings;
$ENV{'PATH'} = "/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin:";
my $SHADOW = etc_shadow;
my $secSHADOW = sec_passwd;
$|=1;
#
#
#

open (SHADOWfh, "<$SHADOW") || die "Can not open the file: $SHADOW\n";
open (secSHADOWfh, ">$secSHADOW") || die "Can not open the file:
$secSHADOW\n";

my @lines = <SHADOWfh> ;
foreach my $line (@lines) {
my ($username,$pass,$lastchange) = split /:/, $line;
my $sec_lastupdate = ($lastchange*86400);
printf ("%s:\n\tpassword = %s\n\tlastupdate = %d\n\tflags = \n
\n",$username,$pass,$sec_lastupdate); # OK here
printf $secSHADOWfh "%s:\n\tpassword = %s\n\tlastupdate = %d\n
\tflags = \n\n",$username,$pass,$sec_lastupdate; # NOT OK here.
}
close $SHADOWfh;
close $secSHADOWfh;
 
V

Vahid

Hi all;
I am trying to read two files and write the result into the third file
Sorry, actually just reformatting UNIX's /etc/shadow file to
user:
password =
lastupdate =
flags =
and then writing it to another file. I get the following error after
running the script:

Can't use an undefined value as a symbol reference at /tmp/
mksec_passwd.pl line 22, <SHADOWfh> line 860.
Line 860 is the last line in the shadow file.
Thanks again.
 
P

Paul Lalli

I am trying to read two files and write the result into the third file
but keep getting "..undefined value as a symbol.." error. Can someone
please tell me what am I doing wrong?

Yes. You're ignoring Perl telling you about your errors, and instead
asking us to tell you about them instead. Very inefficient of you.
#!/usr/bin/perl -w
#
#
#use strict; # if uncomment this, I get many errors.

No kidding. That's because you made many errors. You should be
FIXING the errors it tells you about, not plugging your fingers in
your ears and screaming LA LA LA I CAN'T HEAR YOU!!!
use warnings;
$ENV{'PATH'} = "/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin:";
my $SHADOW = etc_shadow;
my $secSHADOW = sec_passwd;

Here's your first two errors. You're assigning barewords to these
variables. Perl doesn't know what they are, so it *guesses* that
they're strings, because there's no subroutines with those names in
scope. use strict will prevent this guessing and tell you to fix this
problem. Strings in perl are delimited by quotes:
my $SHADOW = 'etc_shadow';
my $secSHADOW = 'sec_passwd';
$|=1;
#
#
#

open (SHADOWfh, "<$SHADOW") || die "Can not open the file: $SHADOW\n";
open (secSHADOWfh, ">$secSHADOW") || die "Can not open the file:
$secSHADOW\n";

my @lines = <SHADOWfh> ;
foreach my $line (@lines) {

You're reading the entire array into an array at once, and then
processing the array one element at a time. Very wasteful and
inefficient. Instead, read one line and process that line before you
read another:
while (my $line = said:
my ($username,$pass,$lastchange) = split /:/, $line;
my $sec_lastupdate = ($lastchange*86400);
printf ("%s:\n\tpassword = %s\n\tlastupdate = %d\n\tflags = \n
\n",$username,$pass,$sec_lastupdate); # OK here
printf $secSHADOWfh "%s:\n\tpassword = %s\n\tlastupdate = %d\n
\tflags = \n\n",$username,$pass,$sec_lastupdate; # NOT OK here.}

Of course it's not. $secSHADOWfh is a variable that doesn't
previously exist. It's not a filehandle. The filehandle is
secSHADOWfh. `use strict;` would have told you aobut this error. But
you decided you didn't want to hear Perl tell you about these errors,
and instead asked Usenet what you did wrong instead.
close $SHADOWfh;
close $secSHADOWfh;

Once again, Perl will tell you there's no such variables with these
names if you let it.

Paul Lalli
 
J

J. Gleixner

Vahid said:
Hi all;
I am trying to read two files and write the result into the third file
but keep getting "..undefined value as a symbol.." error. Can someone
please tell me what am I doing wrong? Thank you.

#!/usr/bin/perl -w
#
#
#use strict; # if uncomment this, I get many errors.

Good. Then uncomment and fix the errors.
use warnings;
$ENV{'PATH'} = "/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin:";
my $SHADOW = etc_shadow;
my $secSHADOW = sec_passwd;

my $SHADOW = 'etc_shadow';
my $secSHADOW = 'sec_passwd';
$|=1;
#
#
#

open (SHADOWfh, "<$SHADOW") || die "Can not open the file: $SHADOW\n";
open (secSHADOWfh, ">$secSHADOW") || die "Can not open the file:
$secSHADOW\n";

Print the reason why open failed, which will be stored in $!.

perldoc -f open
my @lines = <SHADOWfh> ;
foreach my $line (@lines) {

Replace those with:

while( my $line = said:
my ($username,$pass,$lastchange) = split /:/, $line;
my $sec_lastupdate = ($lastchange*86400);

my $sec_lastupdate = $lastchange * 86400;
printf ("%s:\n\tpassword = %s\n\tlastupdate = %d\n\tflags = \n
\n",$username,$pass,$sec_lastupdate); # OK here
printf $secSHADOWfh "%s:\n\tpassword = %s\n\tlastupdate = %d\n
\tflags = \n\n",$username,$pass,$sec_lastupdate; # NOT OK here.

Using the version of open, you're using above, that would be:

printf secSHADOWfh

perldoc -f open
 
V

Vahid

Once again, Perl will tell you there's no such variables with these
names if you let it.
The error was not descriptive enough for me so I did not know what to
do with it, your explanation was.
I modified my code to the following and everything is fine now.
Thanks.

#!/usr/bin/perl -w
#
use strict;
use warnings;
$ENV{'PATH'} = "/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin:";
my $SHADOW = 'etc_shadow';
my $secSHADOW = 'sec_passwd';
$|=1;
#
open (SHADOWfh, "<$SHADOW") || die "Can not open the file: $SHADOW\n";
open (secSHADOWfh, ">$secSHADOW") || die "Can not open the file:
$secSHADOW\n";

while (my $line = <SHADOWfh>)
{
my ($username,$pass,$lastchange) = split /:/, $line;
my $sec_lastupdate = ($lastchange*86400);
printf ("%s:\n\tpassword = %s\n\tlastupdate = %d\n\tflags = \n
\n",$username,$pass,$sec_lastupdate);
printf secSHADOWfh "%s:\n\tpassword = %s\n\tlastupdate = %d\n
\tflags = \n\n",$username,$pass,$sec_lastupdate;
}
close SHADOWfh;
close secSHADOWfh;

Thanks again.
 
P

Paul Lalli

The error was not descriptive enough for me so I did not know
what to do with it, your explanation was.

perldoc perldiag
for a list of all perl errors/warnings and their meanings.

Or just put
use diagnostics;
in your script, to have Perl give you the detailed explanation.

Paul Lalli
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top