How do I check for an empty scalar in perl? If I have no $list
, I do not want to send an email.
Can I check for empty message in the send_email routine or do this outside?
I have a query that uses Win32::OLE.
my $servSet = $wmiObj->ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName LIKE 'ServiceNameHere%'", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
I'm looping through it here and building a list $list
foreach my $serv (in $servSet) {
next if $serv->{started};
my $sname = $serv->{name};
my $sstate = $serv->{started};
my $ssmode = $serv->{startmode};
$list .= "Service: $sname - $sstate - $ssmode\n";
}
I use the $list to send as body of the email:
sub send_email {
...
..
$smtp->datasend($list);
..
.
}
In Perl, undef
, ""
(and also 0
and "0"
) evaluate to "false". So you can just do a boolean test:
send_email() if $list;
I don't like to fool around with what's actually in the variable. If I want to see if anything, anything at all, is in a scalar, I check its length:
send_mail() if length $scalar;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With