CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
i have a bug in a code i made please help

 



gerble1000
User

Jan 15, 2012, 2:21 PM

Post #1 of 23 (2006 views)
i have a bug in a code i made please help Can't Post

this code gets parameters from my website into variables.
the first 2 are the username and password
the rest are names and numbers.
the code i have gets the variables and puts them into a txt file on 1 line..example
johnsmith,123pass,mother,07878767655,dad,07565633456,name,number,name,number,.......

the name and number are contact info that has not been filled in yet.
the problem im getting is every now and then it puts the code in wrong.
it would put "mber" in another line and for some strange reason it would put a trailing space on the end of the contacts ?


here is the code





Code
 
use CGI::Carp qw( fatalsToBrowser );



print "Content-Type: text";


#######################################################################################

use CGI qw/ :standard /;



use CGI;









my $query = new CGI;


my $username = $query->param('u');
my $password = $query->param('p');




my $result1 = $query->param('a01');
my $result2 = $query->param('a02');
my $result3 = $query->param('a03');
my $result4 = $query->param('a04');
my $result5 = $query->param('a05');
my $result6 = $query->param('a06');
my $result7 = $query->param('a07');
my $result8 = $query->param('a08');
my $result9 = $query->param('a09');
my $result10 = $query->param('a10');
my $result11 = $query->param('a11');
my $result12 = $query->param('a12');
my $result13 = $query->param('a13');
my $result14 = $query->param('a14');
my $result15 = $query->param('a15');
my $result16 = $query->param('a16');
my $result17 = $query->param('a17');
my $result18 = $query->param('a18');
my $result19 = $query->param('a19');
my $result20 = $query->param('a20');



if ($result1 eq "" )
{
$result1 = "empty";
}
if ($result2 eq "" )
{
$result2 = "empty";
}

if ($result3 eq "" )
{
$result3 = "empty";
}

if ($result4 eq "" )
{
$result4 = "empty";
}

if ($result5 eq "" )
{
$result5 = "empty";
}

if ($result6 eq "" )
{
$result6 = "empty";
}

if ($result7 eq "" )
{
$result7 = "empty";
}

if ($result8 eq "" )
{
$result8 = "empty";
}
if ($result9 eq "" )
{
$result9 = "empty";
}

if ($result10 eq "" )
{
$result10 = "empty";
}

if ($result11 eq "" )
{
$result11 = "empty";
}
if ($result12 eq "" )
{
$result12 = "empty";
}

if ($result13 eq "" )
{
$result13 = "empty";
}

if ($result14 eq "" )
{
$result14 = "empty";
}
if ($result15 eq "" )
{
$result15 = "empty";
}
if ($result16 eq "" )
{
$result16 = "empty";
}
if ($result17 eq "" )
{
$result17 = "empty";
}

if ($result18 eq "" )
{
$result18 = "empty";
}

if ($result19 eq "" )
{
$result19 = "empty";
}
if ($result20 eq "" )
{
$result20 = "empty";
}















if ($result2 eq "Username" )
{
goto SKIP;
}
if ($result4 eq "Password" )
{
goto SKIP;
}



if ($result2 eq "empty" )
{
goto SKIP;
}


if ($result2 eq "saved" )
{
goto SKIP;
}
if ($result2 eq "" )
{
goto SKIP;
}
if ($password eq "" )
{
goto SKIP;
}
if ($username eq "" )
{
goto SKIP;
}
if ($username eq "username" )
{
goto SKIP;
}






open(F,"contacts.txt");
my @list=<F>;close F;
my $this="$username,$password";
my @f=grep /$this/,@list;

close (F);


my $data = "@f";

if ($data eq "" )
{
open(F, '>>:utf8', 'contacts.txt');
print F "$username,$password,$result1,$result2,$result3,$result4,$result5,$result6,$result7,$result8,$result9,$result10,$result11,$result12,$result13,$result14,$result15,$result16,$result17,$result18,$result19,$result20\n";
close F;
}


