Note that Perl does ship with a (still) core module called Class::Struct, which offers a simple syntax for OO programming.
An example I wrote 2 years ago is somewhat similar to the Point class example you provided. I only wrote it as a rebuttal to a claim that Perl OO programming is very hard without installing something from CPAN
package Planet;
use Class::Struct mass => '$', radius => '$';
use Math::Trig;
sub density {
my $self = shift;
my $vol = 4/3*pi*($self->radius)**3);
return $vol/($self->mass);
}
package main;
use Planet;
my $Jupiter = Planet->new(mass => 1.8E27, radius => 6.9E7);
print "Jupiter has mass of " . $Jupiter->mass . " and ";
print "radius of " . $Jupiter->radius . "\n";
print "It's density is " . $Jupiter->density . "\n";
Not as clean as your proposed syntax, but is something that can be used without needing anything from CPAN. And there are limitations, such as Class::Struct classes can only inherit from UNIVERSAL.
Edit: corrected syntax as pointed out by LanX. Also, changed mod:// links to doc://
-
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.
|