Perl Rocks (even yet)
Submitted by johnk on Sun, 10/28/2007 - 17:19.Perl still rocks. People still say it's hard to read (true), but it's because the language is terse.
sub getAccountIdsOfSitesToSuspend
{
$sql = <<EOQ;
SELECT account_id
FROM account
WHERE
account.account_balance < (SELECT triggerAmount FROM fk_suspension_rules)
AND (
SELECT IF(SUM(transaction_ammount), SUM(transaction_ammount), 0)
FROM transaction
WHERE transaction_date > DATE_SUB( CURDATE(),
INTERVAL (SELECT paymentWindow FROM fk_suspension_rules) DAY)
AND transaction.account_id = account.account_id
) < FLOOR( -account.account_balance * (SELECT releaseFraction FROM fk_suspension_rules))
AND (
SELECT IF(COUNT(*), TRUE, FALSE )
FROM fk_domains
WHERE account_id = account.account_id
AND never_suspend<>'y'
)
EOQ
$sth = $dbh->prepare($sql);
$sth->execute;
my @result = ();
map { push @result, $_->[0]; } @{ $sth->fetchall_arrayref([0]) };
return @result;
}
This function runs an SQL statement, and then returns the result. It could be shorter, yes, but it has a nice way of collapsing a loop into a one-liner: map { push @result, $_->[0]; } @{ $sth->fetchall_arrayref([0]) };
map { ... } @ar -- this applies the code in the brackets to every element in array @ar.
@{ ... } -- dereferences the array reference returned in the brackets into an array.
fetchall_arrayref([0]) -- gets all the results from the query, slicing array element 0 from the results.
The syntax is ugly, but explicit (if you know perl).
