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

I'm having problems passing the correct value of the textfield 'name' to a subroutine that i wrote... The value is being stored correctly from the looks of it to $user_name, but for some reason the value doesn't seem to work when passed into the routine. I've tried passing values directly into my routine to see if it is the routine, and those passed fine. Any ideas what is wrong? Here is the code below:
# HTML CODE use CGI qw/:standard/; print header, start_html('Punch Records'), start_form, "What is your employee number? ",textfield('name'),p, submit, end_form, hr,"\n"; if (param) { $user_name = em(param('name')); &file_creation($user_name); } print end_html; # Code to create output files sub file_creation { # Variables # $quit_loop = 0; $found_name = 0; $user_name=$_[0]; $loop = 0; # Changing directories chdir "c:/Inetpub/wwwsbmf/scripts"; # Converting username to correct format for comparison search $user_name = substr($user_name, 1, 4); if ( substr($username, 0, 1) eq "0" ) { $user_name = substr($user_name, 1, 3); } # Cleaning up the Kronos document open(ORIGFILE,"current") || die "cannot open current for reading"; open(REVFILE,">revfile") || die "cannot open revfile for writing"; while (<ORIGFILE>) # read each line of ORIGFILE { if (/South/) # If a South Bend Line { <ORIGFILE>; <ORIGFILE>; <ORIGFILE>; } elsif (/-+/) { print REVFILE $_; <ORIGFILE>; <ORIGFILE>; } else { print REVFILE $_; } } close(ORIGFILE); close(REVFILE); # Searching for the correct username to return data to another file open(REVFILE, "revfile") || die "cannot open revfile for reading"; open(FINALFILE, ">finalfile.txt") || die "cannot open finalfile for +writing"; while (<REVFILE>) { # Converting Kronos username for comparison $current_line = $_; $current_id = substr($current_line, 33, 4); if ( substr($current_id, 0, 1) eq "0" ) { $current_id = substr($current_id, 1, 3); } if ( substr($current_id, 3, 1) eq " ") { $current_id = substr($current_id, 0, 3); } unless ($quit_loop == 1 ) { if ($found_name == 1) { if (/-+/) { $quit_loop = 1; } else { print FINALFILE $_; } } elsif ($current_id eq $user_name) { $found_name = 1; print FINALFILE $_; } else { $found_name = 0; } } } $quit_loop = 0; close(REVFILE); close(FINALFILE); }

Replies are listed 'Best First'.
Re: Argument values
by TheoPetersen (Priest) on Jul 23, 2001 at 23:03 UTC
    You don't give much to go on; what do you mean by "the value doesn't work"?

    My first guess though would be that the problem is here in the main routine:

    $user_name = em(param('name')); &file_creation($user_name);
    file_creation then converts $user_name as so:
    $user_name = substr($user_name, 1, 4);
    The em function prints an <EM> tag, so $user_name ends up containing "EM>" plus the first character of the parameter value.
      Not only that, but the variable in the expression directly above it is misspelled (note the lack of an underscore):
      ... if ( substr($username, 0, 1) eq "0" ) ...
      Please, use strict from now on!

      ar0n ]

        It's fixed guys, i wasnt sure what the em() was doing... thanks... sometimes you just dont see those little things that others see immediately and vice versa.
Re: Argument values (boo)
by boo_radley (Parson) on Jul 24, 2001 at 00:32 UTC
    first, the obligatory :
    # perl -wT # ^ whatever executable's appropriate! use strict;

    Also, this chunk probably doesn't do what you want :

    if (param) { $user_name = em(param('name')); &file_creation($user_name); }
    It checks to see if any parameter exists, then assumes that the name param will. that's bad.
    Assuming a user name of "Jim", the next line assigns <em>Jim</em> to $user_name, and that's probably not what you want.
      I've gone to use warnings; so don't worry about the -w flag anymore.
Re: Argument values
by abstracts (Hermit) on Jul 23, 2001 at 23:24 UTC
    Hello,

    You have this code:

     if (param) 
      {
        $user_name = em(param('name'));
        &file_creation($user_name);
      }
    
    If your CGI script is passed "John" for the "name" field, you'd be calling file_creation("<EM>John</EM>"); which doesn't look like what you wanted to do.

    Remove the "em" and see what happens

    Hope this helps,,,

    Aziz,,,