perl - Should Exception::Class objects evaluate to false in boolean context -


i'm trying exception::class first time , surprised me exception::class objects evaluate true when returned function. shouldn't default opposite.

i know can change overload wondering if it's idea

sub gethtml{     return myexception->new( error => 'some error' ); } $response = &gethtml    if($response){     #do html  } else{     #something went wrong check if it's exception object } 

you're confusing exceptions returning false value indicate error.

part of point of exceptions provide own channel indicate error. leaves return free return valid values. there's no need check false vs defined, or special objects, or per-function call error checking @ all. it's caught , dealt @ end of block.

if return exception object defeats point; they're not exceptions, they're error codes.

to take advantage of exceptions, code in example should written this:

sub get_html {     ...try html...      return $html if defined $html;      myexception->throw( error => 'some error' ); }  eval {      $html = get_html;     # $html; } if ( $e = exception::class->caught() ) {     ...deal error... } 

this can made bit prettier try::tiny.

this makes more sense when have lot of things might error, such bunch of file, network or database operations. modules such autodie , path::tiny how works.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -