Possible change to Device::Firmata->open()
Closed this issue · 2 comments
I noticed the open() subroutine dies if the connection to the Firmata Server fails. This causes an issue for me because it kills the script that calls the open() subroutine as well. Would it not be better to return(undef) instead of dieing?
I have modified my local copy of Firmata.pm to test this, and it provides the expected functionality. I am able to test the Firmata Server connection with the initial call to open() and handle the error in the calling script instead of having Device::Firmata kill the perl process.
Here is an example:
Original code:
my $device = $package->open($serial_port,$opts) or die "Could not connect to Firmata Server";
New code:
my $device = $package->open($serial_port,$opts) or return(undef);
Error handling is controlled by the calling script, not the Device::Firmata module. This approach will allow the calling script to provide its own error handling (display an error message, try again, etc...) instead of killing the perl process.
Sample code using the return(undef).
my $serial = Device::Firmata->open( 'unavailable_serial_port' ) || die "Could not connect to Firmata Server\n";
Just wanted to see if anyone has any objections to this approach before I commit my changes.
using die is the proper way to throw exceptions in object-oriented perl (see http://www.perl.com/pub/2002/11/14/exception.html). One uses eval to catch the error-condition:
eval {
Device::Firmata->open(...);
};
print $@ if $@;
That makes sense, I have never encountered that behavior with a perl module before though.