# 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;
# 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+$/} @_;
$term->setcflag($cflag); $term->setlflag($lflag);
$term->setoflag($oflag); $term->setiflag($iflag);
$term->setattr(fileno($self), TCSANOW);
+ $$self->{TERM} = $term;
+
return $self;
}
{
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;