Turn on debug in below PERL code

How to turn one debug on below function sendSMTP ??
What is the fucntion of var $DEBUG ??


#!/usr/bin/perl -w

use Socket;
use strict;

my($mailTo)     = 'chuikingman@yahoo.com.hk';
my($mailServer) = 'mail.hgcbroadband.com';

my($mailFrom)   = 'medined@mtolive.com';
my($realName)   = "chuikingman";
my($subject)    = 'Test send_mail';
my($body)       = "Test Line One.\
Test Line Two.\
";

$main::SIG{'INT'} = 'closeSocket';

my($proto)      = getprotobyname("tcp")        || 6;
my($port)       = getservbyname("SMTP", "tcp") || 25;
my($serverAddr) = (gethostbyname($mailServer))[4];

die('gethostbyname failed.') unless (defined($serverAddr));

socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
    or die("socket: $!");

 my $packFormat;
$packFormat = 'S n a4 x8';   # Windows 95, SunOs 4.1+
#$packFormat = 'S n c4 x8';   # SunOs 5.4+ (Solaris 2)

connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
    or die("connect: $!");

select(SMTP); $| = 1; select(STDOUT);    # use unbuffered i/o.

{
    my($inpBuf) = '';

    recv(SMTP, $inpBuf, 200, 0);
    recv(SMTP, $inpBuf, 200, 0);
}
my $debug = 1 ;
sendSMTP(1, "HELO\
");
sendSMTP(1, "MAIL From: <$mailFrom>\
");
sendSMTP(1, "RCPT To: <$mailTo>\
");
sendSMTP(1, "DATA\
");

send(SMTP, "From: $realName\
", 0);
send(SMTP, "Subject: $subject\
", 0);
send(SMTP, $body, 0);

sendSMTP(1, "\\r\
.\\r\
");
sendSMTP(1, "QUIT\
");

close(SMTP);

sub closeSocket {     # close smtp socket on error
    close(SMTP);
    die("SMTP socket closed due to SIGINT\
");
}

sub sendSMTP {
    my($debug)  = shift;
    #my($debug)  = 1;
    my($buffer) = @_;

    print STDERR ("> $buffer") if $debug;
    send(SMTP, $buffer, 0);

    recv(SMTP, $buffer, 200, 0);
    print STDERR ("< $buffer") if $debug;

    return( (split(/ /, $buffer))[0] );
}




How to turn one debug on below function sendSMTP ??

perl functions are defined with no parameters. Instead, a function receives an array named @_, which contains any arguments. Now, note the call to shift() here:

sub sendSMTP {
    my($debug)  = shift;  #<------

When shift() is called without an argument, by default it shifts a value from the @_ array. ‘shift’ means: ‘remove the first element of the array’. That means if you call sendSMTP like this:

sendSMTP(1, "hello", "world")

the $debug variable will be set equal to 1 (which evaluates to true in perl).

What is the fucntion of var $DEBUG ??

There is no $DEBUG variable in the code you posted. If you mean $debug, then setting its value to true causes the program to print selected information. For instance, in this code:

my($buffer) = @_;

print STDERR ("> $buffer") if $debug;

$buffer is assigned the array @, which contains the remaining arguments that the function was called with. Because of the syntax that was used, $buffer is assigned the first element of @, which is the second argument the function was called with(remember the first argument was shifted out of the array and assigned to $debug). In this case, the first element remains in the array–it is not shifted out of the array as with $debug.

If the function is called with a first argument of 1, then $debug is assigned the value 1, so this line:

print STDERR ("> $buffer") if $debug;

will print out a string containing the second argument to the function. On the other hand, if the function is called with a first argument of 0, then lines ending with ‘if $debug’ won’t execute.