Question

* SECOND UPDATE FOR SUNDAY*

Now at this point I have found some added success at displaying the desired sub The new snippet of code which enabled me to actually invoke a specific sub as I wanted.

In researching I stumbled upon the following snippet which deals with reading incoming FORM data. This snippet does enable the invocation of the sub of my choice from this script. However from the CLI when I run perl -x against the script the system returns the following nonfatal *warnings* that I would like to gain understanding of and resolve. My research shows that (tr///) and $ENV{"REQUEST_METHOD"} and $buffer are returning empty values "" OR 0. How would I best resolve these following errors? I realize I can just delete any reference to (tr///) and $buffer to resolve those errors, however, I question removing *$ENV{"REQUEST_METHOD"}* as it seems this imperative to the function of this snippet???

CLI ERROR

Use of uninitialized value in transliteration (tr///) at test.pl line 36 (#1)
Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at test.pl line 37 (#1)
Use of uninitialized value $buffer in split at test.pl line 44 (#1)
#!/usr/bin/perl -w
# (test.pl)

use DBI;
use DBD::mysql;
use warnings;
use strict;
use diagnostics;

$| = 1;

# The script I am wanting to create, is to allow users at (NAS) HotSpot to create a user account
# which is to write into MySQL db TABLE's *radcheck* and *radreply*.
#
# Now at this point I have found some added success at displaying the desired *sub*
# The new snippet of code which enabled me to actually *invoke* a specific *sub* as I wanted
# from an HTML form.
# Please see below for solution which still has some questions.

  print "Content-type: text/html\n\n";

  sub BuildAcctNow {
      print "<h1 style=\"color:blue;font-family:Arial;font-size:xx-large;\">TO BUILD YOUR ACCOUNT TODAY WE WILL NEED A SMALL AMOUNT OF INFORMATION</h1><br><br>\n\n";
  }

  sub PauseAcctNow {
      print "<h2 style=\"color:red;font-family:Arial;font-size:xx-large;\">YOUR ACCOUNT HAS BEEN PAUSED PLEASE MAKE A PAYMENT HERE.</h2><br><br>\n\n";
  }

#  In researching I stumbled upon the fllowing snippet which deals with reading inward FORM data.
#  This snippet *does* enable the *invocation* of the *sub* of my choice from this script.
#  However from the CLI when I run perl -x against the script the system returns the following
#  *nonfatal* *warnings* that I would like to gain understading of and resolve.
#  My research shows that  (tr///) and  $ENV{"REQUEST_METHOD"} and $buffer are returning empty
#  values, How would I best resolve these following errors? I realize I can just delete any
#  reference to (tr///) and $buffer to resolve those errors, howerver I question removing
#  $ENV{"REQUEST_METHOD"} as it seems this imperative to the function of th ssnippet???
#
#
#  Use of uninitialized value in transliteration (tr///) at test.pl line 36 (#1)
#  Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at test.pl line 37 (#1)
#  Use of uninitialized value $buffer in split at test.pl line 44 (#1)

    my ($buffer, @pairs, $pair, $name, $value, %FORM);

    # Read in text
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    }else {
        $buffer = $ENV{'QUERY_STRING'};
    }
    # Split information into name/value pairs
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%(..)/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
    }

  if ($FORM{PauseAcct}) {
  PauseAcctNow();
  exit;
  }

 elsif ($FORM{BuildAcct}) {
  BuildAcctNow();
  exit;
  }

END SECOND SUNDAY UPDATE

SUNDAY UPDATE ** I have made a simple script to hopefully exhibit what I am trying to do to this point hopefully? The script I am ultimately needing to create will write to MySQL db into radcheck and radreply to enable users to log onto a (NAS) HotSpot. So I will have more than one subroutine within the script.

The script presently displays an empty screen VIA browser when I use a HTML doc with a properly named SUBMIT form within the doc named BuildAcct.

I am traditionally used to defining the sub within the script and then I would define if test(s) within the script which would wait for match(es) from any defined form name(s) when they are interacted with to then call a particular sub.

Below is a test script I have created just trying to get past the antiquated use of & when calling a sub, this is causing me some grief and I am hoping for some valuable input.

#!/usr/bin/perl -w
# (test.pl)

use DBI;
use DBD::mysql;
use warnings;
#use strict;
# Presently due to my errors below I have disabled *use strict*.

$| = 1;

# The script I am wanting to create, is to allow users at (NAS) HotSpot to create a user account
# which is to write into MySQL db TABLE's *radcheck* and *radreply*.
#
# Trying to bring myself up to speed with a very basic task which I have defined below, in my
# older scripts I would define the *sub* itself, then in the script I would use an *if* test
# which checks to see if any defined FORM value returns a hit such as $form_data{'BuildAcct'} ne ""
# to call the required *sub* _ThisOne_.

  print "Content-type: text/plain\n\n";

  sub ThisOne {
      print "Trying to display this subroutine upon submission of BuildAcct\n";
  }

#  Following is the *if* test I am accustomed to using to make a call to a particular sub when
#  the form NAME BuildAcct is interacted with, but this is unacceptable now I realize.
#  CLI Return:
#  Use of uninitialized value $form_data{"BuildAcct"} in string ne at test.pl line 32.
#  Use of uninitialized value $form_data{"BuildAcct"} in string ne at test.pl line 41.

  if ($form_data{'BuildAcct'} ne "")
  {
  &ThisOne;
  exit;
  }

# SO, I have Google'd, and looked over numerous methods of calling *subs*, I am just stuck though,
# Why can't the following *if* test work if use of & is no longer used?

  if ($form_data{'BuildAcct'} ne "")
  {
  ThisOne();
  exit;
  }

Thanking you in advance for help... Best Regards

UPDATE **
I have turned the -w switch off on the script, not sure if that poses a negative influence, pretty new to perl.

I also created some clunky code that is ugly. The weird thing is that from the CLI when I execute the script the system returns:

Use of uninitialized value $form_data{"BuildAcct"} in string at acctmanager.pl line 211.
Use of uninitialized value $form_data{"Test"} in string at acctmanager.pl line 212. 

Yet VIA browser from a HTML doc I can change the SUBMIT name value back and forth between BuildAcct and Test and the script successfully returns two different and correct subroutines when submitted.

The BuildAcct sub returns form fields I defined within that subroutine, whereas Test does a MySQL TABLE GROUP rowfetch and displays 3 different tables from a db and prints them to the browser.

Below is my present code :-(

local ($form_data{'BuildAcct'}) = "$form_data{'BuildAcct'}";
local ($form_data{'Test'}) = "$form_data{'Test'}";
                #
                # AddNewUser FORM definition.
if ($form_data{'BuildAcct'} ne "")
   {
   &AddNewUser;
   exit;
  }
                 #
                 # DispTest FORM definition.
elsif ($form_data{'Test'} ne "")
   {
   &DispTest;
   exit;
  }

Could someone give me a nudge into the right direction possibly?

Thanking you in advance

          ORIGINAL POST

At this point I have a FORM on an HTML doc named BuildAcct, likewise within my script I have defined the following which is to make the call to the subroutine AddNewUser when the user submits the HTML FORM...

 if ($form_data{'BuildAcct'} ne "")
   {
   &AddNewUser;
   exit;
  }

The script uses cgi-lib.pl

# Enable parsing of FORM_DATA VIA cgi-lib.pl.
 &ReadParse(*form_data);

## FORM or IMG FORM Fix
foreach (keys %form_data)
 {

## Fix incoming form data with Image buttons
 $form_data{$1} = $form_data{$_} if (/(.*)\.x/);
 }

The thing I can't understand is why does this work in another script I use but this new script returns the following upon execution at the CLI;

Use of uninitialized value $form_data{"BuildAcct"} in string ne at acctmanager.pl line 208.
Use of uninitialized value $form_data{"Test"} in string ne at acctmanager.pl line 215.

Help and suggestions are greatly appreciated. Best Regards

Was it helpful?

Solution

My best guess is that %form_data hash is being populated via cgi-lib.pl, therefore when you run it via command line, cgi-lib.pl isn't getting any input from the web browser... having said this, you haven't included the code where you're using cgi-lib.pl, so I can't be sure.

p.s. don't turn off warnings. They're there for a reason. If nothing else, you can paste the warning into google. While you're at it, alway put use strict; at the top of your script, and fix all undeclared variables.

OTHER TIPS

To fix the error "Use of uninitialized value $form_data{"BuildAcct"} in string",

You can check with defined($form_data{"BuildAcct"}) or defined $form_data{"BuildAcct"} like below, before making use of $form_data{"BuildAcct"} :

if (defined $form_data{"BuildAcct"}) { < other code based on $form_data{"BuildAcct"} > }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top