File Coverage

blib/lib/Data/Lua.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Data::Lua;
2              
3 4     4   5322 use Carp qw();
  4         9  
  4         73  
4 4     4   18 use warnings;
  4         7  
  4         109  
5 4     4   573 use strict;
  4         8  
  4         302  
6              
7              
8             our $VERSION = '0.02';
9              
10              
11             # Workaround for a used-only-once warning being generated by Inline::Lua.
12             { my $dummy = $Inline::Lua::Nil }
13              
14              
15 0           use Inline Lua => q{
16             function _lua_loadstring(string)
17             local func, err = loadstring(string)
18             return _vars_from_func(func, err)
19             end
20              
21              
22             function _lua_loadfile(filename)
23             local func, err = loadfile(filename)
24             return _vars_from_func(func, err)
25             end
26              
27              
28             function _vars_from_func(func, err)
29             if func == nil then return nil, err end
30              
31             local env = {}
32             setfenv(func, env)
33             func()
34             return env
35             end
36 4     4   3704 };
  0            
37              
38              
39              
40              
41             sub parse {
42             my($class, $string) = @_;
43             return undef unless defined $string and length $string;
44             return _check(_lua_loadstring($string));
45             }
46              
47              
48              
49              
50             sub parse_file {
51             my($class, $file) = @_;
52             return undef unless defined $file and length $file;
53             return _check(_lua_loadfile($file));
54             }
55              
56              
57              
58              
59             # This subroutine is something of a workaround for a quirk of Inline::Lua.
60             # If the value being parsed (file or string) sets no variables then the env
61             # table is empty. Inline::Lua translates an empty table to an array. You
62             # should not otherwise get an array out of a top-level parse of variables, so
63             # we convert that array to an empty hash.
64             #
65             # It also (now) checks for parse errors and raises an exception.
66              
67             sub _check {
68             my($return, $error) = @_;
69              
70             if (not defined $return) {
71             Carp::croak("Lua parse error: $error");
72              
73             } elsif (ref $return eq 'ARRAY') {
74             if (@$return) { return { 0 => $return } }
75             else { return {} }
76             }
77              
78             return $return;
79             }
80              
81              
82              
83             1;
84              
85             __END__