vrushali has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to access the serial port through perl script, for that i am using the SerialPort.pm and ComPort.pm module. In serialPort.pm module there is a subroutine called "new". I would like to understand the exact working of the new subroutine. Some portion of the new subroutine is as follows
sub new { my $proto = shift; my $class = ref($proto) || $proto; my $device = shift; my $alias = $device; if ($device =~ /^COM\d+$/io) { $device = '\\\\.\\' . $device; # required COM10 and beyond, done for all to facilitate testing } my @new_cmd = ($device); my $quiet = shift; if ($quiet) { push @new_cmd, 1; } my $self = $class->SUPER::new(@new_cmd); unless ($self) { return 0 if ($quiet); return; }

Replies are listed 'Best First'.
Re: Working of new subroutine
by Corion (Patriarch) on Jan 04, 2011 at 09:18 UTC

    What parts do you have problems with?

    Also, the modules are not ComPort.pm and SerialPort.pm but likely Win32::SerialPort.

Re: Working of new subroutine
by Utilitarian (Vicar) on Jan 04, 2011 at 10:32 UTC
    The code presented simply parses the two arguments it is called with and instantiates an object of it's SUPER class with these arguments modified for the SUPER.

    In essence it changes the device name so that com2 becomes \\\\.\\com2 and accepts any second argument as the quiet flag.
    EDIT: The second argument must be true (ie. non-zero )

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."