CPAN – Grey Panthers Savannah https://grey-panther.net Just another WordPress site Mon, 27 Nov 2006 06:52:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 206299117 Fixing Open Source https://grey-panther.net/2006/11/fixing-open-source.html https://grey-panther.net/2006/11/fixing-open-source.html#comments Mon, 27 Nov 2006 06:52:00 +0000 https://grey-panther.net/?p=1002 Before you all jump over me: I won’t be talking here about a silver bullet which would take the open source movement in the right direction, ensure its competitiveness or something like that. What I’ll talk about is the joy that you can have by fixing open source scripting libraries.

To be specific: in one of my Perl scripts I wanted to send mail. The problem was that I didn’t have access to a SMTP server on that machine. After toying around with the example code of Net::SMTP::Server I concluded that it didn’t work and went off to find some lightweight solution. One of the more promising solutions was RunMail, however I was still uncomfortable about running binary code of unknown origin on the server. As a results I fired up telnet and the Perl debugger and stepped into the library source code. There I found the following part:

    # Loop through the recipient list.
    foreach $target (@{$self->{TO}}) {
 my $rr;
 my $domain = /@(.*)/;
 my $res = new Net::DNS::Resolver;
 my @mx = mx($res, defined($1) ? $1 : hostdomain);

I promptly replaced the line so that $1 would be set, and voila, it works!

    # Loop through the recipient list.
    foreach $target (@{$self->{TO}}) {
 my $rr;
 $target =~ /@(.*)/;
 my $res = new Net::DNS::Resolver;
 my @mx = mx($res, defined($1) ? $1 : hostdomain);

Now, if this would have been a binary library, I would had to: (a) get the source code (b) set up a build environment (c) compile from source (to get the debugging symbols) and (c) figure out how to integrate the debugging version with the release version such that I get a breakpoint where I need to and I get the debugging symbols. And I didn’t even speak about the rich introspection capabilities of scripting language debuggers!

Update: the actual problem was in Net::SMTP::Server::Relay, not in Net::SMTP::Server.

Update: below you can see the full code of the module (since it is rather short). The part shown with bold is the one which needed to be changed:

package Net::SMTP::Server::Relay;

require 5.001;

use strict;
use vars qw($VERSION @ISA @EXPORT);

require Exporter;
require AutoLoader;
use Carp;
use Net::DNS;
use Net::Domain qw(hostdomain);
use Net::SMTP;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw();

$VERSION = '1.1';

sub _relay {
    my $self = shift;
    my $target;
    
    # Loop through the recipient list.
    foreach $target (@{$self->{TO}}) {
 my $rr;
 $target =~ /@(.*)/;
 my $res = new Net::DNS::Resolver;
 my @mx = mx($res, defined($1) ? $1 : hostdomain);
 
 next unless defined(@mx);
 
 # Loop through the MXs.
 foreach $rr (@mx) {
     my $client = new Net::SMTP($rr->exchange) || next;
     
     $client->mail($self->{FROM});
     $client->to($target);
     $client->data($self->{MSG});
     $client->quit;
     
     last;
 }
    }
}

]]>
https://grey-panther.net/2006/11/fixing-open-source.html/feed 1 1002
Why do I love perl? https://grey-panther.net/2006/10/why-do-i-love-perl.html https://grey-panther.net/2006/10/why-do-i-love-perl.html#comments Mon, 09 Oct 2006 07:16:00 +0000 https://grey-panther.net/?p=1047 Before you accuse me of being a fanboy, I must say that I know that every language has bad sides, but also some incontestable merits. I want to talk about two perl related stories here. They both are a kind of short how-tos, and the morale of the story is that you can find ready made libraries for perl for almost anything at CPAN.

The first story is as follows: I maintain an internal website and recently the need came up for the users to write small scripts which then needed to be executed on certain time intervals. My first reaction was: no way am I allowing such a thing, although I trust my users :). Then I started looking for scripting languages which allowed restrictions to be placed on the scripts. A colleague suggested that I use a trimmed down version of Python by deleting all but the needed few libraries, but curiously the official Python distribution recreates the files if they are deleted (probably it considers that it was damaged and tries to repair itself). Then I thought that I compile a custom version of the LUA interpreter, but I felt lazy. Luckily I found the Safe perl module and now I can execute scripts safely. The morale: perl can do almost anything, has libraries for almost everything and it’s almost sure that somebody did it before so play around with google before writing code.

The second story was: I needed to pull some files using SFTP. There is a nice module (as for almost everything!) on the CPAN, but the problem was that it was not available through the ActiveState PPM repositories (yes, I needed to use it on a Windows machine). Grumbling I ended up scripting WinSCP (which is a great program by the way), but calling an external program isn’t a good idea because (a) if there are errors you’ll have very limited knowledge of what the problem exactly was (while normally you could just hook the DIE and WARN handlers and get very detailed information about the problem). (b) it is a security risk because of the braindead implementation of windows where the current directory is in the path so you might end up executing the wrong program (c) there are problems with passing parameters to command line programs because you need to remember to quote them and so on. Then I found a blog post which explained that the cryptographic packages are not included in Perl because of Canadian export law (ActiveState being based in Canada – see, I give all kind of useless trivia information here), but you can use alternative repositories (which I already knew from the time when I installed Log4Perl). So if you need these packages and are stuck on Windows, just start your perl package manager and do a rep add Winnapeg http://theoryx5.uwinnipeg.ca/ppms/. Now you can install packages like Net-SSH or Net-SFTP without a problem. Morale: governments make stupid technological laws and there are alternative package sources.

Happy perl coding!

]]>
https://grey-panther.net/2006/10/why-do-i-love-perl.html/feed 1 1047