else
{
open (IN, "+<contacts.txt");

@file = <IN>;

seek IN,0,0;

foreach $file (@file){
$file =~ s/$data/$username,$password,$result1,$result2,$result3,$result4,$result5,$result6,$result7,$result8,$result9,$result10,$result11,$result12,$result13,$result14,$result15,$result16,$result17,$result18,$result19,$result20\n/g;
print IN $file;
}
close IN;


}







SKIP:

print $query->header;



my $result1 = $query->param('a01');
my $result2 = "saved";
my $result3 = $query->param('a03');
my $result4 = "saved";
my $result5 = $query->param('a05');
my $result6 = "saved";
my $result7 = $query->param('a07');
my $result8 = "saved";
my $result9 = $query->param('a09');
my $result10 = "saved";
my $result11 = $query->param('a11');
my $result12 = "saved";
my $result13 = $query->param('a13');
my $result14 = "saved";
my $result15 = $query->param('a15');
my $result16 = "saved";
my $result17 = $query->param('a17');
my $result18 = "saved";
my $result19 = $query->param('a19');
my $result20 = "saved";


print "$result1,$result2,$result3,$result4,$result5,$result6,$result7,$result8,$result9,$result10,$result11,$result12,$result13,$result14,$result15,$result16,$result17,$result18,$result19,$result20";



Quote

the comma is a deliminator on the recieving javascript


rovf
Veteran

Jan 16, 2012, 5:09 AM

Post #2 of 23 (1981 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

Aside from general style questions, I see the following problems with your program:

(1) You don't have use strict; use warnings fatal => qw(all); in your program, which means that many errors will get unnoticed.

(2) You don't check your input for newlines or trailing space (not a problem in general, but might be related to the issue you reported).

(3) You update the file in-place. You didn't write which platform you are on. If you are on a Mac or on Linux, this probably wouldn't hurt, but if you are on Windows, this is certainly not something I would recommend.

(4) You don't have error handling (for example, if the 'open' failed). This is not related to the question you asked, but you should nevertheless add it to your code.

HTH

Ronald


gerble1000
User

Jan 16, 2012, 6:31 AM

Post #3 of 23 (1978 views)
Re: [rovf] i have a bug in a code i made please help [In reply to] Can't Post

i am using windows platform.
is there a specific part which you think is causing this problem..
i will address all the issues you mentioned though.


rovf
Veteran

Jan 16, 2012, 9:32 AM

Post #4 of 23 (1973 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

Basically, you don't want to position (seek) in Windows text files, because of the Windows line ending translation. However in your case, you seek only to position 0, so this should not matter. Didn't notice it the first time.

However:

(1) You are (for whatever reason) read from the file, then start overwriting it from beginning. The overwriting stops somewhere in the middle, leaving garbage at the end, if the data being written, is shorter than the data on the file.

(2) Are you aware that $data in s/$data/..../ is a regular expression? This means that if, for example, $password contains a character which has special meaning in a regexp, will cause pretty unpredictable behaviour.

Did you alread put strict and warnings in your code and re-run it? Before that, it doesn't make much sense searching for all the bugs.


FishMonger
Veteran / Moderator

Jan 16, 2012, 12:14 PM

Post #5 of 23 (1968 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

Personally, I think you should start over from scratch. What you have here is very poorly written code.


gerble1000
User

Jan 16, 2012, 12:28 PM

Post #6 of 23 (1964 views)
Re: [FishMonger] i have a bug in a code i made please help [In reply to] Can't Post

i think your right :(
what would you do as a simple save method.

i need to save the varables in a new line if the username and password dont exist.
if it does exist overwrite the line that contains the username and password.
that is basicly all i want to do


FishMonger
Veteran / Moderator

Jan 16, 2012, 12:34 PM

Post #7 of 23 (1962 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

I would load the data file into a HoH (Hash of Hashes) and update the hash with the new/updated record and then output the data to the file; overwriting the original after making a backup.

Using DBD::CSV could simplify the process.
http://search.cpan.org/~hmbrand/DBD-CSV-0.33/lib/DBD/CSV.pm


(This post was edited by FishMonger on Jan 16, 2012, 12:36 PM)


gerble1000
User

Jan 16, 2012, 12:36 PM

Post #8 of 23 (1958 views)
Re: [FishMonger] i have a bug in a code i made please help [In reply to] Can't Post

mmmm
gerble1000 = newbie
i didnt get any of that m8..
i work better with examples to play with :)


