简体   繁体   中英

How do I check for an empty scalar in 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);
..
.                        
}

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;

Have you tried this?

 if (!($list eq ""))
     send_email(...);

or

 if ($list ne "")
     send_email(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM