Why use the "do {} if" block in Perl?

While watching CPAN, I came across a block of code in this module that puzzled me.

sub import {
  for my $mod (keys %INC) {
    do {
      delete $INC{$mod};
      $mod =~ s/\.pm$//; $mod =~ s/\//::/g;
      delete_package($mod);
    } if $mod =~ m/^SOAP/;
  }
}

Why did the author use a block do {} ifinstead of a regular ifblock?

+5
source share
5 answers

One difference is that it do { ... }returns a value, while the if statement does not (although see the comments below).

eg:.

my $x = 3;
my $z = do { warn "in the do block"; 10 } if $x == 3;

You can do almost the same with the ternary operator, although you cannot order statements inside the branches of the ternary operator.

+5
source

. . Perl . .

+4

, if , if.

+2

if, , . A do {} - , .

if, , , . .

+2

perl " "

+1

All Articles