Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm losing contents from my database and I'm not sure why. After switching lines around for over the past four hours I'm finally annoyed enough to see if anyone else has a suggestion.

The part we are concerned about is the $sig database and the final if loop on the bottom of the page. After filling out the form and clicking 'save' you get the message saying it's saved and it prints it to screen as assurance. However it doesn't stay in the database, it just disappears and I'm not assigning anything else to it.

I really need to know what I'm doing wrong, this is such a mental strain for me.

#!/usr/bin/perl -w use strict; use warnings; use POSIX; use CGI qw/:standard/; require SDBM_File; my %sig; my $sigsave = "sig.dbm"; my %emails; my $list = "list.dbm"; my $adminmail = "admin\@test.com"; my $sendmail = "/usr/lib/sendmail"; my $signature; tie %emails, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %emails ) { print "database unsuccessful $!.\n"; } tie %sig, 'SDBM_File', $sigsave, O_CREAT | O_RDWR, 0644; if ( !tied %sig ) { print "database unsuccessful $!.\n"; } print header, start_html('Email Management'); print "DB contents: $sig{$signature}\n"; print start_form(), table( Tr( td("Subject: "), td( textfield( -name => 'subject', -size => 40 ) ) ), Tr( td("Message: "), td( textarea( -name => 'message', -columns => 40, -rows => 5 ) ) ), Tr( td("Signature: "), td( textarea( -name => 'signature', -default => $sig{$signature}, -columns => 40, -rows => 5 ) ) ), Tr( td( checkbox_group( -name => 'todo', -values => [ 'use', 'save' ], -rows => 2, -columns => 2 ), ), td(submit) ), ), end_form(), hr(); # rid ourselves from those nasty params my $message = param('message'); my $subject = param('subject'); my $signature = param('signature'); my $todo = param('todo'); if ( param() ) { if ( $message && $subject eq "" ) { print "Your subject or email content was missing.\n"; } else { foreach my $key ( keys %emails ) { print "Things we have: $key <br>\n"; } print "<br>\n"; while ( my ( $key, $value ) = each(%emails) ) { # Email Subs, special commands $message =~ s/\[name\]/$value/; #[name] = user name open( MAIL, "| $sendmail -t" ); print MAIL "To: $key\n"; print MAIL "From: $adminmail\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; if ( $todo eq "use" && $signature ne "" ) { print MAIL "$signature\n"; } print MAIL ".\n"; close(MAIL); print "Email was sent to: $key !<br>"; } } if ( $todo eq "save" ) { print "<br>"; print "Saving to database<br>\n"; $sig{$signature} = $signature; print "\$sig{\$signature}: $sig{$signature}"; } } untie %emails; untie %sig;

Replies are listed 'Best First'.
Re: $var{$var1} = $var1; ?
by AcidHawk (Vicar) on Apr 07, 2003 at 08:16 UTC

    Just a note.

    Line 80 declares $signature again when you have already done this in line 15.

    "my" variable $signature masks earlier declaration in same scope at 248529.pl line 80.

    Remove the my on line 80.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      I removed the second my and a few other small changes but nothing helped. The $signature shows up in the email if it exists and it prints to screen doing a db test print. If you run the script over again the $sig{$signature} doesn't stick and you get an empty string.

      Any other suggestions?

      #!/usr/bin/perl -w use strict; use warnings; use POSIX; use CGI qw/:standard/; require SDBM_File; my %sig; my $sigsave = "sig.dbm"; my %emails; my $list = "list.dbm"; my $adminmail = "admin\@test.com"; my $sendmail = "/usr/lib/sendmail"; my $signature; tie %emails, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %emails ) { print "database unsuccessful $!.\n"; } tie %sig, 'SDBM_File', $sigsave, O_CREAT | O_RDWR, 0644; if ( !tied %sig ) { print "database unsuccessful $!.\n"; } print header, start_html('Email Management'); print "DB contents: $sig{$signature}\n"; print start_form(), table( Tr( td("Subject: "), td( textfield( -name => 'subject', -size => 40 ) ) ), Tr( td("Message: "), td( textarea( -name => 'message', -columns => 40, -rows => 5 ) ) ), Tr( td("Signature: "), td( textarea( -name => 'signature', -default => $sig{$signature}, -columns => 40, -rows => 5 ) ) ), Tr( td( checkbox_group( -name => 'todo', -values => [ 'use', 'save' ], -rows => 2, -columns => 2 ), ), td(submit) ), ), end_form(), hr(); # rid ourselves from those nasty params my $message = param('message'); my $subject = param('subject'); $signature = param('signature'); my $todo = param('todo'); if ( param() ) { if ( $message && $subject eq "" ) { print "Your subject or email content was missing.\n"; } else { foreach my $key ( keys %emails ) { print "Things we have: $key <br>\n"; } print "<br>\n"; while ( my ( $key, $value ) = each(%emails) ) { # Email Subs, special commands $message =~ s/\[name\]/$value/; #[name] = user name open( MAIL, "| $sendmail -t" ); print MAIL "To: $key\n"; print MAIL "From: $adminmail\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; if ( $todo eq "use" && $signature ne "" ) { print MAIL "$signature\n"; } print MAIL ".\n"; close(MAIL); print "Email was sent to: $key !<br>"; } } if ( $todo eq "save" ) { print "<br>"; print "Saving to database<br>\n"; $sig{$signature} = $signature; print "\$sig{\$signature}: $sig{$signature}"; } } untie %emails; untie %sig;
Re: $var{$var1} = $var1; ?
by Aragorn (Curate) on Apr 07, 2003 at 08:27 UTC
    The signature is saved to the database, but print $signature{$signature} with an empty $signature is not going to print it.

    Using the signature itself as a key is probably not what you want. I think it would be more logical to use the user name as the key to the signature database: $signature{$key} = $signature.

    Arjen