Thursday, March 01, 2007

Some Basic Perl Help

This is for anyone attempting to script a perl program that can login to a website. I had to use something called 'AUTHORIZATION: BASIC '. This doesn't sound complicated, and it isn't, but it took me a fair time to find the proper method.

If you are logging into an https site, you'll need the Crypt::SSLeay module. This can be acquired via CPAN for the source or via the University of Winnipeg's ppd download site for the ActiveState package. You'll, also need LWP, which comes standard with ActiveState Perl.

Once you've got your functional Perl installation, you just need to be able to create the proper header to POST with. Here, I was stumped, and googled 'perl', 'authorization', and 'basic' all the live long day without finding an example. Finally, I trolled through the lwp cookbook in the ActiveState documentation, and I found this:

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => '
http://www.linpro.no/secret/');
$req->authorization_basic('aas', 'mypassword');
print $ua->request($req)->as_string;


This was all I needed.

Labels: ,

Thursday, September 14, 2006

perl/Tk vs wxperl vs Tkx/Tcl::Tk

This post ported from a test blog called 'nodding off' that I deleted. The timestamp is from the original post.

I wanted to explore the differences between Perlls main(only?!) gui toolkits.

The first is perl/Tk. This was a port of the Tk libraries from Tcl/Tk to perl. It works fairly well, but hasn't been updated in some time (over two years ago, I believe). The effort to port over the code was apparently too great to deal with. I like this version myself, and I have to most experience with it, but I'm going to have to drop it due to lack of support. Which leads me to....

wxPerl. This is an up to date(ppm module dated late last year, source releases from last month available at CPAN) toolkit using the wxWidgets libraries. It seems to work smoothly and quickly(at least the demo/samples do) and looks attractive. However, the code is not that friendly. It looks as if this wrapper is too light, without any simplifications. I can imagine that the code is only slightly shorter than it would be if written in C. Even worse, the drawing/cavas modules are very raw. To move items I will have to code every event and redraw. I'm not saying I couldn't do it, I'm saying I'm far to lazy to unless there aren't any other options.

Which leads us to Tkx/Tcl::Tk. These appear to be cousins of a sort, with Tkx being the Activestate product. I have been unable to mess with this thoroughly, but I was initially turned off by it because you have to install a full Tcl/Tk installation alongside your perl one. Which makes me wonder, why don't I just learn Tcl/Tk and be done with it? Or, why don't I just give up Perl altogether and go with ruby(/tk)? Well, unfortunately I can't switch languages at work, and I really don't want to have to deal with two different languages. So, I'm guessing that I'll go with Tkx for now. I've read that the new Activestates(818+) will contain a *special* tcl-tk dll that will enable this functionality without loading tcl-tk. Here's hoping that it's true and easy to use.

Labels: , ,

Wednesday, August 30, 2006

Vim user tip

This post ported from a test blog called 'nodding off' that I deleted. The timestamp is from the original post

Anyway, here's a little Perl hacker/vim user tip.

If you like using Perl's SelfLoader module, then you will need to have a defined __DATA__ marker in your file. This is what tells the Perl compiler to stop parsing, but allows the SelfLoader module to read in the rest for loading. When editing such a file(module) in vim, you will notice sadly that the code below the __DATA__ marker is all blue'd out, because typically this area doesn't have code in it. To fix this, you need to comment out the __DATA__ marker (like this #__DATA__), but you need to make sure you uncomment before you save. The following settings in your vimrc(gvimrc) file will do the trick:

au BufWritePre *.pm :%s/^#__DATA__ *$/__DATA__/e
au BufWritePost *.pm :%s/^__DATA__ *$/#__DATA__/e

The au(autocmd) verb assigns the actions (the substitutions: :%s....) to *.pm(module) files whenever the buffer is written. The BufWritePre occurs before the file is saved, the BufWritePost occurs after the file has been written. In this way, the __DATA__ marker is uncommented just before writing, and re-commented just after. The user doesn't even perceive that the buffer was changed, but the file ALWAYS is saved with the __DATA__ marker uncommented, making sure that SelfLoader works correctly.

Labels: , , ,