Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

helping along $_

by boo_radley (Parson)
on Sep 08, 2002 at 18:43 UTC ( #196068=CUFP: print w/replies, xml ) Need Help??

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;
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;

Replies are listed 'Best First'.
Re: helping along $_
by belg4mit (Prior) on Sep 08, 2002 at 20:33 UTC
    Hmm, interesting. Not sure how useful that really is, but here are some ideas to extend it.
    1. useless use of constant in void context becomes assignment. So...
      10; #is equivalent to $_=10;
    2. Add support for some of the perl6 operators such as //=
    3. UPDATE: An example script using this, which is an interactive calculator.
      > 22/7; < 3.14285714285714 > *=9**2; < 254.571428571429

    perl -pew "s/\b;([mnst])/'$1/g"

      Not sure I like this idea. Modules tend to end with 1; and 0 and 1 occasionally appear in void context in legit uses like 1 while s/foo/bar/; I would rather see the simple = usable this way as in =10; Though now that I think about it, this may be confusing to distinguish from POD directives.

      Makeshifts last the longest.

        changing 1; to $_=1; in a module is not going to do anything significant, unless $_ is not scoped to the module and something else using the module expects $_ to be undefined initially. Your other example is not a useless use of constant in void context, I'm not sure it's even in void context, but I'll take your word for that. It's not useless because it's part of a larger, more meaningful statement, if the remainder of the line consisted of only numeric operations, numbers, (variables) and semi-colon, that would be useless use... and safe to muck with.

        perl -pew "s/\b;([mnst])/'$1/g"

Re: helping along $_
by Cybercosis (Monk) on Sep 09, 2002 at 05:33 UTC
    ++! this is magnificent. ought to annoy the hell out of a friend of mine. i think it's appropriate that the ARGHelper begins with ARGH.


Re: helping along $_
by amrangaye (Friar) on Dec 17, 2002 at 13:41 UTC
    I really don't think this is very useful. Sure, perl programs are supposed to be short, but at the same time they're also suppossed to be elegant and readable. Try using this module for a longer program and you'll probably get lost editing the program 2 months later. In short, it leads to code that's not very maintanable. cheers :)
      package amrangaye; no humor; return 1;
        OK, OK! Point taken :) but don't blame me when you have to stay up all night trying to work out just where that error is in your code. ;) Oh, btw happy birthday perl.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://196068]
Approved by Albannach
Front-paged by PodMaster
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-07-11 20:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found