Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, the Perl is easy, but I am rusty in the other languages, so I will update with them in a bit. Here is a plain Perl class:</p> <pre><code>#!/usr/bin/perl package Person; use strict; use warnings; use Carp; sub new { my $class = shift; my $self = { @_ }; croak "bad arguments" unless defined $self-&gt;{firstname} and defined $self-&gt;{lastname}; return bless $self, $class; #this is what makes a reference into an object } sub name { my $self = shift; return "$self-&gt;{firstname} $self-&gt;{lastname}"; } #and here is some code that uses it package main; my $person = Person-&gt;new(firstname =&gt; "Chas.", lastname =&gt; "Owens"); print $person-&gt;name, "\n"; </code></pre> <p>Here is the same class written using the new Moose style objects:</p> <pre><code>#!/usr/bin/perl package Person; use Moose; #does use strict; use warnings; for you has firstname =&gt; ( is =&gt; 'rw', isa =&gt; 'Str', required =&gt; 1 ); has lastname =&gt; ( is =&gt; 'rw', isa =&gt; 'Str', required =&gt; 1 ); sub name { my $self = shift; return $self-&gt;firstname . " " . $self-&gt;lastname; } #and here is some code that uses it package main; my $person = Person-&gt;new(firstname =&gt; "Chas.", lastname =&gt; "Owens"); print $person-&gt;name, "\n"; </code></pre> <p>And MooseX::Declare removes the need for even more code and makes things look nice:</p> <pre><code>#!/usr/bin/perl use MooseX::Declare; class Person { has firstname =&gt; ( is =&gt; 'rw', isa =&gt; 'Str', required =&gt; 1 ); has lastname =&gt; ( is =&gt; 'rw', isa =&gt; 'Str', required =&gt; 1 ); method name { return $self-&gt;firstname . " " . $self-&gt;lastname; } } #and here is some code that uses it package main; my $person = Person-&gt;new(firstname =&gt; "Chas.", lastname =&gt; "Owens"); print $person-&gt;name, "\n"; </code></pre> <p>A quick note, These two class are the first two Moose classes I have ever written. And here is some very rusty C++ code (don't cut yourself on it or you will need a tetanus shot):</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; class Person { char* firstname; char* lastname; public: Person(char* first, char* last) { firstname = first; lastname = last; } char* name(void) { int len = strlen(firstname) + strlen(lastname) + 1; char* name = new char[len]; name[0] = '\0'; strcat(name, firstname); strcat(name, " "); strcat(name, lastname); return name; } }; int main(void) { Person* p = new Person("Chas.", "Owens"); char* name = p-&gt;name(); printf("%s\n", name); delete name; delete p; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload