photo
Jordan Sissel
geek

Wed, 23 Jun 2004

Yet another random number generator

I was bored...

dd if=/dev/urandom bs=1 count=30 2> /dev/null | \
perl -e 'read(STDIN,$foo,5); print unpack("J",$foo) % 6 . "\n";'

Comments: 0 (view comments)
Tags: ,
Permalink: /oneliners/120
posted at: 23:56

Regular expression to match quoted and nonquoted strings

The method I was using to match quoted strings didn't work all that well and it wasn't entirely flexible - so I wrote a lamer version which works for everything I've tried -

# Make the underlined part match characters that can't be in a word.
# For HTML, set this to [^\s>]
my $regex = q!(?:"([^"]*)"|'([^']*)'|([^\s]+))!;

while (<>) {
	while (s/^$regex\s+//) {
		my $string = $1 || $2 || $3;
		print "Quoted string: $string
";
	}
}

It works, I tested it. Here's some sample output:

nightfall(~) > perl quoteregex.pl
testing
Quoted string: testing
foo bar baz
Quoted string: foo
Quoted string: bar
Quoted string: baz
"hello there" how are 'you doing'
Quoted string: hello there
Quoted string: how
Quoted string: are
Quoted string: you doing
'foo
Quoted string: 'foo
'foo bar baz
Quoted string: 'foo
Quoted string: bar
Quoted string: baz

For an example on how to get this to work with html, here's something that'll pull all the links from a webpage (anchor tags, and only the 'href' attribute):

#!/usr/bin/perl

use strict;
use HTTP::Handle;

my $hd = HTTP::Handle->new();
my $regex = q!(?:"([^"]*)"|'([^']*)'|([^\s>]+))!;

$hd->url($ARGV[0] || "http://www.google.com");
$hd->connect();
my $fd = $hd->fd();

undef $/;
my $source = <$fd>;

while ($source =~ s/<a?s+(?:[^>]+s+)*href=$regex[^>]*>//s) {
	my $link = $1 || $2 || $3;
	print "Link: $link\n";
}

Comments: 0 (view comments)
Tags:
Permalink: /geekery/119
posted at: 04:53

Sun, 20 Jun 2004

Removing duplicates from arrays in Perl

After needing to do this in a project of mine, I went googling and found, as expected, a wide variety of solutions. Solutions ranged from using map, foreach, grep, etc.. All using things like a temporary hash to count instances and ensure uniqueness - but I remembered that hashes have unique keys and that hashes are often treated the same way as arrays in perl, so my solution is as follows:

my %foo;  #Temp var
my @a = qw (hello there hello how are you today there what now hello hello hello);

%foo = @a;
@a = keys(%foo);

I also have a one-liner version:

# Assumedly, @a is already defined and has stuff in it, perhaps...

@a = do { my %foo = @a; keys(%foo) };

Comments: 3 (view comments)
Tags: ,
Permalink: /geekery/118
posted at: 19:19

Sat, 19 Jun 2004

Neat Perl DBI features

Something I'd never bothered doing is reading perldoc DBI. I was looking through it today looking for a function I forgot the name of and I ran across a function, selectall_hashref. This thing is *totally* sweet. It takes a SQL query and a key field as arguments. It then puts all the results into a hash with the "key field" as the hash key. I never knew about this until now. If you're still at a loss for what it saves you, here's what it shortens:

my $db = DBI->connect(...);

my $res = $db->preprae("SELECT * FROM foo");
$res->execute();

my $foo;
while (my $hr = $res->fetchrow_hashref()) {
	$foo->{$hr->{"id"}} = $hr;
}

And using selectall_hashref instead:

my $db = DBI->connect(...);

my $foo = $db->selectall_hashref("SELECT * FROM foo", "id");

Comments: 0 (view comments)
Tags:
Permalink: /geekery/117
posted at: 16:27

Sun, 06 Jun 2004

tic 1.0 / Term::Shelly 0.2

Candice wanted a nonsucky, terminal-based aim client, and I was bored enough to write one. So after about 3 months of off-and-on development I've got enough work done on it to safely say it's useable. She and I both use it more or less regularly now. Including work done on my new module, Term::Shelly, I've decided to tag a release. tic's mostly bug free at the moment and supports a fair set of the features in AIM.

Features of tic:

  • Basic functionality: sending messages, receiving them, checking info, setting away, seeing who's online, etc.
  • Aliases can be created on demand to do whatever you want. You can cascade aliases aswell, meaning you can have one alias call another.
  • Logging is supported, logs go to ~/.tic/
  • Persistent settings can be put in ~/.ticrc. See below for more info on this.
  • Tab completion of all commands, aswell as completion of screennames for certain commands (like /msg)
  • A shortcut for replying to whoever messaged you most recently - typing ; at the beginning of the line will auto fill in "/msg [whoever]" for you.

You'll need 3 perl modules: Net::OSCAR, Term::ReadKey, and a module I wrote, Term::Shelly. The first two you can get from cpan. However, Term::Shelly hasn't been uploaded yet (I'm lazy) so you'll have to download it from me:
Term::Shelly

.ticrc syntax
The basic syntax is: key = value
Comments are prefixed with #, and whitespace between the key, =, and value. The following settings are available:

  • screenname = someusername
  • password = yourpassword
  • port = 8080
  • log = all|off
  • timestamp = on|off

Download tic over there on the right...

Comments: 0 (view comments)
Tags:
Permalink: /geekery/116
posted at: 01:45

Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)

Tag Cloud

Calendar

< June 2004 >
SuMoTuWeThFrSa
   1 2 3 4 5
6 7 8 9101112
13141516171819
20212223242526
27282930   

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati