Hi all especially Perl teachers if anyone is ... :-)

I just want to know if someone can provide some explanation on how does the
argument iterator sub-routine below work. The Perl script is called from a
UNIX Korn script as below:

mail_smtp.pl -r ${MAILFROM} -s "$subject_line TEST EMAIL"
supportm...@test.com < /tmp/test_email.txt

The Perl script is working and SMTP mail is working. Am just trying to
understand how the getval sub-routine is parsing the command line arguments.
the getval subroutine is as below.

=================================================================

sub getval {
        my $refVal = '';
        foreach $var(@ARGV) {
                if ($refVal ne '') {
                        $$refVal = $var;
                        $refVal = '';
                }
                else {
                        $_ = $var;
                        if (/-r/ ) {
                                $refVal=\$fromUser;
                        }
                        elsif (/-f/) {
                                $refVal=\$dataFile;
                        }
                        elsif (/-s/) {
                                $refVal=\$subject;
                        }
                        else {
                                @toUser = split(/[\;,]/,$var);
                        }

                }
        }
}

=================================================================

The portion that am confused at is at the following lines:

$$refVal = $var;

and

$_ = $var;
if (/-r/ ) {
        $refVal=\$fromUser;
}

Does "if (/-r/ )" means "ignore" all command line that begins with a hyphen
but reference by value the next command line argument after them?

Does $_ contains the following values on each iteration?

mail_smtp.pl
-r
${MAILFROM}
-s
"$subject_line TEST EMAIL"
supportm...@test.com
<
/tmp/test_email.txt

Any "explanation" on this will be very much appreciated. Am going nuts
trying to understand how the iteration functions although am glad it is
functioning.

Thanks in advance.


===============================
mail_smtp.pl source code below:
===============================

#!/usr/bin/perl -w
use Net::SMTP;
use FileHandle;


# global variables
my $fromUser = $ENV{USER};
my @toUser = {};
my $smtpSvr = '192.168.3.11';
my $subject = '';
my $dataFile = '';
#$mailBody = '';
sub getval {
        my $refVal = '';
        foreach $var(@ARGV) {
                if ($refVal ne '') {
                        $$refVal = $var;
                        $refVal = '';
                }
                else {
                        $_ = $var;
                        if (/-r/ ) {
                                $refVal=\$fromUser;
                        }
                        elsif (/-f/) {
                                $refVal=\$dataFile;
                        }
                        elsif (/-s/) {
                                $refVal=\$subject;
                        }
                        else {
                                @toUser = split(/[\;,]/,$var);
                        }

                }
        }
}

# main
getval(@ARGV);
if (@toUser ne {}) {

        if ($dataFile eq '') {
                $dataFile = '-';
        }
        open(my $inFile, "< $dataFile");
        $smtp = Net::SMTP->new($smtpSvr);
        $smtp->mail($fromUser);
        $smtp->to(@toUser);
        $smtp->data();
        $smtp->datasend("From:$fromUser\n");
        $smtp->datasend("To:".join(';',@toUser)."\n");
        if ($subject ne '') {
                $smtp->datasend("Subject:$subject\n");
        }
        while (<$inFile>) {
                if (/^\.$/) {
                        last;
                }
                else {
                    $smtp->datasend($_);
                }

        }
        close($inFile);
        $smtp->dataend();
        $smtp->quit;
}

Reply via email to