source: LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm @ 291

Last change on this file since 291 was 267, checked in by nanardon, 15 years ago
  • more debug messages
  • Property svn:keywords set to Id Rev
File size: 1.3 KB
Line 
1package LATMOS::Accounts::Utils;
2use 5.010000;
3use strict;
4use warnings;
5use Exporter ();
6use vars qw(@ISA @EXPORT_OK @EXPORT);
7use utf8;
8use LATMOS::Accounts::Log;
9
10our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
11
12@ISA = qw(Exporter);
13@EXPORT = qw(to_ascii exec_command);
14@EXPORT_OK = qw(to_ascii exec_command);
15
16sub to_ascii {
17    my ($text) = @_;
18    return $text unless(defined($text));
19    utf8::decode($text) unless(utf8::is_utf8($text));
20    $text =~ tr/uàâÀÂÄÀçéÚêëÉÈÊËïîÏÎöÎÖÔÌûÛÜ/uaaaAAAceeeeEEEEiiIIooOOuuUU/;
21    $text =~ s/([^[:ascii:]])//;
22    $text
23} 
24
25sub exec_command {
26    my ($command, $env) = @_;
27
28    my @exec = ref $command
29        ? (@$command)
30        : ($command);
31    la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec));
32
33    my $pid = fork;
34    if (!defined($pid)) {
35        la_log(LA_ERR, "Can't launch script: cannot fork");
36    } elsif ($pid) {
37        # Father
38        waitpid($pid, 0);
39        if (my $exitstatus = $?) {
40            la_log(LA_ERR, 'command %s exit with status %d',
41                join(' ', @exec), $exitstatus);
42        }
43    } else {
44        # Child
45        foreach (keys %{ $env || {} }) {
46            $ENV{"LA_$_"} = $env->{$_};
47        }
48        exec(@exec);
49        exit($!);
50    }
51    1
52}
53
54
551;
Note: See TracBrowser for help on using the repository browser.