Archive for the Perl Category

Finding all matches to a Regular Expression

Posted in Perl with tags on March 30, 2009 by razorvine

@matches= $string =~ m/$regex/g;

Bulk Deleting Posts On WordPress

Posted in Perl with tags , on March 8, 2009 by razorvine

Either i’m vision impaired (well, i am, but …), or wordpress.com doesn’t offer useful bulk operations on posts. Fortunately, there is Perl, with the WordPress::XMLRPC module. I wanted to remove all posts which contained a certain keyword in their titles:


#!/usr/bin/perl -w
#use strict;
use WordPress::XMLRPC;

my $o = WordPress::XMLRPC->new({
  username => 'user',
  password => 'password,
  proxy => 'http://razorvine.wordpress.com/xmlrpc.php',
});
my $posts= $o->getRecentPosts(2210); #get all posts
my $nelem = scalar @{ $posts }; #how many posts are there?
my $TITLE = "": # The title i wanted to use as filter

for (my $i=0 ; $i<$nelem; $i++)
{
  my $post = $$posts[$i];
  my $titel = $post->{title};
  if ( $titel =~ /$TITLE/ )
  {
    $o->deletePost($post->{postid});
  }
}

Filtering by categories, author, etc would work the same way, of course. Just change line #17 appropriately.

Makeshift DNS

Posted in Perl with tags , on March 8, 2009 by razorvine

Sometimes there is a problem with the dyndns.com update scripts, leaving my server unreachable. To solve this, i set up this blog as a makeshift backup DNS server. It sends my current server IP (obtained by wget whatismyip.org) to http://razorvine.wordpress.com/ip/.

#!/usr/bin/perl -w
use strict;
use WordPress::XMLRPC;

#the url of the page
my $URL = qw(http://razorvine.wordpress.com/ip/) ;

my $o = WordPress::XMLRPC->new({
   username => 'name',
   password => 'password',
   proxy => 'http://razorvine.wordpress.com/xmlrpc.php',
});

#load your current ip. I load the result of wget whatismyip.org
my $ip = "";

#get all pages
my $pages= $o->getPages();

my $nelem = scalar @{ $pages };

for (my $i=0 ; $i<$nelem; $i++)
{
   my $page = $$pages[$i];
   if ( $page->{permaLink} eq $URL )
   {
      $page->{description}=$ip;
      my $id = $page->{page_id};
      $o->editPage($id, $page) or die ( $o->errstr );
   }
}

Then i added a cronjob to have this script execute regularly.

Perl WordPress Interface (over XMLRPC)

Posted in Bookmarks, Perl on March 8, 2009 by razorvine