A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Which Perl modules are installed?

To run: perl script.pl $(cat list_of_modules.txt)

#!/usr/bin/perl
use strict;
use warnings;

foreach my $mod (@ARGV) {
(my $fn="$mod.pm")=~s|::|/|g; # Foo::Bar::Baz => Foo/Bar/Baz.pm
if (eval { require $fn; 1; }) {
print "Module $mod loaded ok\n";
} else {
print "Could not load $mod. Error Message: $@\n";
}
}

# Thanks Perl Monks

Also try typing perldoc perllocal to see a list of all installed modules.

XSL tips

  • In XSL templates, when generating new static elements with attributes, instead of <xsl:element name="foo"><xsl:attribute name="bar"><xsl:value-of select="'baz'"> etc, you can just do <foo bar="baz">! The main use for <xsl:element> and <xsl:attribute> is for dynamically named elements and attributes.
  • Print debug statements like this: <xsl:message><xsl:text>min_score = </xsl:text><xsl:value-of select="$min_score"/></xsl:message>

Stop XML::LibXSLT and XML::LibXML connecting to w3.org

If you have this line in your XML/XSL:

<!DOCTYPE xml PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Then your parser may connect to w3.org for validation. If w3 blocks you for hammering their server, then you may get this error message: http error : Operation in progress

http error : Operation in progress

If you don't want/need validation against the DTD (and you probably don't), you can turn off that feature in XML::LibXML:

1.70
$parser = XML::LibXML->new(load_ext_dtd => 0);

< 1.70
$parser = XML::LibXML->new();
$parser->load_ext_dtd(0);

Tailing all the logs files

Truncate all the log files:
find . -name "*.log" -exec cp /dev/null {} \;

Tail all the log files at once:
tail -f $(find . -name "*.log")

Free counter

Needs no registration, works with Blogger: http://www.e-zeeinternet.com/

Working with iptables on Redhat linux

iptables is the default firewall software on Redhat Linux

Display a list of ports it's restricting:
sudo /sbin/iptables -L

This is what it looks like when it's not restricting anything:

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination


If it doesn't look like that and you need to open a port, edit this file:
/etc/sysconfig/iptables

It might look something like this:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT


Add the line in bold above to open up port 80.

Restart like this:
/etc/init.d/iptables restart

Min/Max for XSLT

Obtaining the MIN:
<!-- Create a variable named $minEventDate containing the MIN date -->
<xsl:variable name="minEventDate">
<xsl:for-each select="event">
<xsl:sort select="@date" data-type="text" order="ascending" />
<xsl:if test="position() = 1">
<xsl:value-of select="@date" />
</xsl:if>
</xsl:for-each>
</xsl:variable>

Obtaining the MAX:
<!-- Create a variable named $maxEventDate containing the MAX date -->
<xsl:variable name="maxEventDate">
<xsl:for-each select="event">
<xsl:sort select="@date" data-type="text" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="@date" />
</xsl:if>
</xsl:for-each>
</xsl:variable>

XSLT1 processing, using Perl

Example taken from XML::LibXSLT docs, code commented and replaced because of old versions of libs:
$ perl -MXML::LibXML -le'print $XML::LibXML::VERSION'

1.69

$ perl -MXML::LibXSLT -le'print $XML::LibXSLT::VERSION'

1.59


Code:

#!/usr/bin/perl

use strict;
use warnings;
use XML::LibXSLT;
use XML::LibXML;

my $xslt = XML::LibXSLT->new();

#my $source = XML::LibXML->load_xml(location => 'file.xml');
my $parser = XML::LibXML-> new();
my $source = $parser->parse_file('file.xml');

#my $style_doc = XML::LibXML->load_xml(location=>'file.xsl', no_cdata=>1);
my $parser2 = XML::LibXML-> new();
my $style_doc = $parser2->parse_file('file.xsl');

my $stylesheet = $xslt->parse_stylesheet($style_doc);

my $results = $stylesheet->transform($source);

#print $stylesheet->output_as_bytes($results);
print $results->toString;

Hide number sending on Blackberry Bold

  • Open phone application
  • Press menu
  • Choose 'options'

Rename multiple files in bash

for i in *.JPG; do mv "$i" "${i/.JPG}".jpg; done
or
for i in *.jpg; do mv "$i" "`basename $i .jpg`.JPG"; done