For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Undefined subroutine &Main::BadData called at line 42









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Undefined subroutine &Main::BadData called at line 42
Ron Smith

2006-09-30, 9:59 pm

Hi all,

I get the error: "Undefined subroutine &Main::BadData
called at line 42" when executing the following '.cgi'
script, with nothing entered in all of the text
fields. I'm unfamiliar with this error.

Could someone give me an explaination behind this
error?

Here's the code:

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header(), start_html("Which Triangle Is It?");

print p("This page tells you what type of triangle
you've entered."), br(),
"A triangle with each side being equal is
equilateral.", br(),
"A triangle with two sides being equal is an isosceles
triangle.", br(),
"A right triangle is one where the square of one side
is equal to", br(),
"the sum of the squares of the other two sides.",
br();
print br();
print '<FORM method="post"
action="pg250ex7.7_whichTriangle.pl">';
print '<TABLE bgcolor="lightblue">';
print Tr( td("Enter length of first side: "),
td('<INPUT type="text" name="side1">') );
print Tr( td("Enter length of second side: "),
td('<INPUT type="text" name="side2">') );
print Tr( td("Enter length of third side: "),
td('<INPUT type="text" name="side3">') );
print Tr( td('<INPUT type="reset" value="Clear">'),
td('<INPUT type="submit" value="Submit">') );
print "</TABLE></FORM>";

my $firstSide = param("side1");
my $secondSide = param("side2");
my $thirdSide = param("side3");

if ($firstSide =~ /^(\d+)$/) {
$firstSide = $1;
if ($secondSide =~ /^(\d+)$/) {
$secondSide = $1;
if ($thirdSide =~ /^(\d+)$/) {
$thirdSide = $1;
if ($firstSide == $secondSide &&
$firstSide == $thirdSide) {
print p("That's an equilateral
triangle!\n");
} elsif ($firstSide == $secondSide ||
$firstSide == $thirdSide || $secondSide == $thirdSide)
{
print p("That's an Isosceles
triangle!\n");
}
}
}
} else {
BadData();
}

sub BadInput {
print p("Please, enter numbers only!");
die "Click the \"Clear\" button and try again.\n";
}

print end_html();

TIA,


Ron Smith
gsatlarge@yahoo.com
Paul Lalli

2006-10-01, 7:57 am

Ron Smith wrote:
> I get the error: "Undefined subroutine &Main::BadData
> called at line 42" when executing the following '.cgi'
> script, with nothing entered in all of the text
> fields. I'm unfamiliar with this error.


> Could someone give me an explaination behind this
> error?


What is difficult to understand about it? Did you *read* the error
message? You called a subroutine named "BadData", but there is no
defined subroutine with that name. Figure out what subroutine you
*meant* to call, and fix the call to it.

Did you *read* the code either? The subroutine call that doesn't exist
is right next to a subroutine definition with a mildly different name.


> } else {
> BadData();
> }
>
> sub BadInput {
> print p("Please, enter numbers only!");
> die "Click the \"Clear\" button and try again.\n";
> }


Please make an *attempt* to solve your own problems before asking
thousands of people around the world to solve them for you.

Paul Lalli

Rob Dixon

2006-10-01, 7:57 am

Ron Smith wrote:
> Hi all,
>
> I get the error: "Undefined subroutine &Main::BadData called at line 42" when
> executing the following '.cgi' script, with nothing entered in all of the
> text fields. I'm unfamiliar with this error.
>
> Could someone give me an explaination behind this error?
>
> Here's the code:
>
> use strict;
> use warnings;
> use CGI qw(:standard);
> use CGI::Carp qw(fatalsToBrowser);
>
> print header(), start_html("Which Triangle Is It?");
>
> print p("This page tells you what type of triangle you've entered."), br(),
> "A triangle with each side being equal is equilateral.", br(),
> "A triangle with two sides being equal is an isosceles triangle.", br(),
> "A right triangle is one where the square of one side is equal to", br(),
> "the sum of the squares of the other two sides.",
> br();
> print br();
> print '<FORM method="post"
> action="pg250ex7.7_whichTriangle.pl">';
> print '<TABLE bgcolor="lightblue">';
> print Tr( td("Enter length of first side: "),
> td('<INPUT type="text" name="side1">') );
> print Tr( td("Enter length of second side: "),
> td('<INPUT type="text" name="side2">') );
> print Tr( td("Enter length of third side: "),
> td('<INPUT type="text" name="side3">') );
> print Tr( td('<INPUT type="reset" value="Clear">'),
> td('<INPUT type="submit" value="Submit">') );
> print "</TABLE></FORM>";
>
> my $firstSide = param("side1");
> my $secondSide = param("side2");
> my $thirdSide = param("side3");
>
> if ($firstSide =~ /^(\d+)$/) {
> $firstSide = $1;
> if ($secondSide =~ /^(\d+)$/) {
> $secondSide = $1;
> if ($thirdSide =~ /^(\d+)$/) {
> $thirdSide = $1;
> if ($firstSide == $secondSide && $firstSide == $thirdSide) {
> print p("That's an equilateral triangle!\n");
> } elsif ($firstSide == $secondSide || $firstSide == $thirdSide || $secondSide == $thirdSide) {
> print p("That's an Isosceles triangle!\n");
> }
> }
> }
> } else {
> BadData();
> }
>
> sub BadInput {
> print p("Please, enter numbers only!");
> die "Click the \"Clear\" button and try again.\n";
> }
>
> print end_html();
>
> TIA,



Hi Ron.

It should say "Undefined subroutine &main::BadData ..." (with a lower case 'm')
as the current package is 'main' unless you specify another one. It's saying
that there's no BadData() subroutine defined in your code. And indeed there
isn't, because you've called it BadInput()!

HTH,

Rob
Ron Smith

2006-10-01, 6:58 pm

--- Aaron Priven <aaron@priven.com> wrote:

>
> On Sep 30, 2006, at 7:30 PM, Ron Smith wrote:
>
> &Main::BadData
> '.cgi'
>
> [...]
>
> again.\n";
>
> You are trying to call the subroutine "BadData", but
> Perl can't find
> a subroutine with that name. Instead, your "sub"
> uses a different
> name, "BadInput". Use the same name both places and
> the error should
> go away.
>
> --
> Aaron Priven, aaron@priven.com,
> http://www.priven.com/aaron
>
>

Doh! Here's a case where I forgot I made a name
change. Another Lesson learned. I suppose I would have
caught this too, if I'd used the debugger. I guess I
was in a hurry at the time.

Thanks, everybody. :)


Ron Smith
gsatlarge@yahoo.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com