Cliff Peng Cliff Peng - 4 months ago 10
Perl Question

What's wrong with this piece of Perl code?

To maintain a Zimbra server, I need to delete one part of a postfix queue.
After searching on Google, I found a piece of Perl code from https://wiki.zimbra.com/index.php?title=Managing-The-Postfix-Queues&redirect=no:

#!/usr/bin/perl

$REGEXP = shift || die “no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!”;

@data = qx;
for (@data) {
if (/^(\w+)(\*|\!)?\s/) {
$queue_id = $1;
}
if($queue_id) {
if (/$REGEXP/i) {
$Q{$queue_id} = 1;
$queue_id = “”;
}
}
}

open(POSTSUPER,”|/opt/zimbra/postfix/sbin/postsuper -d -”) || die “couldn’t open postsuper” ;

foreach (keys %Q) {
print POSTSUPER “$_\n”;
};
close(POSTSUPER);


But when I run it, the system reports:

Unmatched right curly bracket at ./delete_queue.pl line 9, at end of line
(Might be a runaway multi-line ;; string starting on line 5)
syntax error at ./delete_queue.pl line 9, near "}"
syntax error at ./delete_queue.pl line 15, near "}"
Unmatched right curly bracket at ./delete_queue.pl line 17, at end of line
Execution of ./delete_queue.pl aborted due to compilation errors.


Since I am not a Perl programmer but just a non-professional mail server administrator, I do not know what's wrong with this piece of Perl code.

Thanks for your help.

Answer

Here's the original code:

#!/usr/bin/perl

$REGEXP = shift || die "no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!";

@data = qx</usr/sbin/postqueue -p>;
for (@data) {
  if (/^(\w+)(\*|\!)?\s/) {
     $queue_id = $1;
  }
  if($queue_id) {
    if (/$REGEXP/i) {
      $Q{$queue_id} = 1;
      $queue_id = "";
    }
  }
}

#open(POSTSUPER,"|cat") || die "couldn't open postsuper" ;
open(POSTSUPER,"|postsuper -d -") || die "couldn't open postsuper" ;

foreach (keys %Q) {
  print POSTSUPER "$_\n";
};
close(POSTSUPER);

As you can see, the pages you copied from mangled the qx</usr/sbin/postqueue -p> part, causing syntax errors.