Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Apr 302010
 

Character classes not working in grep command

Seems grep / egrep does not support character classes, e.g.

# find -type f | grep -e '[\d]'

It finds all the file contains the character d, instead of decimal number(s).

Answer:

By default, egrep only understand the POSIX Basic Regular Expressions (BRE) standard, so what would like to use is:

# find -type f | grep -e '[[:digit:]]'

or you can force to interpret as a Perl regular expression

# find -type f | grep -P -e '[\d]'

Apr 252010
 

eq vs == in Perl

Answer:

1. eq

It perform string comparison.

my $foo = "123";
my $bar = "123";

print $foo eq $bar; # output "1"

2. ==

It perform numeric comparison, string is first converted to numeric value before compare.

my $foo = "foo";
my $bar = "bar";

print $foo == $bar; # output "1", since 0 = 0 as strings are converted to 0 in both variables
Mar 012010
 

How to remove install CPAN modules?

Answer:

1. Copy the script below, e.g. remove_module.pl

#!/usr/bin/perl -w 

use ExtUtils::Packlist; 
use ExtUtils::Installed; 

$ARGV[0] or die "Usage: $0 Module::Name\n"; 

my $module = $ARGV[0]; 

my $inst = ExtUtils::Installed->new(); 

foreach my $item (sort($inst->files($module))) { 
    print "Removing $item\n"; 
    unlink $item; 
} 

my $packfile = $inst->packlist($module)->packlist_file(); 
print "Removing $packfile\n"; 
unlink $packfile; 

2. Execute the script to remove module, e.g. MongoDB

# perl remove_module.pl MondoDB