File Coverage

blib/lib/Cwd/Guard.pm
Criterion Covered Total %
statement 32 32 100.0
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 1 2 50.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             package Cwd::Guard;
2              
3 3     3   44951 use strict;
  3         6  
  3         78  
4 3     3   11 use warnings;
  3         4  
  3         71  
5 3     3   1334 use parent 'Exporter';
  3         790  
  3         16  
6              
7             our @EXPORT_OK = qw/cwd_guard/;
8             our $Error;
9              
10             our $VERSION = '0.05';
11              
12 3     3   257 use constant USE_FCHDIR => eval { opendir my $dh, '.'; chdir $dh; 1 };
  3         4  
  3         4  
  3         93  
  3         26  
  3         279  
13 3     3   2115 use if !USE_FCHDIR, Cwd => qw/getcwd/;
  3         35  
  3         13  
14              
15             sub cwd_guard {
16 2     2 1 354 my $dir = shift;
17 2         8 __PACKAGE__->new($dir);
18             }
19              
20             sub new {
21 2     2 0 3 my $class = shift;
22 2         2 my $dir = shift;
23 2         2 my $cwd;
24 2         3 if (USE_FCHDIR) { opendir $cwd, '.' } else { $cwd = getcwd() }
  2         45  
25             my $callback = sub {
26 1     1   14 chdir $cwd;
27 2         8 };
28 2 50       23 my $result = defined $dir ? chdir($dir) : chdir();
29 2         12 $Error = $!;
30 2 100       14 return unless $result;
31 1         4 bless $callback, $class;
32             }
33              
34             sub DESTROY {
35 1     1   620 $_[0]->();
36             }
37              
38              
39             1;
40             __END__