X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=DWeather%2Flib%2FDWeather%2FSerial.pm;h=ccf31ca7d124693b48202a1867a215d14082cdb1;hb=HEAD;hp=44da24f7cbe7a2fde0db1f00822deb8d852d22c7;hpb=dc4a7b485f2c395471e3f7b64667da3625dcdc64;p=dweather.git diff --git a/DWeather/lib/DWeather/Serial.pm b/DWeather/lib/DWeather/Serial.pm index 44da24f..ccf31ca 100644 --- a/DWeather/lib/DWeather/Serial.pm +++ b/DWeather/lib/DWeather/Serial.pm @@ -2,15 +2,17 @@ # Module to do serial handling on perl FileHandles # -use strict; - -package Serial; +package DWeather::Serial; use POSIX qw(:termios_h); use Fcntl; +use Scalar::Util qw(weaken); -our @ISA = qw(IO::File); +@ISA = qw(IO::File); +$VERSION = 1.3; + +use strict; # Linux-specific Baud-Rates use constant B57600 => 0010001; @@ -29,7 +31,7 @@ sub new # get my attributes $$self->{ORIGTERM} = POSIX::Termios->new(); - my $term = $$self->{TERM} = POSIX::Termios->new(); + my $term = POSIX::Termios->new(); $$self->{ORIGTERM}->getattr(fileno($self)); $term->getattr(fileno($self)); my ($speed) = grep {/^\d+$/} @_; @@ -63,6 +65,8 @@ sub new $term->setcflag($cflag); $term->setlflag($lflag); $term->setoflag($oflag); $term->setiflag($iflag); $term->setattr(fileno($self), TCSANOW); + $$self->{TERM} = $term; + return $self; } @@ -77,14 +81,22 @@ sub setattr { my $self = shift; my $attr = shift || $$self->{TERM}; - $attr->setattr(fileno($self), &POSIX::TCSANOW); + $attr->setattr(fileno($self), &POSIX::TCSANOW) if fileno($self); } sub close { my $self = shift; - $self->setattr($$self->{ORIGTERM}); + $self->setattr(delete $$self->{ORIGTERM}) if fileno($self) && $$self->{ORIGTERM}; $self->SUPER::close; } +sub DESTROY +{ + my $self = shift; + if (exists $$self->{ORIGTERM}) { + $self->close; + } +} + 1;