Oi Breno, baseado no seu email ( não li o artigo do PerlTips :P ) eu estou gostando muuuito do Moose, acho que todos deveriam testar :P é bem fácil e o tutorial do CPAN é simples e direto.<br>Esse artigo é uma boa desculpa para estudar Moose : <a href="http://blog.jrock.us/articles/Myth%3A%20Moose%20is%20an%20unnecessary%20dependency.pod">http://blog.jrock.us/articles/Myth%3A%20Moose%20is%20an%20unnecessary%20dependency.pod</a><br>
<br><div class="gmail_quote">On Tue, Mar 25, 2008 at 1:04 PM, breno &lt;<a href="mailto:breno@rio.pm.org">breno@rio.pm.org</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Abaixo, artigo da PerlTips. Aos programadores de plantão, uma<br>
pergunta: embora o autor comente o uso do tão falado Class::Std (do<br>
Conway) e do Object::InsideOut (do Hedden), ele cita o<br>
Hash::Util::FieldHash como uma ótima alternativa. Pessoalmente nunca<br>
usei o Object::InsideOut, mas o Class::Std parece mais um framework de<br>
OO (como o Moose - embora nunca tenha usado o Moose) do que qualquer<br>
outra coisa. Mesmo sendo do Conway, tenho certo receio em relação a<br>
performance por todas as coisas que rolam por debaixo dos panos, e<br>
pelo fato de não ser core. Já o Hash::Util::FieldHash me pareceu uma<br>
solução muito mais &quot;Perlish&quot;, pq permite a criação de objetos<br>
inside-out de forma simples, preguiçosa e sem preocupações de DESTROY<br>
(contra vazamento) ou CLONE (para multithreading). E agora é core.<br>
<br>
Qual a percepção de vcs?<br>
<br>
[]s<br>
<br>
-b<br>
<br>
<br>
==== Perl 5.10 and Hash::Util::FieldHash ====<br>
<br>
 &nbsp; Perl 5.10 adds the ``Hash::Util::FieldHash&#39;&#39; module to the list of core<br>
 &nbsp; modules. Just like all of the other core modules such as ``File::Find&#39;&#39;<br>
 &nbsp; and ``File::Temp&#39;&#39; this means that ``Hash::Util::FieldHash&#39;&#39; will now be<br>
 &nbsp; installed as standard with Perl for all versions from 5.10.0 onward.<br>
<br>
<br>
== &nbsp; Inside-out objects and Hash::Util::FieldHash &nbsp; ==<br>
<br>
 &nbsp; Inside-out objects provide a method of strong encapsulation and<br>
 &nbsp; compile-time error checking using Perl&#39;s object oriented features. This<br>
 &nbsp; is done by keeping a separate hash for each object attribute, and using<br>
 &nbsp; a unique identifier for the object (commonly its memory address) to<br>
 &nbsp; access these attributes.<br>
<br>
 &nbsp; You can refresh your memory of inside-out objects at our Perl tip at<br>
 &nbsp; &lt;<a href="http://perltraining.com.au/tips/2006-03-31.html" target="_blank">http://perltraining.com.au/tips/2006-03-31.html</a>&gt;.<br>
<br>
 &nbsp; Unfortunately, inside-out objects have two common disadvantages:<br>
<br>
 &nbsp; 1. &nbsp;Care must be taken to clean-up your attribute hashes when your<br>
 &nbsp; &nbsp; &nbsp; object is destroyed (by going out of scope, for example). If you<br>
 &nbsp; &nbsp; &nbsp; don&#39;t do this, you can end up leaking memory.<br>
<br>
 &nbsp; 2. &nbsp;Care must be taken when memory addresses are used as hash keys, as<br>
 &nbsp; &nbsp; &nbsp; these addresses may change (potentially invalidating the object)<br>
 &nbsp; &nbsp; &nbsp; when using a forked or threaded process.<br>
<br>
 &nbsp; When working with inside-out objects, we recommend you use a helper<br>
 &nbsp; module like ``Class::Std&#39;&#39; or ``Object::InsideOut&#39;&#39; which can solve some<br>
 &nbsp; or all of these problems for you. However if these modules are not<br>
 &nbsp; suitable, or if you&#39;re working with existing code that does not use<br>
 &nbsp; them, ``Hash::Util::FieldHash&#39;&#39; provides a convenient and reliable way<br>
 &nbsp; to build inside-out objects.<br>
<br>
 &nbsp; Put very simply, a hash declared as a ``fieldhash&#39;&#39; has the following<br>
 &nbsp; attributes:<br>
<br>
 &nbsp; * &nbsp; A reference can be used directly as a hash key.<br>
<br>
 &nbsp; * &nbsp; This reference will alter itself appropriately if the process forks,<br>
 &nbsp; &nbsp; &nbsp; spawns a new thread, or otherwise causes the reference to relocate<br>
 &nbsp; &nbsp; &nbsp; in memory.<br>
