Gossamer Forum
Home : General : Perl Programming :

filling fields from $in

Quote Reply
filling fields from $in
i can't figure out what's wrong here. see the lines below # why doesn't this work. thanks

Code:
%rec = &get_record($in{$db_key});

my (%tmp);

if ($in{'reunion'}) {

foreach $col (@db2_cols) {
if ($in{$col}) { $tmp{$col} = $in{$col}; }
}
$db2_key = $in{'Userid'};
my (%save_in) = %in;
# undef %in; commented because $tmp didn't work
&switch_to_register;


my %rec2 = &get_record2($db2_key);

if ($rec2{'Userid'}) {
# modify record
foreach $col (@db2_cols) {
# if ($tmp{$col}) { $rec2{$col} = $tmp{$col}; } # why doesn't this work
if ($in{$col}) { $rec2{$col} = $in{$col}; } # this works
}
$db2_key = $rec2{'Userid'};
$db2_key_pos = '1';
&modify_record2(%rec2);
}
#####
else {
# add record
$rec2{'Display'} = 'Yes';
$rec2{'Source'} = 'member';
foreach $col (@db2_cols) {
# if ($tmp{$col}) { $rec2{$col} = $tmp{$col}; } # why doesn't this work

if ($in{$col}) { $rec2{$col} = $in{$col}; } # this works
}
$db2_key = $rec2{'Userid'};
$db2_key_pos = '1';
&add_record2(%rec2);

}

%in = %save_in;
}
Quote Reply
Re: [delicia] filling fields from $in In reply to
Have you tried doing:

Code:
use Data::Dumper;
print Dumper(%tmp);

to see what values are in there?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] filling fields from $in In reply to
i don't understand how to use that in the middle of a subroutine?
Quote Reply
Re: [delicia] filling fields from $in In reply to
Just add it after:

Code:
foreach $col (@db2_cols) {
if ($in{$col}) { $tmp{$col} = $in{$col}; }
}

So if you only want you to see it (i.e its a live site, and being used a lot), just wrap something that checks for your IP:

Code:
if ($ENV{REMOTE_ADDR} eq "123.123.123.123") {
use Data::Dumper;
print "Content-Type: text/html\n\n";
print Dumper(%tmp);
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] filling fields from $in In reply to
duh. couldn't get data dumper to work. but i changed:
Code:
foreach $col (@db2_cols) {
if ($in{$col}) { $tmp{$col} = $in{$col}; }
}
to
Code:
%tmp = %in;
got rid of %save_in

then changed
Code:
if ($in{$col}) { $rec2{$col} = $in{$col}; }

to
Code:
if ($tmp{$col}) { $rec2{$col} = $tmp{$col}; }

it works now. i still have no idea why it didn't work before but this is actually shorter.
Quote Reply
Re: [delicia] filling fields from $in In reply to
Cool. You should really work out how to use Data::Dumper to debug variables, as it's super helpful (lets you see the structure and values in a variable). Maybe even dump to your STDERR:

Code:
use Data::Dumper;
print STDERR Dumper($var)

Then tail your error log, and you should see the output from it:

Code:
tail -n20 -f /path/to/your/error.log

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [delicia] filling fields from $in In reply to
I am no Perl pro but my best guess would be that your problem is based on your copy beeing a pointer not a copy.
By doing the undef you undef the original data, if I am right the following might help you:
https://stackoverflow.com/...ng-a-hashref-in-perl

Regards

n||i||k||o
Quote Reply
Re: [Andy] filling fields from $in In reply to
what if i wrote to a file instead of

print Dumper(%tmp);

then i could look at the file?

something like this:
my ($output) = Dumper(%tmp); # is this valid???
my ($dump_file_name) = "path/whatever.txt";
open (DB, ">$dump_file_name") or &cgierr("error unable to open db file: $dump_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $dump_file_name.\nReason: $!");
}
print DB $output;
close DB;

then i ftp or whatever to look at the file
Quote Reply
Re: [delicia] filling fields from $in In reply to
Hi,

Dumper($vat) literall just prints out the structure of a variable. For example:

Code:
my @bla = qw/foo bar test/;
my $test = {
foo => "bar",
something => \@bla,
1 => 2,
};


use Data::Dumper;
print Dumper($test);

That prints out:

Code:
$VAR1 = {
'1' => 2,
'foo' => 'bar',
'something' => [
'foo',
'bar',
'test'
]
};

Basically, it lets you look into a variable and see the structure

Code:
something like this:
my ($output) = Dumper(%tmp); # is this valid???
my ($dump_file_name) = "path/whatever.txt";
open (DB, ">$dump_file_name") or &cgierr("error unable to open db file: $dump_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $dump_file_name.\nReason: $!");
}
print DB $output;
close DB;

No :)

If you wanted to output the contents into a file, you could do it with something like:

Code:
open (DEBUG, ">>/path/to/write/file.txt")
print DEBUG Dumper($var). "\n";
close(DEBUG);

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] filling fields from $in In reply to
open (DEBUG, ">>/path/to/write/file.txt")
print DEBUG Dumper($var). "\n";
close(DEBUG);

where does $var come from? is it always $var? or would it be %tmp or something in my case?
Quote Reply
Re: [delicia] filling fields from $in In reply to
Quote:
where does $var come from? is it always $var? or would it be %tmp or something in my case?

It would be whatever you want to see the structure of ;) In your case, it could well be %tmp or %in, etc

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!