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 <<a href="mailto:breno@rio.pm.org">breno@rio.pm.org</a>> 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 "Perlish", 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>
Perl 5.10 adds the ``Hash::Util::FieldHash'' module to the list of core<br>
modules. Just like all of the other core modules such as ``File::Find''<br>
and ``File::Temp'' this means that ``Hash::Util::FieldHash'' will now be<br>
installed as standard with Perl for all versions from 5.10.0 onward.<br>
<br>
<br>
== Inside-out objects and Hash::Util::FieldHash ==<br>
<br>
Inside-out objects provide a method of strong encapsulation and<br>
compile-time error checking using Perl's object oriented features. This<br>
is done by keeping a separate hash for each object attribute, and using<br>
a unique identifier for the object (commonly its memory address) to<br>
access these attributes.<br>
<br>
You can refresh your memory of inside-out objects at our Perl tip at<br>
<<a href="http://perltraining.com.au/tips/2006-03-31.html" target="_blank">http://perltraining.com.au/tips/2006-03-31.html</a>>.<br>
<br>
Unfortunately, inside-out objects have two common disadvantages:<br>
<br>
1. Care must be taken to clean-up your attribute hashes when your<br>
object is destroyed (by going out of scope, for example). If you<br>
don't do this, you can end up leaking memory.<br>
<br>
2. Care must be taken when memory addresses are used as hash keys, as<br>
these addresses may change (potentially invalidating the object)<br>
when using a forked or threaded process.<br>
<br>
When working with inside-out objects, we recommend you use a helper<br>
module like ``Class::Std'' or ``Object::InsideOut'' which can solve some<br>
or all of these problems for you. However if these modules are not<br>
suitable, or if you're working with existing code that does not use<br>
them, ``Hash::Util::FieldHash'' provides a convenient and reliable way<br>
to build inside-out objects.<br>
<br>
Put very simply, a hash declared as a ``fieldhash'' has the following<br>
attributes:<br>
<br>
* A reference can be used directly as a hash key.<br>
<br>
* This reference will alter itself appropriately if the process forks,<br>
spawns a new thread, or otherwise causes the reference to relocate<br>
in memory.<br>
<br>
* Our hash key is considered a *weak reference*, meaning that if the<br>
only use of our reference is inside ``fieldhash'' structures, then<br>
the entire key/value pair will be garbage collected. This provides<br>
for automatic destruction of any objects that fall out of scope.<br>
<br>
These properties mean that we can write inside-out objects without the<br>
headaches of finding appropriate identifiers for our objects,<br>
forking/threading issues, or garbage collection. It's also possible to<br>
enable only *some* of the above features if we want more specific<br>
behaviour (see "Further reading" below).<br>
<br>
Imagine a playing card as an object: it would have a suit and a face<br>
value (rank). We can use ``Hash::Util::FieldHash'' to simplify the<br>
creation of this as an inside-out object. A minimal class may look like<br>
this:<br>
<br>
package PlayingCard;<br>
use strict;<br>
use warnings;<br>
use Hash::Util::FieldHash qw(fieldhash);<br>
<br>
fieldhash my %suit_of;<br>
fieldhash my %rank_of;<br>
<br>
sub new {<br>
my ($class, $rank, $suit) = @_;<br>
<br>
# This strange looking line produces an<br>
# anonymous blessed scalar.<br>
<br>
my $this = bless \do{my $anon_scalar}, $class;<br>
<br>
$suit_of{ $this } = $suit;<br>
$rank_of{ $this } = $rank;<br>
<br>
return $this;<br>
}<br>
<br>
sub get_suit {<br>
my ($this) = @_;<br>
return $suit_of{ $this };<br>
}<br>
<br>
sub get_rank {<br>
my ($this) = @_;<br>
return $rank_of{ $this };<br>
}<br>
<br>
1;<br>
<br>
What's interesting about this example is what's missing. We don't need a<br>
``DESTROY'' method for memory management, nor a ``CLONE'' method for<br>
threading. By declaring %suit_of and %rank_of with ``fieldhash'', this<br>
work is already done for us.<br>
<br>
We could use our playing card as follows:<br>
<br>
#!/usr/bin/perl -w<br>
use strict;<br>
use feature qw(say);<br>
use PlayingCard;<br>
<br>
my $card = PlayingCard->new('ace', 'spades');<br>
say 'My rank is ', $card->get_rank;<br>
say 'My suit is ', $suit->get_suit;<br>
<br>
{<br>
my $card2 = PlayingCard->new('king', 'diamonds');<br>
say 'My new card rank is a ', $card->get_rank;<br>
}<br>
<br>
$card2 will automatically be destroyed and its memory garbage collected<br>
at the end of its block.<br>
<br>
<br>
== Further reading ==<br>
<br>
To find out what other modules have been included in the core read the<br>
Perl delta for 5.10.0 at <<a href="http://search.cpan.org/perldoc?perl5100delta" target="_blank">http://search.cpan.org/perldoc?perl5100delta</a>>.<br>
To learn more about ``Hash::Util::FieldHash'' read<br>
<<a href="http://search.cpan.org/perldoc?Hash::Util::FieldHash" target="_blank">http://search.cpan.org/perldoc?Hash::Util::FieldHash</a>>.<br>
<br>
More information about ``Class::Std'' can be found at<br>
<<a href="http://search.cpan.org/perldoc?Class::Std" target="_blank">http://search.cpan.org/perldoc?Class::Std</a>>, and more information about<br>
``Object::InsideOut'' can be found at<br>
<<a href="http://search.cpan.org/perldoc?Class::InsideOut" target="_blank">http://search.cpan.org/perldoc?Class::InsideOut</a>>.<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 "Lorn" 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;