You searched for articles tagged with Perl.

[ #129 ] Perl - Case Insensitivity on Parts of Regex Permalink

Perl Added a year and a bit ago

We know that to make a regex wholly case insensitive, you can add the i modifier like so:

/coca cola/i

... but to make only part of it case insensitive (say, the 'cola' part - it could be COLA or Cola or cola etc), try this:

/coca (?i:cola)/



[ #127 ] Hacking YourWorldOfText Permalink

Perl, Cool Added a year and a bit ago and last edited about a fortnight ago

Edit: This is quite old now, and doesn't work like it used to when YWOT first started. Perhaps I'll fix it someday, perhaps not.

YourWorldOfText is a novel site that is like a big, collaborative whiteboard. It uses JQuery AJAX to send what you type to the server. I have reverse engineered (ha ha, that's the type of pretentious wanker I am, I use the term 'reverse engineered') the co-ordinate system and created a Perl script that let's you do some tricks with the site.

#!/usr/bin/env perl

use strict;
use warnings;

use constant MAX_X => 15;
use constant MAX_Y => 7;

my @thing = (0);

my $bigx_start = $thing[int rand scalar @thing];
my $bigy_start = $thing[int rand scalar @thing];
my $x_start = 0;
my $y_start = 0;

my $bigx = $bigx_start;
my $bigy = $bigy_start;
my ($x, $y) = ($x_start, $y_start);

my $starttime = (time() - 20) . '000';
my $output;
$output .= "\narr = [";

my $count = 0;
while (my $line = <>) {
    chomp($line);
    my @chars = split '', $line;
    $bigx = $bigx_start;
    for my $char (@chars) {
        $char = q(\\") if $char eq '"';
        $char = q(\\\\) if $char eq '\\';
        my $newtime = $starttime + ($count * 17);
        $output .= qq|[$bigy, $bigx, $y, $x, $newtime, "$char"], |;
        $x++;
        if ($x > MAX_X) {
            $x = 0;
            $bigx++;
        }
        $count++;
    }
    $x = $x_start;
    $y++;
    if ($y > MAX_Y) {
        $y = $y_start;
        $bigy++;
    }
}

$output .= "];\n";
$output .= "\n";
my ($prevbleep, $curbleep) = (0, 200);
while ($prevbleep < $count) {
    $output .= "setTimeout(function() { jQuery.post(window.location.pathname, {edits: arr.slice($prevbleep, $curbleep)}, YourWorld.editsDone, 'json') }, 10*$prevbleep);\n";
    $prevbleep = $curbleep;
    $curbleep += 200;
}
print $output;


chmod +x this script, run it and paste in some ASCII art (or figlet output, or whatever.) It will then print the javsascript code you need to insert the ASCII art on the site. You will need firefox and firebug - paste the JS into the firebug console and hit run... then wait to see your art come up on the site.

Pro Tip: Start with a small image, or only a word or two with figlet. Larger stuff takes longer.




[ #124 ] Booleans and Boolean Expressions... Permalink

Perl, Python, Ruby Added a year and a bit ago

It's funny how the little, basic things that you re-examine when you are trying to teach someone else a programming language can surprise you. I thought I knew how these different languages handled this topic but I was wrong. Here we go:

I want to show you some differences between Perl, Python and Ruby (the three of which I consider to be the main "scripting" languages at the moment) with respect to their handling of booleans and boolean expressions. I was taken by surprise with some of this behaviour, as I tend to mainly use booleans as the results of conditional expressions - and fairly explicitly when I do.

Perl

No boolean type.

$ perl -e 'if (100 > 1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (100) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (0) { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if (-1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if ("") { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if ("hello") { print "true\n" } else { print "false\n" }'
true

Python

Has a specific boolean type.

>>> x = False

$ python -c 'print ("true" if 100 > 1 else "false")'
true
$ python -c 'print ("true" if 100 else "false")'
true
$ python -c 'print ("true" if 1 else "false")'
true
$ python -c 'print ("true" if 0 else "false")'
false
$ python -c 'print ("true" if -1 else "false")'
true
$ python -c 'print ("true" if "" else "false")'
false
$ python -c 'print ("true" if "hello" else "false")'
true

... ok so it's the same as Perl at this point, but note these following boolean comparisons and compare to the upcoming Ruby code:

>>> 0 == False
True
>>> 1 == True
True
>>> 100 == True
False
>>> -1 == True
False

Ruby

$ ruby -e 'if (100 > 1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (100) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (0) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (-1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = ""; if (a) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = "hello"; if (a) then puts "true"; else puts "false"; end'
true

... ok ... that's fairly different. That's because Ruby only counts something as false if it is specifically equal to 'false' or 'nil'. Interesting. What about boolean comparisons that aren't wrapped in conditional expressions?

(Python uses 'True' and 'False', Ruby uses 'true' and 'false')

irb(main):001:0> 0 == false
=> false
irb(main):002:0> 1 == true
=> false
irb(main):003:0> 100 == true
=> false
irb(main):004:0> -1 == true
=> false

That is considerably different to Python, right?

What do we know?

OK so this post wasn't exactly straightforward, but it shows how major languages can treat something as simple as booleans so differently.




[ #119 ] Find Domains Permalink

Perl Added a year and a bit ago and last edited a year and a bit ago

Want to find cool, word-like domains that end with a certain tld that are unregistered?

Try this to search for .st:

# adjust this ----v to the tld you want
perl -ne '/^(.*)(st)$/ && { (`whois $1.$2` =~ /No entries/) && print }' /usr/share/dict/words

Which should work on Mac OSX (i.e. wherever your whois fails with 'No entries' present in the output. Change accordingly.)




[ #112 ] AnyEvent: Event Based Perl-Programming Permalink

Perl Added a year and a bit ago

You could use the ol' POE, or this new(ish) bad boy: AnyEvent

AnyEvent is different - it is a thin abstraction layer above all kinds of event loops. Its main purpose is to move the choice of the underlying framework (the event loop) from the module author to the program author using the module.

AnyEvent looks good and I'm going to have a play with it soon!




[ #111 ] Extract Ranges Permalink

Perl Added a year and a bit ago and last edited a year and a bit ago

I'm sure there is a module to do this already, but as as a Perl exercise this morning I wrote the below. It extracts specified arithmetic sequences from arrays of numbers:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

sub getr {
    my $inc = shift;
    my @list = @_;
    my @thislist;
    push(@thislist, shift @list);
    while (my $i = shift @list) {
        if ($i == $thislist[-1] + $inc) {
            push(@thislist, $i);
        }
        else {
            return ( \@thislist, getr($inc, $i, @list) );
        }
    }
    return \@thislist;
}

my @a = qw(1 2 3 4 7 8 9 11 12 16 18 20 21 22 25);
my @b = qw(2 4 6 8 10 11 13 15 17 19 20 21 22 24);
my @c = qw(10 7 4 1 -1 -9 -10 -11 -12 -15 -18);

my @lists = getr(1, @a);
for my $list (@lists) {
    print "Got: @$list\n";
}

print "\n---\n";

@lists = getr(2, @b);
for my $list (@lists) {
    print "Got: @$list\n";
}

print "\n---\n";

@lists = getr(-3, @c);
for my $list (@lists) {
    print "Got: @$list\n";
}

The output:

Got: 1 2 3 4
Got: 7 8 9
Got: 11 12
Got: 16
Got: 18
Got: 20 21 22
Got: 25

---
Got: 2 4 6 8 10
Got: 11 13 15 17 19
Got: 20
Got: 21
Got: 22 24

---
Got: 10 7 4 1
Got: -1
Got: -9
Got: -10
Got: -11
Got: -12 -15 -18

I'll clean it up and put it in a package soon - I think I will start keeping a personal code snippet/exercise git repo - it might be good to look back on as a journal!




[ #108 ] Perly Vim Permalink

Vim, Perl Added a year and a bit ago and last edited a year and a bit ago

Vim regexes aren't great, especially if you are used to Perl ones. When you are looking for a Perly way to substitute some text in Vim, try the simple:

:perldo s/something \d+/another/

This is a good way to make sure your ranges, special chars, character classes and flags etc work the way you have come to expect from using Perl.

By default this operation works on the entire file, but you can prefix the command with line ranges like you would with a normal Vim substitution.




[ #103 ] In the search of MD5(x)=x Permalink

Perl, Mathematics, Agile Added a year and a bit ago and last edited a year and a bit ago

A couple of websites have recently been setup to find x.

Elliot Kembler tries to name x the Kembler Identity which might be a little arrogant, but the idea is explained best on his site.

The MD5 Game site keeps a highscore of who is winning the search using the longest common substring. Cool idea - but of course if you are leading this scoreboard it doesn't mean you are any closer to finding x than anyone else.

So here is some Perl to help you find an input to MD5 that gives you an output with a certain (configurable) substring length:

#!/usr/bin/perl

use strict;
use warnings;

use Digest::MD5 qw(md5_hex);
use String::LCSS_XS qw(lcss);

my $start = q(dd91217c39ac3b9e54846de064d40c62);

my $last = $start;

while (1) {
    my $cur = md5_hex($last);
    my $long = lcss($last, $cur);
    if (length($long) > 5) {
        print "md5('$last') == '$cur'\n";
        print "$long\n";
        if ($cur eq $last) {
            print "^^^^ I found it! ZOMG!!!!\n";
            last;
        }
    }
    $last = $cur;
}

See the 5 in there? Change that to a higher number when you start getting serious about finding really long, common substrings. As an example, here is the output of the above, changing the loop from while (1) to for (1..150000)

md5(dd91217c39ac3b9e54846de064d40c62) => ac3b9e54846de4358d0d460572f128fb
ac3b9e54846de
md5(df08ba8cb16042270ed7f0f4385f0d60) => ad06535b0cd9265d3727dcb1604fa4f4
cb1604
md5(6489123acf277017783b9cc5882121d0) => d052d008f277015cc2fc01f8e41e0574
f27701
md5(29d91a801c36d15f6d393fadce1ed2ce) => 3fadce5dfaf11cfbd4bd0a19838dfa31
3fadce
md5(90253bc725066b48977fc5fd287ac873) => c45ef485c3cd87ac8781698b9b6cbb3b
87ac87
md5(13ab98420d2d390220661bc27cdd01d5) => faccf5f92d098420d41c5b7bcac9f9d3
98420d

Why use dd91217c39ac3b9e54846de064d40c62 as the starting hash? Because that's the one that is currently leading the MD5 Game website (at the time of this post.) You should change it.

You will note that my Perl just uses the MD5 hash of the previous attempt as its new try. This is a novel idea. You could (should?) use a randomly generated hash or just increment your way through the whole space if you like... of course this will take up a fairly large portion of time ;-)




[ #95 ] MySQL Tuner Permalink

MySQL, Perl Added a year and a bit ago

I don't like MySQL, I would recommend PostGreSQL over it any day. However we are often forced to use things we don't like (for example at our workplaces!) so here is some info to help tune MySQL for speed:

MySQL Tuner is a Perl script that gives you a list of recommendations on settings to change. It seems easy and sensible! Just run it and it asks for user/pass settings and then gives you a little report.




[ #94 ] Perl Modules to Learn Permalink

Perl Added a year and a bit ago

Here is a list of modules I am about to acquaint myself with: based both on Perl-community buzz and interesting, exploratory CPAN sessions I have had recently:




Older Posts ... Newer Posts

Colophon

Django Python 960.gs Git Vim NetBSD Nginx

The Author

This is the blog of Brad Willis, a software engineer living in Brisbane.

Meta

Help
Latest entries

*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Chrome Comedy Cool Crosswords Deployment Django English Exim Firefox FTP Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Postfix PostGreSQL Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh

Recent Entries

PostGreSQL setup cheatsheet
Python property decorators
FTP
gvim - Always open new files as new tabs
crontab - escape % (percentage)
OSX Google Chrome - start in incognito mode
SQLite date arithmetic
Postfix - delete message in mailq
Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix

Links

ChoppingBoard, DaveMisc, Project365, RageQuit