file access trouble

C

Chuck

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
 
T

Tore Aursand

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?
 
B

Bob Walton

Chuck said:
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.
 
C

Chuck

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
 
B

Bob Walton

Chuck said:
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.
 
C

Chuck

Bob,

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

--

regards,

Chuck

Bob Walton said:
Chuck said:
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.
 

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