Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for an empty scalar in Perl?

Tags:

perl

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);
..
.                        
}
like image 479
jdamae Avatar asked Dec 02 '22 01:12

jdamae


2 Answers

In Perl, undef, "" (and also 0 and "0") evaluate to "false". So you can just do a boolean test:

send_email() if $list;
like image 68
Eugene Yarmash Avatar answered Dec 18 '22 12:12

Eugene Yarmash


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;
like image 27
brian d foy Avatar answered Dec 18 '22 11:12

brian d foy