writing to file fails

Discussion in 'Perl Misc' started by Vahid, Jun 19, 2007.

  1. Vahid

    Vahid Guest

    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;
    Vahid, Jun 19, 2007
    #1
    1. Advertising

  2. Vahid

    Vahid Guest

    On Jun 19, 5:14 pm, Vahid <> wrote:
    > 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.
    Vahid, Jun 19, 2007
    #2
    1. Advertising

  3. Vahid

    Paul Lalli Guest

    On Jun 19, 5:14 pm, Vahid <> wrote:

    > 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 = <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); # 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
    Paul Lalli, Jun 19, 2007
    #3
  4. Vahid

    J. Gleixner Guest

    Vahid wrote:
    > 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 = <SHADOWfh> )

    > 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

    > }
    > close $SHADOWfh;
    > close $secSHADOWfh;
    >
    J. Gleixner, Jun 19, 2007
    #4
  5. Vahid

    Vahid Guest

    On Jun 19, 5:21 pm, Paul Lalli <> wrote:
    >
    > 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.
    Vahid, Jun 19, 2007
    #5
  6. Vahid

    Paul Lalli Guest

    On Jun 19, 5:39 pm, Vahid <> wrote:
    > On Jun 19, 5:21 pm, Paul Lalli <> wrote:
    >
    > > 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.


    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
    Paul Lalli, Jun 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    1
    Views:
    671
    John Saunders
    Nov 18, 2003
  2. HNguyen
    Replies:
    4
    Views:
    2,401
    HNguyen
    Dec 21, 2004
  3. Dmytro Bablinyuk

    ZIP: Writing binary file fails

    Dmytro Bablinyuk, Sep 6, 2005, in forum: Ruby
    Replies:
    5
    Views:
    174
    Tim Ferrell
    Sep 7, 2005
  4. Replies:
    2
    Views:
    294
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    342
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page