<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><BR><DIV><DIV>On 2007/03/13, at 22:39, Thomas Britis wrote:</DIV><BR class="Apple-interchange-newline"><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Luis Motta Campos wrote:</DIV> <BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">   </SPAN>Claro que não.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">   </SPAN>Num caso, você tem retornos numéricos (e eu presumo que assuma e <SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">teste retornos numéricos em algum lugar do seu código, certo?)</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">   </SPAN>No outro caso, você tem retorno objetivo - o efeito colateral mais <SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">importante disto é que te permite encadear chamadas a outros métodos, <SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">assim:</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">   </SPAN>$obj-&gt;method()-&gt;method2()-&gt;method3();</DIV> </BLOCKQUOTE><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre">        </SPAN>Minha dúvida é: como posso fazer esse encadeamento? Pensei em algo como:</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV></BLOCKQUOTE><BR></DIV><DIV>Acredito que o que o Luis quis dizer aqui foi algo do tipo:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>&lt;code&gt;</DIV><DIV>package A;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>sub new {</DIV><DIV>   return bless {}, shift; # nasty way :-)</DIV><DIV>}</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>sub function1 {</DIV><DIV>    my $self = shift;</DIV><DIV>    ...</DIV><DIV>    return $self;</DIV><DIV>}</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>sub function2 {</DIV><DIV>    my $self = shift;</DIV><DIV>    ...</DIV><DIV>    return $self;</DIV><DIV>}</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>package main;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>my $a = A-&gt;new;</DIV><DIV>$a-&gt;function1()-&gt;function2();</DIV><DIV>&lt;/code&gt;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Você pode se perguntar: Mas como eu detecto um erro desta forma, se eu sempre retorno a referência para o meu objeto?</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Simples:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>&lt;code&gt;</DIV><DIV># implementação do código da function1:</DIV><DIV>sub function1 {</DIV><DIV>    my $self = shift;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>    die "Hey, 'key1' does not exists!" unless exists $self-&gt;{'key1'};</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>    # faz algo com $self-&gt;{'key1'}</DIV><DIV>    ...</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>    return $self;</DIV><DIV>}</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>package main;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>my $a = A-&gt;new;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>eval {</DIV><DIV>    $a-&gt;function1()-&gt;function2();</DIV><DIV>};</DIV><DIV>if ($@) {</DIV><DIV>    warn "Hey, this error ocurred: $@\n";</DIV><DIV>}</DIV><DIV>&lt;/code&gt;</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Existem outras abordagens para esse tipo de modelagem, como o Error, Exception::Class ou o Error::TryCatch. Este último utiliza source filtering, o que muitos desaconselham por modificar o código fonte e poder introduzir erros que se tornam difíceis de perceber. Acredito que outra pessoa na lista possa discutir mais sobre isto, pois realmente não tenho conhecimento necessário para tal.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Espero que isto ajude :-)</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Lucida Grande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><DIV>--</DIV><DIV>Igor Sutton</DIV><DIV><A href="mailto:igor.sutton@gmail.com">igor.sutton@gmail.com</A></DIV><DIV><BR class="khtml-block-placeholder"></DIV><BR class="Apple-interchange-newline"></SPAN> </DIV><BR></BODY></HTML>