So, I am inspired, and I have been messing around with fields. But I am seeing some what seems to me to be odd behavior. (I am running perl 5.8.0 installed on Mac OS X, so maybe I need to update, if so, let me know.)
package Brain;
use strict;
use warnings;
use fields qw/cells/;
sub new {
my $pkg = shift;
my $self = $pkg->fields::new();
$self->{cells} = shift;
$self;
}
package Dog;
use strict;
use warnings;
use fields qw/intelligence/;
sub new {
my $pkg = shift;
my $self = $pkg->fields::new();
my Brain $brain = Brain->new(shift);
$self->{intelligence} = $brain;
$self;
}
sub getBrain {
my Dog $self = shift;
return $self->{intelligence};
}
package main;
use strict;
use warnings;
my Dog $dog = Dog->new(10);
my Dog $brain = $dog->getBrain();
my Brain $b = $brain;
This compiles and runs with no error or warning. Am I doing something wrong, or am I just assuming too much?
Sorry for this question, but my Programming Perl book does not go into too much detail (at least not that I have come across yet) and the Damian OO book is at the office (and I am not). And looking over the docs for fields it is not clear either.
-
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.
|