Random thoughts from scripting (perl)

Here’s a couple of nifty tricks from today’s scripting adventures:

1) This one is sort of obvious in retrospect – but, if you’re processing a long list of items and want to show a status indicator, this works rather well:


for($i=0;$i<10000;$i++) { print "$i\r" if(!($i % 100)); }

I’ve always used the !($i % 100) trick, but never the \r, which returns the carriage to the beginning of the line.

Also fun is:


$|=1;

@spinner = (“|”,”/”,”-“,”\\”);
for($i=0;$i<10000;$i++) { print $spinner[($i % 4)] . "\r"; select(undef,undef,undef,0.1); }

2) moving a byte: handy constants to memorize are 65280, 16711680, and 4278190080, which are the second eight bits, the third eight bits, and the forth eight bits, respectively. You can do ($value & 65280) >> 8, ($value & 16711680) >> 16, and ($value & 4278190080) >> 24, respectively, to get at bytes two, three, and four. The reverse operation is even easier: $b1 + ($b2 << 8) + ($b3 << 16) + ($b4 << 24);

3 Responses to “Random thoughts from scripting (perl)”

  1. sheer_panic Says:

    Other fun spinners:

    @spinner = (“.”,”o”,”O”,”o”);
    @spinner = (“<","^",">“, “V”);

  2. sheer_panic Says:

    Clearly I’ve got too much free time on my hands:
    #!/usr/bin/perl

    $|=1;

    @spinner = (“|”,”/”,”-“,”\\”);
    #@spinner = (“.”,”o”,”O”,”o”);
    #@spinner = (“<","^",">“, “V”);
    #@spinner = (“@”,”*”,”o”,”O”);
    @spinner = split(//,”this is a test this is only a test “);

    for($i=0;$i<10000;$i++) {
    for($j=0;$j<80;$j++) {
    print $spinner[(($i+$j) % @spinner)];
    }

    print "\r";

    select(undef,undef,undef,0.1);
    }

  3. ClintJCL Says:

    those spinners were particularly fun at, say, 300bps or 1200bps

Leave a Reply