<br>
 &nbsp; * &nbsp; Our hash key is considered a *weak reference*, meaning that if the<br>
 &nbsp; &nbsp; &nbsp; only use of our reference is inside ``fieldhash&#39;&#39; structures, then<br>
 &nbsp; &nbsp; &nbsp; the entire key/value pair will be garbage collected. This provides<br>
 &nbsp; &nbsp; &nbsp; for automatic destruction of any objects that fall out of scope.<br>
<br>
 &nbsp; These properties mean that we can write inside-out objects without the<br>
 &nbsp; headaches of finding appropriate identifiers for our objects,<br>
 &nbsp; forking/threading issues, or garbage collection. It&#39;s also possible to<br>
 &nbsp; enable only *some* of the above features if we want more specific<br>
 &nbsp; behaviour (see &quot;Further reading&quot; below).<br>
<br>
 &nbsp; Imagine a playing card as an object: it would have a suit and a face<br>
 &nbsp; value (rank). We can use ``Hash::Util::FieldHash&#39;&#39; to simplify the<br>
 &nbsp; creation of this as an inside-out object. A minimal class may look like<br>
 &nbsp; this:<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package PlayingCard;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use strict;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use warnings;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use Hash::Util::FieldHash qw(fieldhash);<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldhash my %suit_of;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldhash my %rank_of;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub new {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my ($class, $rank, $suit) = @_;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # This strange looking line produces an<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # anonymous blessed scalar.<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my $this = bless \do{my $anon_scalar}, $class;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $suit_of{ $this } = $suit;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rank_of{ $this } = $rank;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub get_suit {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my ($this) = @_;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $suit_of{ $this };<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub get_rank {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my ($this) = @_;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $rank_of{ $this };<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1;<br>
<br>
 &nbsp; What&#39;s interesting about this example is what&#39;s missing. We don&#39;t need a<br>
 &nbsp; ``DESTROY&#39;&#39; method for memory management, nor a ``CLONE&#39;&#39; method for<br>
 &nbsp; threading. By declaring %suit_of and %rank_of with ``fieldhash&#39;&#39;, this<br>
 &nbsp; work is already done for us.<br>
<br>
 &nbsp; We could use our playing card as follows:<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #!/usr/bin/perl -w<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use strict;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use feature qw(say);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use PlayingCard;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my $card = PlayingCard-&gt;new(&#39;ace&#39;, &#39;spades&#39;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say &#39;My rank is &#39;, $card-&gt;get_rank;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say &#39;My suit is &#39;, $suit-&gt;get_suit;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my $card2 = PlayingCard-&gt;new(&#39;king&#39;, &#39;diamonds&#39;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say &#39;My new card rank is a &#39;, $card-&gt;get_rank;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; $card2 will automatically be destroyed and its memory garbage collected<br>
 &nbsp; at the end of its block.<br>
<br>
<br>
== &nbsp; Further reading &nbsp; ==<br>
<br>
 &nbsp; To find out what other modules have been included in the core read the<br>
 &nbsp; Perl delta for 5.10.0 at &lt;<a href="http://search.cpan.org/perldoc?perl5100delta" target="_blank">http://search.cpan.org/perldoc?perl5100delta</a>&gt;.<br>
 &nbsp; To learn more about ``Hash::Util::FieldHash&#39;&#39; read<br>
 &nbsp; &lt;<a href="http://search.cpan.org/perldoc?Hash::Util::FieldHash" target="_blank">http://search.cpan.org/perldoc?Hash::Util::FieldHash</a>&gt;.<br>
<br>
 &nbsp; More information about ``Class::Std&#39;&#39; can be found at<br>
 &nbsp; &lt;<a href="http://search.cpan.org/perldoc?Class::Std" target="_blank">http://search.cpan.org/perldoc?Class::Std</a>&gt;, and more information about<br>
 &nbsp; ``Object::InsideOut&#39;&#39; can be found at<br>
 &nbsp; &lt;<a href="http://search.cpan.org/perldoc?Class::InsideOut" target="_blank">http://search.cpan.org/perldoc?Class::InsideOut</a>&gt;.<br>
_______________________________________________<br>
Rio-pm mailing list<br>
<a href="mailto:Rio-pm@pm.org">Rio-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/rio-pm" target="_blank">http://mail.pm.org/mailman/listinfo/rio-pm</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Lindolfo &quot;Lorn&quot; Rodrigues<br>- <a href="http://www.slackwarezine.com.br">www.slackwarezine.com.br</a><br>- <a href="http://lornlab.org">http://lornlab.org</a><br>
- <a href="http://sao-paulo.pm.org">http://sao-paulo.pm.org</a><br>use Catalyst;