Forum Moderators: coopster & phranque

Message Too Old, No Replies

function doubling my data! why?

         

tarek990

7:44 pm on May 15, 2004 (gmt 0)

10+ Year Member



hello,
im new to perl.. im editting a perl program i purchased..
the problem: whenever i call this function twice (once for every element i want to edit) it modifies the intended element (what it is supposed to do) AND duplicated all the data in the .dat file (the database file)..

the function:
[sub modify_element
{
my ($element, $new_value) = @_;
if(&pm_check eq "yes")
{
&init($wholeline);
my $s = $input{'session'};
%input = %values;
$input{'session'} = $s;
if ($element ne "")
{$input{$element} = $new_value;}
$input{'action'} = "modified";
&pm_modify;
}
else { &err($error_message{'no permission'});&end; } }]

how can i call this function more than once without my data doubling?
or what can i do?
any suggestions or help is appreciated..
thanks,
Tarek

volatilegx

2:22 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, tarek990 :)

The trouble here is that we don't have enough info about the script to comment on it. There are a lot of subroutines here which we don't know the workings of. I recommend you take your question to the author of the script.

IanKelley

10:13 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Most likely you've got a varible or array being appended to somewhere. Solve the problem by initializing it to '' or undefing it at the beginning of the function.

tarek990

8:05 am on May 24, 2004 (gmt 0)

10+ Year Member



hello,
thanks for the replies.. i cant go to the script writer coz my support period has expired..
here is the pm_modify function


sub pm_modify
{
foreach (keys %values) {
if (not exists $input{$_}) {
$input{$_} = $values{$_};
}
}
my %before = %values;
$modify = 1;
&pm_delete;
&pm_register;
my $changes = "";
foreach(@base){
if($input{$_} ne $before{$_}){
if($input{$_} eq " " && $before{$_} eq ""){
next;
}
if($_ eq "action"){
next;
}
$changes .= "\n------------------------------\n$_:\nbefore:$before{$_}\nafter:$input{$_}\n";
}
}
#if($changes ne ""){
#&sendemail($from, $from, "Modification Changes for $input{'login'}", $changes);
#}
}

what variable should i initialize or declare as ".
im really new to this so please help..

thanks,
Tarek

IanKelley

7:04 pm on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$changes looks like a likely candidate. :-)

tarek990

9:31 pm on May 24, 2004 (gmt 0)

10+ Year Member



thanks for the reply..
$changes is already declared as "" in the function pm_modify as you can see up there..
any more sugestions?
thanks,
Tarek

killermonkey

6:18 pm on May 30, 2004 (gmt 0)



I think you should help this program practice good programming technique by setting up your 'foreach' loops like this:

foreach $var (keys %input) {
$input{$var} = 'BLAH';
}

I personnally hate it when people use $_ or other crazy variables because they can easily be overwritten or corrupted without you even knowing it!

I suggest leaving Perl's internal variables to perl's functions and use your own on your functions.

Can you post an example of the output this function produces and what you put in as input? Thanks!