Sunday, November 06, 2011

» Installing Perl Module RPMs on openSUSE

The additional repository devel:languages:perl has quite a slew of Perl module packages in it (over 2000 at the time of writing).

Hence, if you are often using and requiring Perl modules, it makes a lot of sense to add it to your list of repositories, which you can do with the following command (as root):

zypper addrepo http://r.opensu.se/devel:languages:perl.repo

(if you wonder what r.opensu.se is, read up on it here: r.opensu.se).

A little known fact is that when building RPM packages, there is a post-build script that analyzes the files that are part of the resulting package in order to scan for Perl modules. For each of those Perl modules, it adds a Provides with the Perl name of that Perl module, with a specific notation which is like this: perl(Name::of::the::Perl::package).

As an example, if you need the Perl module Net::SMTP::SSL, you just need to do this:

zypper install 'perl(Net::SMTP::SSL)'

Note that you should indeed put that parameter to the zypper install command into quotes, as if you don't, bash will attempt to interpret the braces and give a syntax error.

Now, in this case, it is fairly simple, as the Perl module Net::SMTP::SSL is provided by the RPM package perl-Net-SMTP-SSL (at least on openSUSE/SLE), so you might have been able to derive the name of the RPM package from the name of the missing Perl module on your own. But that Perl module could very well be part of a package with a different name: for example, the Perl module Class::MOP is not in the RPM package perl-Class-MOP but in the RPM package perl-Moose (because it's part of the Moose CPAN module).

And a last little trick: if you only want to find out which RPM package(s) provides specific Perl modules, you may also use this:

zypper what-provides 'perl(Class::MOP)'

(Note that this one only works on repositories that you have in your list of active repositories, which you can see with zypper repos or zypper lr.)

Labels: , , ,

Sunday, August 01, 2010

» Shell trick: CRLF to LF

I do this again and again in RPM .spec files, so just in case: a little trick to remove the CR from CRLF ;) (Windows uses two bytes for end-of-line markers, namely CR and LF, while Linux and Unix only use one: LF) You might of course use dos2unix or recode, but this one works in-place, which means that you don't need to make a copy, work on the copy and then copy (heh) that file back onto the original -- here is how to do it with dos2unix:
cp file file.orig && dos2unix file
And here the shorter version, using sed and its in-place editing feature:
sed -i 's/\r$//' file
Might as well do it with Perl, which also supports in-place editing:
perl -pi 's/\r$//' file
Update: as _Marcus_ just told me, recode actually does in-place editing by default, my bad :)

Labels: , , , ,

Saturday, June 26, 2010

» Finding out whether a script is piped

When writing shell scripts, sometimes you need to find out whether your script is being piped into something else (e.g. a pager like less, or just running on the terminal (unpiped). One use case could be to determine whether you want to enable ANSI style colouring of the output or not. Turns out it is really simple to do.

Shell

To do that from a bash script, use [ -t 0 ], which is true when unpiped, and false when piped:
[ -t 0 ] && COLOR=1 || COLOR=0

Perl, others

In other scripting languages (or even in C/C++), where the isatty function is available, use that -- this example being in Perl:
use POSIX 'isatty';
my $color = isatty(STDOUT);

Labels: ,