How to use OO and package?

D

Davy

Hi all,

I have write a simple program of OO and package.
But Cow-speak; seems not work right? Thanks!

#-----test.pl---
#! /usr/local/bin/perl
use strict;
use warnings;

use Cow ();
Cow->speak;

#-----Cow.pm---
#! /usr/local/bin/perl
use strict;
use warnings;

{
package Cow;
sub sound {"My god"}
sub speak {
my $class = shift;
print"a $class say hello\n", $class->sound,"\n";
}
}
 
A

anno4000

Davy said:
Hi all,

I have write a simple program of OO and package.
But Cow-speak; seems not work right? Thanks!

Cow->speak isn't even called, your script fails at compile time
at "use Cow;".

You only need "use" when the module is in a different file. The
Cow class is defined inside your file, so scratch "use". Cow->speak
works as well as can be expected.

Anno
 
D

Davy

Hi,

I seperate the Cow.pm file and the calling test.pl file. For I want the
pm file not changed by other programmer.
The error is
Cow.pm did not return a true value at test.pl line 5.
BEGIN failed--compilation aborted at test.pl line 5.

Thanks!
Davy
 
A

anno4000

[Please don't top-post. I put your reply in context]
Hi,

I seperate the Cow.pm file and the calling test.pl file. For I want the
pm file not changed by other programmer.
The error is
Cow.pm did not return a true value at test.pl line 5.
BEGIN failed--compilation aborted at test.pl line 5.

If you get a Perl message you don't understand, put "use diagnostics"
near the beginning of your program. It will add explanations to the
messages.

Anno
 
D

Davy

Hi,

Thanks! But why the program need a return value, what's the function
means??

Davy

Michele said:
#-----Cow.pm---
#! /usr/local/bin/perl
use strict;
use warnings;

{
package Cow;
sub sound {"My god"}
sub speak {
my $class = shift;
print"a $class say hello\n", $class->sound,"\n";
}
}

Since you put your package in a separate module, you should return a
true value, i.e. customarily

1;

at the end for the use() statement to succeed.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
M

Marc Espie

Hi,

Thanks! But why the program need a return value, what's the function
means??

Because packages may do stuff besides defining methods, and they need
a way to report success/failure.

That's the API that was chosen, a .pm file must return a true value
if it's successful.
 
J

John Bokma

Michele Dondi said:
Are you asking why it is so? Well, there are good reasons, but I think
you won't be interested in knowing. You may want to know that there's
people who whish there were a way to use the actual return value

perldoc -f do:

"returns undef and sets an error message in $@. If the file is
successfully compiled, "do" returns the value of the last
expression evaluated.
"

The perldoc -f for require shows that do is used, so I would say that you
actually might be able to get the value returned (at least by require).

Testing:


C:\Documents and Settings\John\My Documents>type rtest.pl
use strict;
use warnings;

print require 'RTest.pm';
C:\Documents and Settings\John\My Documents>type RTest.pm
10;

C:\Documents and Settings\John\My Documents>rtest.pl
10
 
D

Davy

Hi Michele,

I am sorry, but what's top-post mean? Thanks!

Davy

Michele said:
Thanks! But why the program need a return value, what's the function
means??

You're welcome! But why do you top-post? This is annoying, you know...

Re your actual question... I can hardly make any sense of it. We're
*not* speaking of a program's return value. And I don't have the
slightest idea of which means of which function you're referring to.

To come back on the topic we were dealing with: you can have multiple
packages in a file. Thus

#!/usr/bin/perl -l

use strict;
use warnings;

{
package Foo;
sub bar { 'baz' }
}

print Foo->bar;

__END__

But if you plan of making Foo a proper module in a separate file, then
you'll need to use() or require() it. If you check

perldoc -f require

you'll notice this paragraph:

The file must return true as the last statement to indicate
successful execution of any initialization code, so it's cus-
tomary to end such a file with "1;" unless you're sure it'll
return true otherwise. But it's better just to put the "1;",
in case you add more statements.

Are you asking why it is so? Well, there are good reasons, but I think
you won't be interested in knowing. You may want to know that there's
people who whish there were a way to use the actual return value, thus
allowing one to put there something more interesting than a
generically true (or false) value.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
D

Davy

Michele said:
Hi Michele,

I am sorry, but what's top-post mean? Thanks!

Davy
[snip]

Exactly this: putting your text on *top* of the quoted content. You'd
better trim the latter and put your remarks after each relevant
portion of it.
[snip]

Hi, Michele,

I understand, thanks!

Davy
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
J

John Bokma

Michele Dondi said:

Just read it really fast, but does the author want something like:


my $factory = use Some::Very::Long::But::Unique::Module::Name;
my $obj = $factory->new;

?

Or something like:

use Some::Very::Long::But::Unique::Module::Name as Foo;

my $obj = Foo->new;


Which I personally would prefer :-D.
 
B

Ben Morrow

[perl.beginners removed as my swerver doesn't accept it]

Quoth John Bokma said:
Just read it really fast, but does the author want something like:


my $factory = use Some::Very::Long::But::Unique::Module::Name;
my $obj = $factory->new;

?

Or something like:

use Some::Very::Long::But::Unique::Module::Name as Foo;

my $obj = Foo->new;


Which I personally would prefer :-D.

The former. The latter can be got with aliased.pm.
 
J

John Bokma

Michele Dondi said:
Precisely. But it's not the whole story. The whole discussion, along
with some of the links provided there, is interesting and far from
trivial.

Yes, I skipped a bit through it, and will read it better later. Thanks.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top