in reply to Re: Changing the Value Assigned To A Hash Key
in thread Changing the Value Assigned To A Hash Key

Didn't work using an always fail function with any of the following tests:

sub check_name { return 0; } unless (&check_name($app{"first_name"})) { # code } # , unless (&check_name($app{"first_name"}) == 1) { # code } # , unless (&check_name($app{"first_name"}) eq 1) { # code } # , or unless (&check_name($app{"first_name"}) eq "1") { # code } # perl -v # This is perl, v5.8.4 built for MSWin32-x86-multi-thread # (with 3 registered patches, see perl -V for more detail)

Replies are listed 'Best First'.
Re^3: Changing the Value Assigned To A Hash Key
by phaylon (Curate) on Feb 08, 2005 at 22:39 UTC
    Try running this on your commandline:
    use warnings; use strict; use Data::Dumper; my @errors; my %app; $app{"first_name"} = param('first_name'); # $app{"first_name"} = "test_value"; if (request_method() eq "POST") { my $first_name_check = &check_name($app{"first_name"}); unless ($first_name_check eq 1) { print "Not through namecheck\n"; $app{"first_name"} = ""; push (@errors, "First Name: ".$first_name_check); } } print Dumper \@errors; print Dumper \%app; sub request_method { return 'POST' } sub param { return 'testname' } sub print_application { # do some stuff with %app and @errors... } sub check_name { my $name = $_[0]; if ($name =~ /^[A-Z][A-Za-z\-']{2,40}/s) { return 1; } else { return "Helpful message."; }
    I got
    Not through namecheck $VAR1 = [ 'First Name: Helpful message.' ]; $VAR1 = { 'first_name' => '' };
    -- 
    Thru the darkness of futures past, the magician longs to see.
    One chants out, between two worlds - fire, walk with me.

      Works for me on the command line. Also works using most recent changes copied out of the script and into your test:

      unless (&check_name($app{"first_name"})) { undef $app{"first_name"}; push (@errors, "Helpful message..."); }

      It still doesn't work in CGI. I added use Data::Dumper and to the actual script and "Dumper" to the end of the &print_application function:

      sub print_application { header, start_html, table({-border=>'0' -class=>'boxed'}, Tr( [ td([b('First Name:'), "$app{'first_name'}"]) ] ), p("Value is", $app{'first_name'}), pre( Dumper \@errors, Dumper \%app ), end_html; }

      Oddly enough, this is what comes out the other side:

      A form element with the value "nameCheckFailsIfNumbersPresent12345" in + a table, and... Value is # <- undef here has it should be. But not above? $VAR1 = [ 'Names must be between two and forty total characters, start + with an initial capitol, and may only contact alphabetical character +s, a dash, or an apostrophe.' ]; $VAR2 = '$VAR1 = { \'first_name\' => undef }; ';

      That looks like it works, except this line from the same print_application function: print textfield(-name=>'first_name', -size=>'40', -maxlength=>'40', -default=>"$app{'first_name'}");

      returns... <input type="text" name="first_name" value="nameCheckFailsIfNumbersPresent12345" size="40" maxlength="40">

      Why is the value "nameCheckFailsIfNumbersPresent12345" present in $app{'first_name'} but not in Dumper \%app or p("Value is", $app{'first_name'}) - all of which are called from inside the function?

        print textfield(-name=>'first_name', -size=>'40', -maxlength=>'40', -d +efault=>"$app{'first_name'}");
        Whoo, why are you putting $app.. into quotes? Try
        print textfield( -name => 'first_name', -size => 40, -maxlength => 40, -default => $app{'first_name'}, );
        Good Luck!

      Did this for fun and the value shows in the textfield but not as the plain variable. Also tried removing the double quotes surrounding $app{'first_name'} in textfield().

      Tr( [ td( [ b('First Name:'), textfield(-name=>'first_name', -size=>'40', -maxlength +=>'40', -default=>"$app{'first_name'}") ] ), ] ), Tr( [ td( [ b('First Name:'), $app{'first_name'} ] ), ] );

      Those users sure better appreciate not having to re-type form data... :-)