Codebase list libdir-self-perl / c0c789f
[svn-inject] Installing original source of libdir-self-perl (0.10) Gregor Herrmann 13 years ago
7 changed file(s) with 182 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 Makefile.PL
1 MANIFEST
2 README
3 t/Dir-Self.t
4 t/zerlegungsgleichheit/d.t
5 lib/Dir/Self.pm
6 META.yml Module meta-data (added by MakeMaker)
0 --- #YAML:1.0
1 name: Dir-Self
2 version: 0.10
3 abstract: a __DIR__ constant for the directory your source file is in
4 license: ~
5 author:
6 - Lukas Mai <l.mai @web.de>
7 generated_by: ExtUtils::MakeMaker version 6.44
8 distribution_type: module
9 requires:
10 Carp: 0
11 File::Spec: 0
12 Test::More: 0
13 meta-spec:
14 url: http://module-build.sourceforge.net/META-spec-v1.3.html
15 version: 1.3
0 use 5.005;
1 use ExtUtils::MakeMaker;
2 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
3 # the contents of the Makefile that is written.
4 WriteMakefile(
5 NAME => 'Dir::Self',
6 VERSION_FROM => 'lib/Dir/Self.pm', # finds $VERSION
7 PREREQ_PM => {
8 'Test::More' => 0,
9 'Carp' => 0,
10 'File::Spec' => 0,
11 },
12 ($] >= 5.005 ? ## Add these new keywords supported since 5.005
13 (ABSTRACT_FROM => 'lib/Dir/Self.pm', # retrieve abstract from module
14 AUTHOR => 'Lukas Mai <l.mai @web.de>') : ()),
15 );
0 Dir-Self version 0.10
1 =====================
2
3 Provides a __DIR__ pseudo-constant expanding to the full path to the
4 surrounding source file. Useful for access to files "in the same
5 directory".
6
7 INSTALLATION
8
9 To install this module type the following:
10
11 perl Makefile.PL
12 make
13 make test
14 make install
15
16 DEPENDENCIES
17
18 Test::More (build only)
19 File::Spec
20 Carp
21
22 COPYRIGHT AND LICENCE
23
24 Copyright (C) 2007, 2008 by Lukas Mai
25
26 This library is free software; you can redistribute it and/or modify
27 it under the same terms as Perl itself, either Perl version 5.8.8 or,
28 at your option, any later version of Perl 5 you may have available.
0 package Dir::Self;
1
2 use 5.005;
3 use strict;
4
5 use File::Spec ();
6
7 *VERSION = \'0.10';
8
9 sub _croak {
10 require Carp;
11 local $^W = 0;
12 *_croak = \&Carp::croak;
13 goto &Carp::croak;
14 }
15
16 sub __DIR__ () {
17 my $level = shift || 0;
18 my $file = (caller $level)[1];
19 File::Spec->rel2abs(join '', (File::Spec->splitpath($file))[0, 1])
20 }
21
22 sub _const {
23 my $value = shift;
24 sub () { $value }
25 }
26
27 sub import {
28 my $class = shift;
29 my $caller = caller;
30
31 @_ or @_ = '__DIR__';
32
33 for my $item (@_) {
34 if ($item eq '__DIR__') {
35 no strict 'refs';
36 *{$caller . '::__DIR__'} = \&__DIR__;
37 } elsif ($item eq ':static') {
38 no strict 'refs';
39 *{$caller . '::__DIR__'} = _const &__DIR__(1);
40 } else {
41 _croak qq{"$item" is not exported by the $class module};
42 }
43 }
44 }
45
46 1
47 __END__
48
49 =head1 NAME
50
51 Dir::Self - a __DIR__ constant for the directory your source file is in
52
53 =head1 SYNOPSIS
54
55 use Dir::Self;
56
57 use lib __DIR__ . "/lib";
58
59 my $conffile = __DIR__ . "/config";
60
61 =head1 DESCRIPTION
62
63 Perl has two pseudo-constants describing the current location in your source
64 code, C<__FILE__> and C<__LINE__>. This module adds C<__DIR__>, which expands
65 to the directory your source file is in, as an absolute pathname.
66
67 This is useful if your code wants to access files in the same directory, like
68 helper modules or configuration data. This is a bit like L<FindBin> except
69 it's not limited to the main program, i.e. you can also use it in modules. And
70 it actually works.
71
72 As of version 0.10 each use of C<__DIR__> recomputes the directory name; this
73 ensures that files in different directories that share the same package name
74 get correct results. If you don't want this, C<use Dir::Self qw(:static)> will
75 create a true C<__DIR__> constant in your package that contains the directory
76 name at the point of C<use>.
77
78 =head1 AUTHOR
79
80 Lukas Mai E<lt>l.mai @web.deE<gt>
81
82 =head1 COPYRIGHT AND LICENSE
83
84 Copyright (C) 2007, 2008 by Lukas Mai
85
86 This library is free software; you can redistribute it and/or modify
87 it under the same terms as Perl itself, either Perl version 5.8.8 or,
88 at your option, any later version of Perl 5 you may have available.
89
90 =cut
0 #!perl -w
1 use strict;
2
3 use Test::More tests => 3;
4
5 use Dir::Self;
6 ok 1, 'use Dir::Self';
7
8 #########################
9
10 # Insert your test code below, the Test::More module is use()ed here so read
11 # its man page ( perldoc Test::More ) for help writing this test script.
12
13 like __DIR__, '/\bt$/';
14
15 use lib __DIR__;
16 require "zerlegungsgleichheit/d.t";
17 like zd(), '/\bzerlegungsgleichheit$/';
0 use strict;
1
2 sub zd { __DIR__ }
3
4 1