| 1 | #----------------------------------------------------------------- | ||
| 2 | # MOSES::MOBY::Utils | ||
| 3 | # Author: Martin Senger <martin.senger@gmail.com>, | ||
| 4 | # Edward Kawas <edward.kawas@gmail.com> | ||
| 5 | # For copyright and disclaimer see below. | ||
| 6 | # | ||
| 7 | # $Id: Utils.pm,v 1.7 2009/10/13 16:46:21 kawas Exp $ | ||
| 8 | #----------------------------------------------------------------- | ||
| 9 | |||
| 10 | package MOSES::MOBY::Generators::Utils; | ||
| 11 | use File::Spec; | ||
| 12 | use strict; | ||
| 13 | |||
| 14 | # add versioning to this module | ||
| 15 | use vars qw /$VERSION/; | ||
| 16 | $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /: (\d+)\.(\d+)/; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | MOSES::MOBY::Utils - what does not fit elsewhere | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | # find a file located somewhere in @INC | ||
| 25 | use MOSES::MOBY::Generators::Utils; | ||
| 26 | my $file = MOSES::MOBY::Generators::Utils->find_file ('resource.file'); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | General purpose utilities. | ||
| 31 | |||
| 32 | =head1 AUTHORS | ||
| 33 | |||
| 34 | Martin Senger (martin.senger [at] gmail [dot] com) | ||
| 35 | Edward Kawas (edward.kawas [at] gmail [dot] com) | ||
| 36 | |||
| 37 | =head1 SUBROUTINES | ||
| 38 | |||
| 39 | =head2 find_file | ||
| 40 | |||
| 41 | Try to locate a file whose name is created from the C<$default_start> | ||
| 42 | and all elements of C<@names>. If it does not exist, try to replace | ||
| 43 | the C<$default_start> by elements of @INC (one by one). If neither of | ||
| 44 | them points to an existing file, go back and return the | ||
| 45 | C<$default_start> and all elements of C<@names> (even - as we know now | ||
| 46 | - such file does not exist). | ||
| 47 | |||
| 48 | There are two or more arguments: C<$default_start> and C<@names>. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | my %full_path_of = (); | ||
| 53 | |||
| 54 | #----------------------------------------------------------------- | ||
| 55 | # find_file | ||
| 56 | #----------------------------------------------------------------- | ||
| 57 | sub find_file { | ||
| 58 | my ($self, $default_start, @names) = @_; | ||
| 59 | my $fixed_part = File::Spec->catfile (@names); | ||
| 60 | return $full_path_of{ $fixed_part } if exists $full_path_of{ $fixed_part }; | ||
| 61 | |||
| 62 | my $result = File::Spec->catfile ($default_start, $fixed_part); | ||
| 63 | if (-e $result) { | ||
| 64 | $full_path_of{ $fixed_part } = $result; | ||
| 65 | return $result; | ||
| 66 | } | ||
| 67 | |||
| 68 | foreach my $idx (0 .. $#INC) { | ||
| 69 | $result = File::Spec->catfile ($INC[$idx], $fixed_part); | ||
| 70 | if (-e $result) { | ||
| 71 | $full_path_of{ $fixed_part } = $result; | ||
| 72 | return $result; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | $result = File::Spec->catfile ($default_start, $fixed_part); | ||
| 76 | $full_path_of{ $fixed_part } = $result; | ||
| 77 | return $result; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ |