• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mikkoi / env-dot / f4d2d7303

17 May 2026 04:01PM UTC coverage: 94.35%. Remained the same
f4d2d7303

push

github

web-flow
Merge pull request #13 from mikkoi/add-coveralls

Add coveralls

167 of 177 relevant lines covered (94.35%)

18.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.29
/lib/Env/Dot/ScriptFunctions.pm
1
## no critic (ValuesAndExpressions::ProhibitConstantPragma)
2
package Env::Dot::ScriptFunctions;
3
use strict;
1✔
4
use warnings;
1✔
5
use 5.010;
1✔
6

7
use Exporter 'import';
1✔
8
our @EXPORT_OK = qw(
9
  convert_variables_into_commands
10
);
11
our %EXPORT_TAGS = ( 'all' => [qw( convert_variables_into_commands )], );
12

13
use English qw( -no_match_vars );    # Avoids regex performance penalty in perl 5.18 and earlier
1✔
14
use Carp;
1✔
15

16
# ABSTRACT: Read environment variables from a .env file
17

18
our $VERSION = '0.023';
19

20
use constant {
21
    OPTION_FILE_TYPE         => q{file:type},
1✔
22
    OPTION_FILE_TYPE_PLAIN   => q{plain},
23
    OPTION_FILE_TYPE_SHELL   => q{shell},
24
    DEFAULT_OPTION_FILE_TYPE => q{shell},
25
};
1✔
26

27
my %DOTENV_OPTIONS = (
28
    'file:type'             => 1,
29
    'var:allow_interpolate' => 1,
30
);
31

32
my %VAR_OUTPUT = (
33
    q{sh}   => \&_convert_var_to_sh,
34
    q{csh}  => \&_convert_var_to_csh,
35
    q{fish} => \&_convert_var_to_fish,
36
);
37

38
=pod
39

40
=encoding UTF-8
41

42
=for stopwords envdot env
43

44
=head1 STATUS
45

46
This module is currently being developed so changes in the API are possible,
47
though not likely.
48

49

50
=head1 SYNOPSIS
51

52
    use Env::Dot::ScriptFunctions qw( convert_variables_into_commands );
53

54
=head1 DESCRIPTION
55

56
=for stopwords envdot
57

58
This package just contains functions for use
59
in the main package L<Env::Dot> and in
60
the command line tool B<envdot>.
61

62
=head1 FUNCTIONS
63

64
No functions are automatically exported to the calling namespace.
65

66
=head2 convert_variables_into_commands()
67

68
# Return all variables from the F<.env> file
69
# as a list of hashes (name/value pairs).
70
# This list is created in the same order the variables
71
# are read from the files and may therefore contain
72
# the same variable several times.
73

74
=cut
75

76
sub convert_variables_into_commands {
77
    my ( $shell, @vars ) = @_;
2✔
78
    my $out = q{};
2✔
79
    foreach my $variable (@vars) {
2✔
80
        $out .= _convert_variable( $shell, $variable );
4✔
81
        $out .= "\n";
4✔
82
    }
83
    return $out;
2✔
84
}
85

86
# Private subroutines
87

88
sub _convert_variable {
89
    my ( $shell, $variable ) = @_;
4✔
90
    if ( exists $VAR_OUTPUT{$shell} ) {
4✔
91
        return &{ $VAR_OUTPUT{$shell} }($variable);
4✔
92
    }
93
    else {
94
        croak "Unknown shell: $shell";
×
95
    }
96
}
97

98
sub _convert_var_to_sh {
99
    my ($variable) = @_;
7✔
100
    my ( $name, $value, $want_export, $allow_interpolate ) =
101
      ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, );
7✔
102
    my $quote = $allow_interpolate ? q{"} : q{'};
7✔
103
    if ($want_export) {
7✔
104
        return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name;
4✔
105
    }
106
    else {
107
        return sprintf "%s=$quote%s$quote", $name, $value;
3✔
108
    }
109
}
110

111
sub _convert_var_to_csh {
112
    my ($variable) = @_;
3✔
113
    my ( $name, $value, $want_export, $allow_interpolate ) =
114
      ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, );
3✔
115
    my $quote = $allow_interpolate ? q{"} : q{'};
3✔
116
    if ($want_export) {
3✔
117
        return sprintf "setenv %s $quote%s$quote", $name, $value;
2✔
118
    }
119
    else {
120
        return sprintf "set %s $quote%s$quote", $name, $value;
1✔
121
    }
122
}
123

124
sub _convert_var_to_fish {
125
    my ($variable) = @_;
×
126
    my ( $name, $value, $want_export, $allow_interpolate ) =
127
      ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, );
×
128
    my $quote = $allow_interpolate ? q{"} : q{'};
×
129
    return sprintf "set -e %s; set -x -U %s $quote%s$quote", $name, $name, $value;
×
130
}
131

132
=pod
133

134
=head1 AUTHOR
135

136
Mikko Koivunalho <mikkoi@cpan.org>
137

138
=head1 COPYRIGHT AND LICENSE
139

140
This software is copyright (c) 2023 by Mikko Koivunalho.
141

142
This is free software; you can redistribute it and/or modify it under
143
the same terms as the Perl 5 programming language system itself.
144

145
=cut
146

147
1;
148
__END__
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc