despite the many shortcuts perl offers the lazy programmer via the $_ variable by using it as a default argument in many functions, I realized that I could wrest even more optimization from perl by glomming this function onto common math operations!
For instance, if you're writing a quickie script to do some tallying, you might have line after line of $_+=10;
$_+=$n;
and so on. No more with the ARGHelper.
turn $_+=20 into +=20 and $_++ into the delightful ++! that's a savings of half your keystrokes right there! Handles ++, --, and all op= operators!
In case you're still having difficulty, here's a sample script :
#! perl -l use ARGHelper; $_=10; +=2; **=2; print;
package ARGHelper; use Filter::Simple; FILTER_ONLY code => sub { my $nl = qr/(\n?;\n?)|&&|\|\|/sm; s/($nl)\s*?--\s*?($nl)/$1\$_--$2/smg; s/($nl)\s*?\+\+\s*?($nl)/$1\$_++$2/smg; print; my @ops = ('**=', '+=', '*=', '&=', '<<=', '&&=', '-=', '/=', '|=', '>>=', '||=', '.=', '%=', '^=', 'x='); foreach my $op (@ops) { my $qo = quotemeta $op; s/($nl)\s*?$qo/$1\$_$op/smg; } } ; return 1;
In reply to helping along $_ by boo_radley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |