Thanks for the replies, I figured it out now, I think.
Saying
my My $my
indeed tells the compiler that $my should be a My object. This information is not used for type-checking, however (neither at run-time nor at compile time). It is currently only used to make use of the fields
pragma, which lets you define your class' fields:
# compile error
$my->{no_such_field} ++;
In addition to checking the spelling of field names, you
get the improved performance of having the hash access turned
into an array/pseudo-hash access (On the other hand,
pseudo-hashes seem to be evil, the docs warn about their imminent removal from Perl).
use strict;
{
package My;
use fields qw[ one ];
sub new {
return fields::new(shift);
}
}
my My $my = new My;
$my->{one}++;
b-deparses to
package My;
sub BEGIN {
use strict 'refs';
require fields;
do {
'fields'->import('one')
};
}
sub new {
use strict 'refs';
return fields::new(shift @_);
}
package main;
use strict 'refs';
{;};
my $my = 'My'->new;
++$$my[1]; ## <--- array access
-
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.
|