I've started using
Moose for implementing abstract classes. Here is a demo, starting with your Foo module:
package Foo;
use Moose::Role;
requires 'table_name';
has 'name' => (is => 'rw');
1;
and then a class that implements the abstract method:
package Abc;
use Moose;
with 'Foo';
sub table_name { 'ABC_TABLE' }
1;
and one that should but doesn't:
package NoTableName;
use Moose;
with 'Foo';
1;
Then a script to test Abc:
use strict;
use warnings;
use Abc;
my $obj = Abc->new(name => 'Steve');
printf "My name is %s\n", $obj->name;
(running it)
C:\Users\arun>perl abc.t
My name is Steve
and one to test NoTableName
use strict;
use warnings;
use NoTableName;
my $obj = NoTableName->new(name => 'Bob');
printf "My name is %s\n", $obj->name;
(just compiling it)
C:\Users\arun>perl -c notablename.t
'Foo' requires the method 'table_name' to be implemented by 'NoTableNa
+me' at C:/Perl/site/lib/Moose/Meta/Role/Application.pm line 59
Moose::Meta::Role::Application::apply('Moose::Meta::Role::Applicat
+ion::ToClass=HASH(0x23ea6a4)', 'Moose::Meta::Role=HASH(0x379d674)', '
+Moose::Meta::Class=HASH(0x368b944)') called at C:/Perl/site/lib/Moose
+/Meta/Role/Application/ToClass.pm line 18
Moose::Meta::Role::Application::ToClass::apply('Moose::Meta::Role:
+:Application::ToClass=HASH(0x23ea6a4)', 'Moose::Meta::Role=HASH(0x379
+d674)', 'Moose::Meta::Class=HASH(0x368b944)') called at C:/Perl/site/
+lib/Moose/Meta/Role.pm line 448
Moose::Meta::Role::apply('Moose::Meta::Role=HASH(0x379d674)', 'Moo
+se::Meta::Class=HASH(0x368b944)') called at C:/Perl/site/lib/Moose/Ut
+il.pm line 94
Moose::Util::apply_all_roles('Moose::Meta::Class=HASH(0x368b944)',
+ 'Foo') called at C:/Perl/site/lib/Moose.pm line 77
Moose::with('NoTableName', 'Foo') called at C:/Perl/site/lib/Moose
+/Exporter.pm line 201
Moose::with('Foo') called at NoTableName.pm line 5
require NoTableName.pm called at notablename.t line 3
main::BEGIN() called at NoTableName.pm line 0
eval {...} called at NoTableName.pm line 0
Compilation failed in require at notablename.t line 3.
BEGIN failed--compilation aborted at notablename.t line 3.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.