{"id":218,"date":"2015-11-24T20:44:07","date_gmt":"2015-11-24T20:44:07","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=218"},"modified":"2015-11-24T20:44:07","modified_gmt":"2015-11-24T20:44:07","slug":"stdclass-demolish-exceptions","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/","title":{"rendered":"Std::Class DEMOLISH doesn&#8217;t forward exceptions"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p><a href=\"http:\/\/search.cpan.org\/~chorny\/Class-Std-0.013\/lib\/Class\/Std.pm\" target=\"_blank\">Std::Class<\/a> is one of the CPAN modules for implementing object classes in Perl. According to the book <a href=\"http:\/\/shop.oreilly.com\/product\/9780596001735.do\" target=\"_blank\">Perl Best Practices<\/a> by <a href=\"http:\/\/damian.conway.org\/About_us\/Bio_formal.html\" target=\"_blank\">Damian Conway<\/a> it is the preferred method for implementing objects in Perl.<\/p>\n<p>The module creates a DEMOLISH method in every instantiated object. This method is implicitly called every time before the object gets destroyed, so developers can put their own clean-up code in there for e.g. closing file handles.<\/p>\n<p>The purpose of this post is to warn of a discovered deficiency in regard with using exceptions in DEMOLISH code as well as to provide a workaround for the problem.<\/p>\n<h3>Test case<\/h3>\n<p>Firstly, we&#8217;ll create the class Test and store it in the file Test.pm:<\/p>\n<pre><code>package Test ;\r\n\r\nuse base qw( Exporter ) ;\r\nuse strict ;\r\nuse Carp ;\r\nuse Class::Std ;\r\nuse English ;\r\n\r\n{\r\n  sub exec_proc {\r\n    croak 'My Exception' ;\r\n  }\r\n  sub DEMOLISH {\r\n    my $a = 0 ;\r\n  }\r\n}\r\n1;\r\n<\/code><\/pre>\n<p>Secondly, we&#8217;ll create a Test program, which creates a Test object, then calls the method exec_proc and finally checks for raised exceptions. The sole purpose of the method exec_proc is to croak an exception:<\/p>\n<pre><code>#!\/u00\/oracle\/orabase\/local\/perl\/bin\/perl -w\r\nuse strict;\r\nuse English ;\r\nuse Test ;\r\n\r\nmy $subref =sub  {\r\n  my $p_obj = Test-&gt;new( { } ) ;\r\n  $p_obj-&gt;exec_proc() ;\r\n} ;\r\n\r\neval { $subref-&gt;() ;} ;\r\nif ($EVAL_ERROR) {\r\n  print $EVAL_ERROR ;\r\n} else {\r\n  print \"no exception caught\" ;\r\n}\r\n<\/code><\/pre>\n<p>No surprise there, the exception gets caught in the main procedure:<\/p>\n<pre><code>My Exception at .\/Test.pl line 10.\r\n<\/code><\/pre>\n<p>Now, let&#8217;s enclose the DEMOLISH code in an eval block:<\/p>\n<pre><code>package Test ;\r\n\r\nuse base qw( Exporter ) ;\r\nuse strict ;\r\nuse Carp ;\r\nuse Class::Std ;\r\nuse English ;\r\n\r\n{\r\n  sub exec_proc {\r\n    croak 'My Exception' ;\r\n  }\r\n  sub DEMOLISH {\r\n    <span style=\"color: #ff0000;\">eval {<\/span>\r\n      my $a = 0 ;\r\n    <span style=\"color: #ff0000;\">};<\/span>\r\n  }\r\n}\r\n1;\r\n<\/code><\/pre>\n<pre><code>no exception caught\r\n<\/code><\/pre>\n<p>Strangely, we didn&#8217;t getting any exceptions this time. I reported a <a href=\"https:\/\/rt.cpan.org\/Public\/Bug\/Display.html?id=95834\" target=\"_blank\">bug<\/a> for this behavior.<\/p>\n<h3>Workaround<\/h3>\n<p>Apparently, the value of $EVAL_ERROR had been changed within the DEMOLISH method without being restored before returning the call. Localizing $EVAL_ERROR resolves the problem:<\/p>\n<pre><code>package Test ;\r\n\r\nuse base qw( Exporter ) ;\r\n\r\nuse strict ;\r\nuse Carp ;\r\nuse Class::Std ;\r\nuse English ;\r\n\r\n{\r\n  sub exec_proc {\r\n    croak 'My Exception' ;\r\n  }\r\n  sub DEMOLISH {\r\n    <span style=\"color: #ff0000;\">local $EVAL_ERROR ;<\/span>\r\n    eval {\r\n      my $a = 0 ;\r\n    };\r\n  }\r\n}\r\n1;\r\n<\/code><\/pre>\n<pre><code>My Exception at .\/Test.pl line 10.\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>std::class demolish doesn&#8217;t forward exceptions <a href=\"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-218","post","type-post","status-publish","format-standard","hentry","category-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Std::Class DEMOLISH doesn&#039;t forward exceptions<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Std::Class DEMOLISH doesn&#039;t forward exceptions\" \/>\n<meta property=\"og:description\" content=\"std::class demolish doesn&#039;t forward exceptions Continue Reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-24T20:44:07+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Std::Class DEMOLISH doesn&#8217;t forward exceptions\",\"datePublished\":\"2015-11-24T20:44:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/\"},\"wordCount\":229,\"commentCount\":0,\"articleSection\":[\"Perl\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/\",\"name\":\"Std::Class DEMOLISH doesn't forward exceptions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-11-24T20:44:07+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stdclass-demolish-exceptions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Std::Class DEMOLISH doesn&#8217;t forward exceptions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Std::Class DEMOLISH doesn't forward exceptions","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/","og_locale":"en_US","og_type":"article","og_title":"Std::Class DEMOLISH doesn't forward exceptions","og_description":"std::class demolish doesn't forward exceptions Continue Reading &rarr;","og_url":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/","og_site_name":"All-round Database Topics","article_published_time":"2015-11-24T20:44:07+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Std::Class DEMOLISH doesn&#8217;t forward exceptions","datePublished":"2015-11-24T20:44:07+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/"},"wordCount":229,"commentCount":0,"articleSection":["Perl"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/","url":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/","name":"Std::Class DEMOLISH doesn't forward exceptions","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2015-11-24T20:44:07+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/stdclass-demolish-exceptions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Std::Class DEMOLISH doesn&#8217;t forward exceptions"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":2,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/218\/revisions\/277"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}