#!/usr/bin/perl -w use strict; use Config; die "usage: $0 file(s)\n" unless scalar @ARGV; ## check whether the used version of Perl has been compiled ## with thread support unless ($Config{useithreads}) { die "The used version Perl does not support threads!\n"; } require threads; require Lingua::Lid; my $max_threads = 2; my $nr = 0; ## while there are files given as arguments left... while (@ARGV) { my $file = shift(@ARGV); ## ...create a thread that identifies the file's language ## and charset and returns the determined results in ## scalar context when it is requested to join() to the ## main thread of control again. threads->create({ context => "scalar" }, sub { ## identify language and charset of the file using ## lid's lid_ffile my $res = Lingua::Lid::lid_ffile($file); return { file => $file, ## $res will be undef if no result could be ## computed result => $res, ## in this case, Lingua::Lid::errstr() will ## return the error message reported by lid's ## lid_strerror() function. errstr => Lingua::Lid::errstr() }; }); ## if the maximum amount of concurrent threads has been ## reached or no files are left to identify, join all ## threads and print their results. if (scalar @ARGV % $max_threads == 0 || ! scalar @ARGV) { foreach my $thread (threads->list()) { my $rv = $thread->join(); printf("%02d: %s: %s\n", ++$nr, $rv->{file}, $rv->{result} ? join(", ", $rv->{result}->{language}, $rv->{result}->{isocode}, $rv->{result}->{encoding}) : "ERROR: $rv->{errstr}" ); } } }