file access trouble

Discussion in 'Perl Misc' started by Chuck, Apr 7, 2004.

  1. Chuck

    Chuck Guest

    I am a new user to perl - trying to teach myself enough perl to do a
    specific task to be used in conjunction with other software I have
    developed.

    I am a software developer and use a web site for distribution of the
    software. The download folder of this web site uses password protection via
    ..htpasswd

    I created another windows based program that creates the .htpasswd file
    locally - each of my software users has their own username and password to
    download updates to the software - what I need to do with perl is to loop
    through the local .htpasswd file that is created by the other windows app
    and crypt() the password. I have already done this by looping through the
    local .htpasswd file in my windows based code and copying the password to be
    encrypted to the local clipboard on the computer that I use to maintain my
    user list. Then I call perl from within my windows based code, get the data
    from the clipboard, crypt() the password, and then copy it back to the
    clipboard, then get the clipboard data in my windows based code, and update
    the local .htpasswd file with the encrypted string for the password. Then
    after the process is done I use FTP to upload the .htpasswd file to the
    protected folder on the web server. This all works great but I want to loop
    through the entire file using perl rather that calling perl for each record
    in the file - this is rather slow because perl needs to be loaded for each
    record in the local .htpasswd file rather than only loading once.

    I have been through several tutorials and did google searches to see if I
    could determine why my perl script is not working but could not get it to
    work. I ran the script though the perl debugger and made sure there were no
    errors in the script that the debugger could find. I am using wperl.exe so
    the console window does not show but have also used perl.exe.

    I have been working on this for a couple of days but figured that some help
    from the perl experts could expedite this for me. Any suggestions or
    pointers will be greatly appreciated.

    The script runs without any error messages but does not appear to read data
    from the already existing .htpasswd file and it does not create the new
    ..htpasswd2 file that I rename to the original file name after the loop is
    finished - this is my script;

    use strict;
    use warnings;

    my $text1;
    my $text2;

    my $OldFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd";
    my $NewFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd2";

    open(OLD, '<', $OldFile) or die "$!\n";
    open(NEW, '>', $NewFile) or die "$!\n";

    while (<OLD>) {
    $text1 = substr($_,0,5);
    $text2 = crypt(substr($_,5,8),substr($_,13,2));
    print NEW "$text1 $text2 /n";
    }

    close OLD or die "$!\n";
    close NEW or die "$!\n";
    rename($NewFile,$OldFile) or die "$!\n";

    The files do not need to be locked as only one person will be running this
    script at any given time.

    --

    regards,

    Chuck
    CommPay Software
     
    Chuck, Apr 7, 2004
    #1
    1. Advertising

  2. Chuck

    Tore Aursand Guest

    On Wed, 07 Apr 2004 10:24:19 -0500, Chuck wrote:
    > my $text1;
    > my $text2;


    You don't need these.

    > my $OldFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd";
    > my $NewFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd2";


    No need for double quotes here. Save yourself trouble by doing this:

    my $OldFile = 'c:/cwicweb/exec/company/shared data/.htpasswd';
    my $NewFile = 'c:/cwicweb/exec/company/shared data/.htpasswd2';

    > while (<OLD>) {
    > $text1 = substr($_,0,5);
    > $text2 = crypt(substr($_,5,8),substr($_,13,2));
    > print NEW "$text1 $text2 /n";
    > }


    Could have been written as:

    while ( <OLD> ) {
    my $text1 = substr( $_, 0, 5 );
    my $text2 = crypt( substr($_, 5, 8), substr($_, 13, 2) );
    print NEW "$text1 $text2 \n";
    }

    Other than that, I don't see any "errors" in your script. Are you sure
    there aren't any error messages?


    --
    Tore Aursand <>
    "A teacher is never a giver of truth - he is a guide, a pointer to the
    truth that each student must find for himself. A good teacher is
    merely a catalyst." -- Bruce Lee
     
    Tore Aursand, Apr 7, 2004
    #2
    1. Advertising

  3. Chuck

    Bob Walton Guest

    Chuck wrote:

    > I am a new user to perl - trying to teach myself enough perl to do a
    > specific task to be used in conjunction with other software I have
    > developed.
    >
    > I am a software developer and use a web site for distribution of the
    > software. The download folder of this web site uses password protection via
    > .htpasswd
    >
    > I created another windows based program that creates the .htpasswd file
    > locally - each of my software users has their own username and password to
    > download updates to the software - what I need to do with perl is to loop
    > through the local .htpasswd file that is created by the other windows app
    > and crypt() the password. I have already done this by looping through the
    > local .htpasswd file in my windows based code and copying the password to be
    > encrypted to the local clipboard on the computer that I use to maintain my
    > user list. Then I call perl from within my windows based code, get the data
    > from the clipboard, crypt() the password, and then copy it back to the
    > clipboard, then get the clipboard data in my windows based code, and update
    > the local .htpasswd file with the encrypted string for the password. Then
    > after the process is done I use FTP to upload the .htpasswd file to the
    > protected folder on the web server. This all works great but I want to loop
    > through the entire file using perl rather that calling perl for each record
    > in the file - this is rather slow because perl needs to be loaded for each
    > record in the local .htpasswd file rather than only loading once.
    >
    > I have been through several tutorials and did google searches to see if I
    > could determine why my perl script is not working but could not get it to
    > work. I ran the script though the perl debugger and made sure there were no
    > errors in the script that the debugger could find. I am using wperl.exe so
    > the console window does not show but have also used perl.exe.
    >
    > I have been working on this for a couple of days but figured that some help
    > from the perl experts could expedite this for me. Any suggestions or
    > pointers will be greatly appreciated.
    >
    > The script runs without any error messages but does not appear to read data
    > from the already existing .htpasswd file and it does not create the new
    > .htpasswd2 file that I rename to the original file name after the loop is
    > finished - this is my script;
    >
    > use strict;
    > use warnings;
    >
    > my $text1;
    > my $text2;
    >
    > my $OldFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd";
    > my $NewFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd2";
    >
    > open(OLD, '<', $OldFile) or die "$!\n";
    > open(NEW, '>', $NewFile) or die "$!\n";
    >
    > while (<OLD>) {
    > $text1 = substr($_,0,5);
    > $text2 = crypt(substr($_,5,8),substr($_,13,2));
    > print NEW "$text1 $text2 /n";


    \----------------------------^


    > }
    >
    > close OLD or die "$!\n";
    > close NEW or die "$!\n";
    > rename($NewFile,$OldFile) or die "$!\n";

    ....

    Seems to work OK for me, copied verbatim (AS Perl build 806 on Windoze
    98SE). You might want to consider fixing the /n above that looks like
    you probably intended a \n.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Apr 10, 2004
    #3
  4. Chuck

    Chuck Guest

    Bob,

    Tried your suggestion - did not make any difference - my script does not
    create the NEW file. Using ActiveState latest version.

    --

    regards,

    Chuck
    CommPay Software

    "Bob Walton" <> wrote in message
    news:...
    > Chuck wrote:
    >
    > > I am a new user to perl - trying to teach myself enough perl to do a
    > > specific task to be used in conjunction with other software I have
    > > developed.
    > >
    > > I am a software developer and use a web site for distribution of the
    > > software. The download folder of this web site uses password protection

    via
    > > .htpasswd
    > >
    > > I created another windows based program that creates the .htpasswd file
    > > locally - each of my software users has their own username and password

    to
    > > download updates to the software - what I need to do with perl is to

    loop
    > > through the local .htpasswd file that is created by the other windows

    app
    > > and crypt() the password. I have already done this by looping through

    the
    > > local .htpasswd file in my windows based code and copying the password

    to be
    > > encrypted to the local clipboard on the computer that I use to maintain

    my
    > > user list. Then I call perl from within my windows based code, get the

    data
    > > from the clipboard, crypt() the password, and then copy it back to the
    > > clipboard, then get the clipboard data in my windows based code, and

    update
    > > the local .htpasswd file with the encrypted string for the password.

    Then
    > > after the process is done I use FTP to upload the .htpasswd file to the
    > > protected folder on the web server. This all works great but I want to

    loop
    > > through the entire file using perl rather that calling perl for each

    record
    > > in the file - this is rather slow because perl needs to be loaded for

    each
    > > record in the local .htpasswd file rather than only loading once.
    > >
    > > I have been through several tutorials and did google searches to see if

    I
    > > could determine why my perl script is not working but could not get it

    to
    > > work. I ran the script though the perl debugger and made sure there were

    no
    > > errors in the script that the debugger could find. I am using wperl.exe

    so
    > > the console window does not show but have also used perl.exe.
    > >
    > > I have been working on this for a couple of days but figured that some

    help
    > > from the perl experts could expedite this for me. Any suggestions or
    > > pointers will be greatly appreciated.
    > >
    > > The script runs without any error messages but does not appear to read

    data
    > > from the already existing .htpasswd file and it does not create the new
    > > .htpasswd2 file that I rename to the original file name after the loop

    is
    > > finished - this is my script;
    > >
    > > use strict;
    > > use warnings;
    > >
    > > my $text1;
    > > my $text2;
    > >
    > > my $OldFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd";
    > > my $NewFile = "c:\\cwicweb\\exec\\commpay\\shared data\\.htpasswd2";
    > >
    > > open(OLD, '<', $OldFile) or die "$!\n";
    > > open(NEW, '>', $NewFile) or die "$!\n";
    > >
    > > while (<OLD>) {
    > > $text1 = substr($_,0,5);
    > > $text2 = crypt(substr($_,5,8),substr($_,13,2));
    > > print NEW "$text1 $text2 /n";

    >
    > \----------------------------^
    >
    >
    > > }
    > >
    > > close OLD or die "$!\n";
    > > close NEW or die "$!\n";
    > > rename($NewFile,$OldFile) or die "$!\n";

    > ...
    >
    > Seems to work OK for me, copied verbatim (AS Perl build 806 on Windoze
    > 98SE). You might want to consider fixing the /n above that looks like
    > you probably intended a \n.
    >
    > --
    > Bob Walton
    > Email: http://bwalton.com/cgi-bin/emailbob.pl
    >
     
    Chuck, Apr 11, 2004
    #4
  5. Chuck

    Bob Walton Guest

    Chuck wrote:

    > Bob,
    >
    > Tried your suggestion - did not make any difference - my script does not
    > create the NEW file. Using ActiveState latest version.
    >
    >


    Well, here is the exact source I ran (verbatim with yours except for the
    file names and better die messages), along with the exact output:

    D:\junk>type junk444.pl
    use strict;
    use warnings;

    my $text1;
    my $text2;

    my $OldFile = "d:/junk/junk444.txt";
    my $NewFile = "d:/junk/junk444a.txt";

    open(OLD, '<', $OldFile) or die "Oops, couldn't open $OldFile, $!";
    open(NEW, '>', $NewFile) or die "Oops, couldn't open $NewFile, $!";

    while (<OLD>) {
    $text1 = substr($_,0,5);
    $text2 = crypt(substr($_,5,8),substr($_,13,2));
    print NEW "$text1 $text2 /n";
    }

    close OLD or die "$!\n";
    close NEW or die "$!\n";
    rename($NewFile,$OldFile) or die "$!\n";

    D:\junk>type junk444.txt
    asdflkjsdflkjsdflksdflkjsdflkj
    weroiuwqeroiuweroiwueroiuwerqweoiu
    xcvzxcvmnzxcvxzcvxzcvzxcmbnxczvmnbw

    D:\junk>perl junk444.pl

    D:\junk>type junk444.txt
    asdfl sdT3lI5wcQc1Y /nweroi weI0TeB.6rg/o /nxcvzx xzOM.vGz4CsCs /n
    D:\junk>ver

    Windows 98 [Version 4.10.2222]


    D:\junk>perl -v

    This is perl, v5.8.0 built for MSWin32-x86-multi-thread
    (with 1 registered patch, see perl -V for more detail)

    Copyright 1987-2002, Larry Wall

    Binary build 806 provided by ActiveState Corp. http://www.ActiveState.com
    Built 00:45:44 Mar 31 2003


    Perl may be copied only under the terms of either the Artistic License
    or the
    GNU General Public License, which may be found in the Perl 5 source kit.

    Complete documentation for Perl, including FAQ lists, should be found on
    this system using `man perl' or `perldoc perl'. If you have access to the
    Internet, point your browser at http://www.perl.com/, the Perl Home Page.


    D:\junk>

    The junk444a.txt file does not appear, since it was renamed over top of
    the original junk444.txt file, which is now gone.

    HTH.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Apr 11, 2004
    #5
  6. Chuck

    Chuck Guest

    Bob,

    I got my script working - there was a problem with my path settings. Thanks
    for the help.

    --

    regards,

    Chuck

    "Bob Walton" <> wrote in message
    news:...
    > Chuck wrote:
    >
    > > Bob,
    > >
    > > Tried your suggestion - did not make any difference - my script does not
    > > create the NEW file. Using ActiveState latest version.
    > >
    > >

    >
    > Well, here is the exact source I ran (verbatim with yours except for the
    > file names and better die messages), along with the exact output:
    >
    > D:\junk>type junk444.pl
    > use strict;
    > use warnings;
    >
    > my $text1;
    > my $text2;
    >
    > my $OldFile = "d:/junk/junk444.txt";
    > my $NewFile = "d:/junk/junk444a.txt";
    >
    > open(OLD, '<', $OldFile) or die "Oops, couldn't open $OldFile, $!";
    > open(NEW, '>', $NewFile) or die "Oops, couldn't open $NewFile, $!";
    >
    > while (<OLD>) {
    > $text1 = substr($_,0,5);
    > $text2 = crypt(substr($_,5,8),substr($_,13,2));
    > print NEW "$text1 $text2 /n";
    > }
    >
    > close OLD or die "$!\n";
    > close NEW or die "$!\n";
    > rename($NewFile,$OldFile) or die "$!\n";
    >
    > D:\junk>type junk444.txt
    > asdflkjsdflkjsdflksdflkjsdflkj
    > weroiuwqeroiuweroiwueroiuwerqweoiu
    > xcvzxcvmnzxcvxzcvxzcvzxcmbnxczvmnbw
    >
    > D:\junk>perl junk444.pl
    >
    > D:\junk>type junk444.txt
    > asdfl sdT3lI5wcQc1Y /nweroi weI0TeB.6rg/o /nxcvzx xzOM.vGz4CsCs /n
    > D:\junk>ver
    >
    > Windows 98 [Version 4.10.2222]
    >
    >
    > D:\junk>perl -v
    >
    > This is perl, v5.8.0 built for MSWin32-x86-multi-thread
    > (with 1 registered patch, see perl -V for more detail)
    >
    > Copyright 1987-2002, Larry Wall
    >
    > Binary build 806 provided by ActiveState Corp. http://www.ActiveState.com
    > Built 00:45:44 Mar 31 2003
    >
    >
    > Perl may be copied only under the terms of either the Artistic License
    > or the
    > GNU General Public License, which may be found in the Perl 5 source kit.
    >
    > Complete documentation for Perl, including FAQ lists, should be found on
    > this system using `man perl' or `perldoc perl'. If you have access to the
    > Internet, point your browser at http://www.perl.com/, the Perl Home Page.
    >
    >
    > D:\junk>
    >
    > The junk444a.txt file does not appear, since it was renamed over top of
    > the original junk444.txt file, which is now gone.
    >
    > HTH.
    > --
    > Bob Walton
    > Email: http://bwalton.com/cgi-bin/emailbob.pl
    >
     
    Chuck, Apr 15, 2004
    #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. Hypo
    Replies:
    6
    Views:
    454
  2. Cold Filtered
    Replies:
    0
    Views:
    468
    Cold Filtered
    Feb 18, 2005
  3. Prabhu Ramachandran

    Trouble with file.seek/file.tell on Win32?

    Prabhu Ramachandran, Aug 15, 2004, in forum: Python
    Replies:
    0
    Views:
    439
    Prabhu Ramachandran
    Aug 15, 2004
  4. Tim Peters
    Replies:
    0
    Views:
    618
    Tim Peters
    Aug 15, 2004
  5. Prabhu Ramachandran

    Re: Trouble with file.seek/file.tell on Win32?

    Prabhu Ramachandran, Aug 16, 2004, in forum: Python
    Replies:
    2
    Views:
    511
    Christos TZOTZIOY Georgiou
    Aug 16, 2004
Loading...

Share This Page