First attempt at a module fails

G

Gary Schenk

Exercise 6.8 of "Perl How to Program" asks the student to create
a simple module to use in making a web page. I've failed
miserably at this, and don't know why. The subroutines work when
they are in the source file, but when I call them from a
package, they return an error.

I've gone to learn.perl.org for examples of return statements and
mine seem to be OK. I'm using fully qualified names properly, I
believe. The llama book doesn't go into this deeply, and the
camel book makes my head hurt.

Hopefully someone will spot my error right away. I do appreciate
any help. Here is the source file:

#################################
#!/usr/bin/perl

use warnings;
use strict;
use diagnostics;
use MINE;

print "Create a simple webpage using my HTML package.\n";

my $start = MINE::start_document();

my $para = MINE::create_paragraph();

my $ending = MINE::end_document();

open( WEB, ">web.html" ) or die("Can't open a new file: $!" );

print( WEB "$start $para $ending");
close WEB;

#####################################
Here is the module MINE.pm:

#####################################
#!/usr/bin/perl

package HTML;

sub start_document
{
print "Enter the title of your webpage: ";
chomp( my $title = <STDIN> );
my $start_statement =
"<html><head><title>$title</title></head><body>\n";
return $start_statement;
}

sub create_paragraph
{
print "Enter a string for a paragraph: ";
chomp( my $string = <STDIN> );
my $para_statement = "<p>$string</p>";
return $para_statement;
}


sub end_document
{
my $end_statement = "</body></html>\n";
return $end_statment;
}

########################################
Here is the error:

-bash-2.05b$ ./web.pl
MINE.pm did not return a true value at ./web.pl line 6.
BEGIN failed--compilation aborted at ./web.pl line 6 (#1)
(F) A required (or used) file must return a true value to
indicate that
it compiled correctly and ran its initialization code
correctly. It's
traditional to end such a file with a "1;", though any true
value would
do. See perlfunc/require.

Uncaught exception from user code:
MINE.pm did not return a true value at ./web.pl line 6.
BEGIN failed--compilation aborted at ./web.pl line 6.
 
J

James Willmore

On Thu, 02 Dec 2004 05:26:02 +0000, Gary Schenk wrote:

#####################################
Here is the module MINE.pm:

#####################################
#!/usr/bin/perl

package HTML;

Change the above line to ....

package MINE;

This construct isn't any different than some other languages (like Java;
the compiler will complain if the class name and the file name are
different).
sub start_document
{
print "Enter the title of your webpage: "; chomp( my $title =
<STDIN> );
my $start_statement =
"<html><head><title>$title</title></head><body>\n";
return $start_statement;
}
}
sub create_paragraph
{
print "Enter a string for a paragraph: "; chomp( my $string =
<STDIN> );
my $para_statement = "<p>$string</p>"; return $para_statement;
}
}
}
sub end_document
{
my $end_statement = "</body></html>\n"; return $end_statment;
}

#you need this!
1;
########################################
Here is the error:

-bash-2.05b$ ./web.pl
MINE.pm did not return a true value at ./web.pl line 6.
BEGIN failed--compilation aborted at ./web.pl line 6 (#1)
(F) A required (or used) file must return a true value to
indicate that
it compiled correctly and ran its initialization code
correctly. It's
traditional to end such a file with a "1;", though any true
value would
do. See perlfunc/require.

Uncaught exception from user code:
MINE.pm did not return a true value at ./web.pl line 6.
BEGIN failed--compilation aborted at ./web.pl line 6.

I'm not going to comment further on this code. The comments I made should
get you started. However, you really should read perlboot and perltoot.

HTH

Jim
 
G

Gary Schenk

James said:
On Thu, 02 Dec 2004 05:26:02 +0000, Gary Schenk wrote:



Change the above line to ....

package MINE;

Yes, I did this. A typo while trying things just to try things.

Still the same error.
This construct isn't any different than some other languages
(like Java; the compiler will complain if the class name and
the file name are different).


#you need this!
1;

This makes no difference. My returns are valid, from the examples
I've seen, so 1; shouldn't be necessary should it?

Thanks, Jim. Every comment helps!
 
M

mccahanry

Gary said:
Yes, I did this. A typo while trying things just to try things.

Still the same error.


This makes no difference. My returns are valid, from the examples
I've seen, so 1; shouldn't be necessary should it?

Thanks, Jim. Every comment helps!
 
S

Sherm Pendley

Gary said:
This makes no difference.

Yes it will.
My returns are valid

The return() functions in your subroutines are irrelevant. They return
values from your subs, when those subs are called. What James' addition
does is return a true value from the *module*.

If a module returns a false value, or none, the use() that loaded it
will think the module failed to load, and exit with the error message
you're seeing:
MINE.pm did not return a true value at ./web.pl line 6.

PS: Did you at least *try* following James' advice before you decided he
was wrong? Why did you omit the rest of the warning - did you even read it?
(F) A required (or used) file must return a true value to indicate that
it compiled correctly and ran its initialization code correctly. It's
traditional to end such a file with a "1;", though any true value would
do. See perlfunc/require.

sherm--
 
M

mccahanry

Gary~~

The "1;" at the end of the file will not affect your subroutines at
all, it just tells Perl that it has successfully loaded the module.
When Perl does an include() or a related call, it wants a "true"
response, so we add 1; usually to the last line of code or thereabouts
to assure that it doesn't fail.

Ryan
 
G

Gary Schenk

Sherm Pendley said:
Yes it will.


The return() functions in your subroutines are irrelevant. They return
values from your subs, when those subs are called. What James' addition
does is return a true value from the *module*.

That was it! The _module_ has to return a true value. I thought only
the subroutines had to return true.
If a module returns a false value, or none, the use() that loaded it
will think the module failed to load, and exit with the error message
you're seeing:


PS: Did you at least *try* following James' advice before you decided he
was wrong? Why did you omit the rest of the warning - did you even read it?

Yes, I tried following James' advice. When I ask for advice, it is in
the interest of learning something, I don't ignore it. I also read the
error message. Before posting here I added the 1; to the end of my
subroutines in the module. That was my mistake.

I've tried many combinations before posting here. Generally, I can
find my mistakes in Perl, but this time I was really stuck. I am an
isolated student, and sometimes I need the help of this group. I
appreciate that time is in short supply nowadays, I don't want to
waste the group's time if I can avoid it.

Thanks very much. I still have a problem with the code, but my module
is working now.

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top