Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using Exporter is fine for smaller projects, but if you have lots of code handling data that is internal to a module, things can get ... messy. Object-orientation is a lot friendlier for this type of thing.</p> <p>Why not construct a method to fetch this data? In fact, why not just use Moose?</p> <p>In your Example.pm, just load Moose - this gives you a constructor and destructor for free, as well as a subroutine to fetch values <em>and</em> turns on strict, etc by default. Array references have to be declared a little differently, due to how Class:MOP (The engine under the antlers of Moose) initializes attributes - you have to wrap it in a code reference (aka sub {}). You would also use Data::Dumper in the script which calls the package, instead of the package itself.</p> <p>Example.pm</p> <pre><code>package Example; use Moose; has 'exported_array' =&gt; (is =&gt; 'rw', default =&gt; sub { [ 'one', 'two', 'three' ] }); 1; </code></pre> <p>Then call this from a script:</p> <p>example.pl</p> <pre><code>#!/usr/bin/env perl use Modern::Perl '2013'; use lib '.'; use Example; use Data::Dumper; my $example = Example-&gt;new; my $imported_array_ref = $example-&gt;exported_array; my @imported_array = @{$imported_array_ref}; foreach my $element(@imported_array) { say $element; } say Dumper(\@imported_array); </code></pre> <p>I made the dereferencing really explicit in the example.pl script above... it can be much more terse by dereferencing it directly into the array:</p> <pre><code>#!/usr/bin/env perl use Modern::Perl '2013'; use lib '.'; use Example; use Data::Dumper; my $example = Example-&gt;new; my @imported_array = @{$example-&gt;exported_array}; foreach my $element(@imported_array) { say $element; } say Dumper(\@imported_array); </code></pre> <p>I think a <em>lot</em> more Perl programmers would embrace Moose if there were more simple examples that show how to get simple things done. </p> <p><a href="https://metacpan.org/pod/Moose%3a%3aManual/" rel="nofollow" title="The Official Moose Manual">The Official Moose Manual</a> is excellent, but it was really written for those who are already familiar with OOP.</p>
 

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