<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
					xmlns:content="http://purl.org/rss/1.0/modules/content/"
					xmlns:wfw="http://wellformedweb.org/CommentAPI/"
					xmlns:atom="http://www.w3.org/2005/Atom"
				  >
<channel>
<atom:link rel="self"  type="application/rss+xml"  href="http://rulinux.net/rss_from_sect_2_subsect_6_thread_34858"  />
<title>rulinux.net - Статьи - Development - Локализация $@ в DESTROY</title>
<link>http://rulinux.net/</link>
<description><![CDATA[Портал о GNU/Linux и не только]]></description>
<image><title>rulinux.net - Статьи - Development - Локализация $@ в DESTROY</title>
<link>http://rulinux.net/</link>
<url>http://rulinux.net/rss_icon.png</url>
</image>
<item>
<title>Локализация $@ в DESTROY</title>
<link>https://rulinux.net/message.php?newsid=34858&amp;page=1#109460</link>
<guid>https://rulinux.net/message.php?newsid=34858&amp;page=1#109460</guid>
<pubDate>Wed, 01 Jun 2011 18:31:00 +0400</pubDate>
<description><![CDATA[<p>Недавно обнаружил, что локализовывать переменную $@ в DESTROY методе бывает очень полезно. Даже, похоже, сабж можно поместить в мануал как best practice (или я невнимательно читал мануалы, и там такое уже есть?). Без лишних слов:</p><p><fieldset><legend>perl</legend><code><br />
<br />
<span style="color: #666666; font-style: italic;">#!perl -w</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
&nbsp;<br />
<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Animal<span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <a href="http://perldoc.perl.org/functions/bless.html"><span style="color: #000066;">bless</span></a> <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #339933;">=&gt;</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">,</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <a href="http://perldoc.perl.org/functions/ref.html"><span style="color: #000066;">ref</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">sub</span> moo<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$self-&gt;{'a'} says: moo<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">sub</span> DESTROY<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">moo</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> main<span style="color: #339933;">;</span><br />
&nbsp;<br />
<a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$a</span> <span style="color: #339933;">=</span> Animal<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Cow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bark</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$@</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Cow cannot bark<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Cow can bark<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp;</code></fieldset></p><p>Программа выведет: Cow says: moo Cow can bark</p><p>Ну, наверное, вам уже стало понятно почему корова может гавкать?! Тут всё довольно просто и прозрачно. А вот диагностировать подобную проблему в сложной системе с кучей используемых модулей и фреймворков довольно сложно. На это можно убить даже не несколько часов, а несколько дней. Так что, рекомендую локализовывать $@ в DESTROY методе ваших классов.</p><p>По этой же причине значению, которое вернул eval, вообще нельзя доверять (ты же не контролируешь код всех модулей, которые используешь), и стоит использовать Try::Tiny или что-то подобное.</p><p>(с) этот текст - найденная на просторах интернета копипаста</p>]]></description>
</item>
</channel>
</rss>