File Coverage

lib/Panotools/Script/Line.pm
Criterion Covered Total %
statement 44 47 93.6
branch 5 6 83.3
condition 2 5 40.0
subroutine 10 12 83.3
pod 0 6 0.0
total 61 76 80.2


line stmt bran cond sub pod time code
1             package Panotools::Script::Line;
2              
3 15     15   102 use strict;
  15         29  
  15         420  
4 15     15   75 use warnings;
  15         56  
  15         430  
5              
6 15     15   9597 use Storable qw/ dclone /;
  15         49064  
  15         8636  
7              
8             =head1 NAME
9              
10             Panotools::Script::Line - Panorama Tools script data
11              
12             =head1 SYNOPSIS
13              
14             Base class for a line in a panotools script
15              
16             =head1 DESCRIPTION
17              
18             A line starts with a single letter identifier then a series of
19             namevalue items separated by whitespace
20              
21             =head1 USAGE
22              
23             my $line = new Panotools::Script::Line::Foo;
24              
25             =cut
26              
27             sub new
28             {
29 343     343 0 3871 my $class = shift;
30 343   33     921 $class = ref $class || $class;
31 343         548 my $self = bless {}, $class;
32 343         845 $self->_defaults;
33 343         740 return $self;
34             }
35              
36             =pod
37              
38             my $identifier = $line->Identifier;
39              
40             =cut
41              
42             sub Identifier
43             {
44 0     0 0 0 return '#';
45             }
46              
47             =pod
48              
49             $line->Parse ('f a1.0 b2.0 bar3.0');
50              
51             =cut
52              
53             sub Parse
54             {
55 281     281 0 7045 my $self = shift;
56 281   50     461 my $string = shift || return 0;
57 281         497 my $valid = $self->_valid;
58 281         2435 my @res = $string =~ / ([a-zA-Z][^ "]+)|([a-zA-Z][a-z]*"[^"]+")/g;
59 281         489 for my $token (grep { defined $_ } @res)
  4726         6038  
60             {
61 2363         9495 my ($key, $value) = $token =~ /$valid/;
62 2363 100       4085 next unless defined $key;
63 2322         3992 $self->{$key} = $value;
64             }
65 281         709 $self->_sanitise;
66 281         816 return 1;
67             }
68              
69             =pod
70              
71             my $string = $line->Assemble;
72              
73             =cut
74              
75             sub Assemble
76             {
77 113     113 0 5196 my $self = shift;
78 113         188 $self->_sanitise;
79 113         192 my @tokens;
80 113         135 for my $entry (sort keys %{$self})
  113         379  
81             {
82 764         1225 push @tokens, $entry . $self->{$entry};
83             }
84 113 50       332 return (join ' ', ($self->Identifier, @tokens)) ."\n" if (@tokens);
85 0         0 return '';
86             }
87              
88             =pod
89              
90             $line->Set (a => 'something', b => 2);
91              
92             =cut
93              
94             sub Set
95             {
96 9     9 0 21 my $self = shift;
97 9         41 my %hash = @_;
98 9         50 for my $entry (sort keys %hash)
99             {
100 59         84 $self->{$entry} = $hash{$entry};
101             }
102 9         39 $self->_sanitise;
103             }
104              
105             =pod
106              
107             Clone a line object
108              
109             $clone = $l->Clone;
110              
111             =cut
112              
113             sub Clone
114             {
115 82     82 0 98 my $self = shift;
116 82         2545 dclone ($self);
117             }
118              
119       19     sub _defaults {}
120              
121 0     0   0 sub _valid { return '^(.)(.*)' }
122              
123             sub _sanitise
124             {
125 416     416   522 my $self = shift;
126 416         730 my $valid = $self->_valid;
127 416         516 for my $key (keys %{$self})
  416         1419  
128             {
129 3873 100       12429 delete $self->{$key} unless (grep /$valid/, $key);
130             }
131             }
132              
133             1;
134