File Coverage

blib/lib/Catalyst/Plugin/Setenv.pm
Criterion Covered Total %
statement 9 22 40.9
branch 0 6 0.0
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 33 39.3


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Setenv;
2              
3 2     2   31086 use warnings;
  2         6  
  2         77  
4 2     2   14 use strict;
  2         6  
  2         61  
5 2     2   1696 use MRO::Compat;
  2         6272  
  2         541  
6              
7             =head1 NAME
8              
9             Catalyst::Plugin::Setenv - Allows you to set up the environment from Catalyst's config file.
10              
11             =head1 VERSION
12              
13             Version 0.03
14              
15             =cut
16              
17             our $VERSION = '0.03';
18              
19             =head1 SYNOPSIS
20              
21             In your application:
22              
23             use Catalyst qw/Setenv/;
24              
25             In your config file:
26              
27             environment:
28             FOO: bar
29             BAR: baz
30              
31             When your app starts, C<$ENV{FOO}> will be "bar", and C<$ENV{BAR}> will be
32             "baz".
33              
34             You can also append and prepend to existing environment variables.
35             For example, if C<$PATH> is C</bin:/usr/bin>, you can append
36             C</myapp/bin> by writing:
37              
38             environment:
39             PATH: "::/myapp/bin"
40              
41             After that, C<$PATH> will be set to C</bin:/usr/bin:/myapp/bin>. You
42             can prepend, too:
43              
44             environment:
45             PATH: "/myapp/bin::"
46              
47             which yields C</myapp/bin:/bin:/usr/bin>.
48              
49             If you want a literal colon at the beginning or end of the environment
50             variable, escape it with a C<\>, like C<\:foo> or C<foo\:>. Note that
51             slashes aren't meaningful elsewhere, they're inserted verbatim into
52             the relevant environment variable.
53              
54             =head1 EXPORT
55              
56             A list of functions that can be exported. You can delete this section
57             if you don't export anything, such as for a purely object-oriented module.
58              
59             =head1 FUNCTIONS
60              
61             =head2 setup
62              
63             Calls the other setup methods, and then sets the environment variables.
64              
65             =cut
66              
67             sub setup {
68 0     0 1   my $c = shift;
69            
70 0           $c->next::method(@_);
71            
72 0           my $env = $c->config->{environment};
73 0 0         return unless ref $env eq 'HASH';
74              
75 0           foreach my $key (keys %$env){
76 0           my $value = $env->{$key};
77            
78 0 0         if($value =~ /^:(.+)$/){
    0          
79 0           $ENV{$key} .= $1;
80             }
81             elsif($value =~ /^(.+[^\\]):$/){
82 0           $ENV{$key} = $1. $ENV{$key};
83             }
84             else {
85 0           $value =~ s/(^\\:|\\:$)/:/;
86 0           $value =~ s/(^\\\\:|\\\\:$)/\\:/;
87              
88 0           $ENV{$key} = $value;
89             }
90             }
91            
92 0           return;
93             }
94              
95             =head1 AUTHOR
96              
97             Jonathan Rockway, C<< <jrockway at cpan.org> >>
98              
99             =head1 BUGS
100              
101             =head2 Escaping
102              
103             Things like "\:foo" can't be literally inserted into an environment
104             variable, due to my simplistic escaping scheme. Patches to fix this
105             (but not interpert C<\>s anywhere else) are welcome.
106              
107             =head2 REPORTING
108              
109             Please report any bugs or feature requests to
110             C<bug-catalyst-plugin-setenv at rt.cpan.org>, or through the web interface at
111             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Setenv>.
112             I will be notified, and then you'll automatically be notified of progress on
113             your bug as I make changes.
114              
115             =head1 SUPPORT
116              
117             You can find documentation for this module with the perldoc command.
118              
119             perldoc Catalyst::Plugin::Setenv
120              
121             You can also look for information at:
122              
123             =over 4
124              
125             =item * The Catalyst Website
126              
127             L<http://www.catalystframework.org/>
128              
129             =item * AnnoCPAN: Annotated CPAN documentation
130              
131             L<http://annocpan.org/dist/Catalyst-Plugin-Setenv>
132              
133             =item * CPAN Ratings
134              
135             L<http://cpanratings.perl.org/d/Catalyst-Plugin-Setenv>
136              
137             =item * RT: CPAN's request tracker
138              
139             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Setenv>
140              
141             =item * Search CPAN
142              
143             L<http://search.cpan.org/dist/Catalyst-Plugin-Setenv>
144              
145             =back
146              
147             =head1 ACKNOWLEDGEMENTS
148              
149             Thanks to Bill Moseley's message to the mailing list that prompted me
150             to write this.
151              
152             =head1 COPYRIGHT & LICENSE
153              
154             Copyright 2006 Jonathan Rockway, all rights reserved.
155              
156             This program is free software; you can redistribute it and/or modify it
157             under the same terms as Perl itself.
158              
159             =cut
160              
161             1; # End of Catalyst::Plugin::Setenv