| 1 | # $Id: /mirror/youri/soft/Utils/trunk/lib/Youri/Utils.pm 2334 2007-03-23T08:48:22.544634Z guillomovitch $ | ||||||||||
| 2 | package Youri::Utils; | ||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | Youri::Utils - Youri shared functions | ||||||||||
| 7 | |||||||||||
| 8 | =head1 DESCRIPTION | ||||||||||
| 9 | |||||||||||
| 10 | This module implement some helper functions for all youri applications. | ||||||||||
| 11 | |||||||||||
| 12 | =cut | ||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use UNIVERSAL::require; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use version; our $VERSION = qv('0.2.1'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | our @EXPORT = qw( | |||||||||
| 24 | create_instance | ||||||||||
| 25 | log_message | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | =head2 create_instance($class, $config, $options) | ||||||||||
| 29 | |||||||||||
| 30 | Create an instance from a plugin implementing given interface, using given | ||||||||||
| 31 | configuration and local options. | ||||||||||
| 32 | Returns a plugin instance, or undef if something went wrong. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub create_instance { | ||||||||
|
Subroutine
create_instance: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 37 | 0 | my ($interface, $config, $options) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | croak 'No interface given' unless $interface; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | ▶ | croak 'No config given' unless $config; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 0 | my $class = $config->{class}; | |||||||||
| 43 | 0 | ▶ | if (!$class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | carp "No class given, can't load plugin"; | |||||||||
| 45 | 0 | return; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | # ensure loaded | ||||||||||
| 49 | 0 | $class->require(); | |||||||||
| 50 | |||||||||||
| 51 | # check interface | ||||||||||
| 52 | 0 | ▶ | if (!$class->isa($interface)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | carp "$class is not a $interface"; | |||||||||
| 54 | 0 | return; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | return $class->new( | ||||||||||
| 58 | 0 | ▶ | $config->{options} ? %{$config->{options}} : (), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | $options ? %{$options} : (), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | ); | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =head2 log_message($message, $time, $process) | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub log_message { | ||||||||
|
Subroutine
log_message: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 68 | 0 | my ($message, $time, $process) = @_; | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | print DateTime->now()->set_time_zone('local')->strftime('[%H:%M:%S] ') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | if $time; | ||||||||||
| 72 | 0 | ▶ | print "$PID " if $process; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | print "$message\n"; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 77 | |||||||||||
| 78 | Copyright (C) 2002-2006, YOURI project | ||||||||||
| 79 | |||||||||||
| 80 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 1 | 1; | |||||||||