Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can re-<a href="http://perldoc.perl.org/functions/bless.html" rel="nofollow">bless</a> the returned object to whatever your heart desires:</p> <pre><code>#!/usr/bin/perl package Some::Data; use strict; use warnings; sub new { my $class = shift; bless { @_ } =&gt; $class } sub a { $_[0]-&gt;{a} } package My::Data; use strict; use warnings; use base 'Some::Data'; sub a_squared { my $self = shift; my $v = $self-&gt;a; return $v * $v; } package Some::Parser; use strict; use warnings; sub new { my $class = shift; bless { @_ } =&gt; $class } sub parse { return Some::Data-&gt;new(a =&gt; 3) } package main; use strict; use warnings; my $data = Some::Parser-&gt;new-&gt;parse; bless $data =&gt; 'My::Data'; printf "%.1f\t%.1f\n", $data-&gt;a, $data-&gt;a_squared; </code></pre> <p>Alternatively, you can use @cjm's idea:</p> <pre><code>#!/usr/bin/perl package Some::Data; use strict; use warnings; sub new { my $class = shift; bless { @_ } =&gt; $class } sub a { $_[0]-&gt;{a} } package My::Data; use strict; use warnings; use base 'Some::Data'; sub a_squared { my $self = shift; my $v = $self-&gt;a; return $v * $v; } package Some::Parser; use strict; use warnings; sub new { my $class = shift; bless { @_ } =&gt; $class } sub parse { my $self = shift; return $self-&gt;data_class-&gt;new(a =&gt; 3); } sub data_class { $_[0]-&gt;{data_class} } package main; use strict; use warnings; my $data = Some::Parser-&gt;new(data_class =&gt; 'My::Data')-&gt;parse; printf "%.1f\t%.1f\n", $data-&gt;a, $data-&gt;a_squared; </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