FishMonger
Veteran / Moderator

Jan 16, 2012, 12:40 PM

Post #9 of 23 (1957 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

I need to work on other projects right now, but will post a short example when I can.


gerble1000
User

Jan 16, 2012, 2:37 PM

Post #10 of 23 (1953 views)
Re: [FishMonger] i have a bug in a code i made please help [In reply to] Can't Post

thankyou :)


gerble1000
User

Jan 16, 2012, 3:06 PM

Post #11 of 23 (1951 views)
Re: [rovf] i have a bug in a code i made please help [In reply to] Can't Post

im trying to get the bug to show itself but i cant seem to mess the code up.
knowing the code is poor what do you think it would take for the txt to have garbage in it..
what would have to be in the variable to make this code corrupt the txt file layout


gerble1000
User

Jan 18, 2012, 8:20 AM

Post #12 of 23 (1905 views)
Re: [rovf] i have a bug in a code i made please help [In reply to] Can't Post

your absolutely right
i just changed the data for a shoter string and it gave garbage at the end..
if anyone can fix this or make me a better perl script that doesnt have this bug ..i will give them 10 thats left in my freelancer account..
you must have an account on freelancer though :)
anyone interested


rovf
Veteran

Jan 19, 2012, 1:35 AM

Post #13 of 23 (1896 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

You are using UTF8 Encoding. Could it be that what you consider as garbage, is related to some special Unicode character?

Where exactly does the garbage come in, and how does it look like?

Did you verify your data using Data::Dumper and as a hexdump? If yes, you should post your findings.

BTW, maybe you find this useful:

http://www.perlmonks.org/?node_id=111481

http://search.cpan.org/~dcantrell/Data-Hexdumper-3.00/lib/Data/Hexdumper.pm

http://search.cpan.org/~ftassin/Data-HexDump-0.02/lib/Data/HexDump.pm


gerble1000
User

Jan 21, 2012, 2:34 AM

Post #14 of 23 (1850 views)
Re: [rovf] i have a bug in a code i made please help [In reply to] Can't Post

the garbage seems to be 3 characters from somewhere else on the txt file..
mber or ber
thats what mainly appears when i try to change a contacts name to a smaller one.
for instance i log in with username and password and change
stephanie 07878787878 to steph 07878787878
i would get the change but at the end of the text file it would put some garbage. like mber wich is part of the name,number,name,number.
not sure how to prevent this


BillKSmith
Veteran

Jan 21, 2012, 4:32 AM

Post #15 of 23 (1848 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

I think that you are keeping the last few characters of the original file. This is the expected behavior when you overwrite a file with a shorter one. Remember Ronald's very first reply. He warned that this is risky. Close the file and reopen for output rather than seeking back to the beginning.
Good Luck,
Bill


gerble1000
User

Jan 21, 2012, 5:38 AM

Post #16 of 23 (1845 views)
Re: [BillKSmith] i have a bug in a code i made please help [In reply to] Can't Post

is there any chance you could modify the original code to accept this change as im not an expert on perl...... please


BillKSmith
Veteran

Jan 21, 2012, 6:55 AM

Post #17 of 23 (1839 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post


Code
    open( IN, "+<contacts.txt" ); 
@file = <IN>;
# seek IN, 0, 0;

close IN;

open( IN, '>', 'contacts.txt' );


foreach $file (@file) {
$file
=~ s/$data/$username,$password,$result1,$result2,$result3,$result4,.........
print IN $file;
}
close IN;



This is the minimum change to fix the problem of garbage at the end. However, I recommend a complete rewrite. Make all the improvements others have already suggested. In the mean time, keep you contacts file well backed up.
Good Luck,
Bill


gerble1000
User

Jan 21, 2012, 7:22 AM

Post #18 of 23 (1835 views)
Re: [BillKSmith] i have a bug in a code i made please help [In reply to] Can't Post

thankyou i will give that a go in a minute :)


FishMonger
Veteran / Moderator

Jan 21, 2012, 7:37 AM

Post #19 of 23 (1832 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

Here's an example of the approach I'd take, which loads the contacts data into a Hash-of-Hashes.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my $contacts = 'contacts.txt';
open my $contacts_fh, '<:encoding(UTF-8)', $contacts
or die "failed to open '$contacts' for reading <$!>";

# build a HoH to hold the contacts info
my %contacts;
while ( my $contact = <$contacts_fh> ) {
chomp $contact;
my ($user, $pass, @results) = split /,/, $contact;
$contacts{$user} = {
pass => $pass,
results => \@results,
}
}
close $contacts_fh;

# lets dump out the data to see the structure
print Dumper \%contacts;

# change username 'janesmith' to 'jane'
$contacts{'jane'} = delete $contacts{'janesmith'};

# lets look at the results
print Dumper \%contacts;


open $contacts_fh, '>:encoding(UTF-8)', $contacts
or die "failed to open '$contacts' for writing <$!>";

# output updated data to the file
foreach my $contact ( sort keys %contacts ) {
print {$contacts_fh} join(',', $contact,
$contacts{$contact}{'pass'},
@{$contacts{$contact}{'results'}},
);
print {$contacts_fh} "\n";
}
close $contacts_fh;


=pod
=begin comment

In my test case, the contents of contacts.txt is:
janesmith,123pass,mother,07878767655,mom,07565633456,name,number,name,number
johnsmith,123pass,father,07878767657,dad,07565633458,name,number,name,number

=end comment
=cut



gerble1000
User

Jan 21, 2012, 8:09 AM

Post #20 of 23 (1827 views)
Re: [BillKSmith] i have a bug in a code i made please help [In reply to] Can't Post

that worked great chears :)


gerble1000
User

Jan 21, 2012, 8:11 AM

Post #21 of 23 (1826 views)
Re: [FishMonger] i have a bug in a code i made please help [In reply to] Can't Post

this looks a lot simpler and easier to understand..
i just cant get it to work..
i will keep playing with this example thankyou guys


FishMonger
Veteran / Moderator

Jan 21, 2012, 8:17 AM

Post #22 of 23 (1825 views)
Re: [gerble1000] i have a bug in a code i made please help [In reply to] Can't Post

What part is not working?

Here's the output I get from that code.

Code
C:\testing>gerble.pl 
$VAR1 = {
'janesmith' => {
'pass' => '123pass',
'results' => [
'mother',
'07878767655',
'mom',
'07565633456',
'name',
'number',
'name',
'number'
]
},
'johnsmith' => {
'pass' => '123pass',
'results' => [
'father',
'07878767657',
'dad',
'07565633458',
'name',
'number',
'name',
'number'
]
}
};
$VAR1 = {
'jane' => {
'pass' => '123pass',
'results' => [
'mother',
'07878767655',
'mom',
'07565633456',
'name',
'number',
'name',
'number'
]
},
'johnsmith' => {
'pass' => '123pass',
'results' => [
'father',
'07878767657',
'dad',
'07565633458',
'name',
'number',
'name',
'number'
]
}
};



FishMonger
Veteran / Moderator

Jan 21, 2012, 8:18 AM

Post #23 of 23 (1824 views)
Re: [FishMonger] i have a bug in a code i made please help [In reply to] Can't Post

And the resulting contacts.txt file gets changed to:

Quote
jane,123pass,mother,07878767655,mom,07565633456,name,number,name,number
johnsmith,123pass,father,07878767657,dad,07565633458,name,number,name,number


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives