Debian Codegraph @

Debconf/Template/Transient.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::Template::Transient - Transient template object
6
7 =cut
8
9 package Debconf::Template::Transient;
10 use warnings;
11 use strict;
12 use base 'Debconf::Template';
13 use fields qw(_fields);
14
15 =head1 DESCRIPTION
16
17 This class provides Template objects that are not backed by a persistent
18 database store. It is useful for situations where transient operations
19 needs to be performed on templates. Note that unlike regular templates,
20 multiple transient templates may exist with the same name.
21
22 =cut
23
24 =head1 CLASS METHODS
25
26 =item new(template)
27
28 The name of the template to create must be passed to this function.
29
30 =cut
31
32 sub new {
33 my $this=shift;
34 my $template=shift;
35
36 unless (ref $this) {
37 $this = fields::new($this);
38 }
39 $this->{template}=$template;
40 $this->{_fields}={};
41 return $this;
42 }
43
44 =head2 get
45
46 This method is not supported by this function. Multiple transient templates
47 with the same name can exist.
48
49 =cut
50
51 sub get {
52 die "get not supported on transient templates";
53 }
54
55 =head2 fields
56
57 Returns a list of all fields that are present in the object.
58
59 =cut
60
61 sub fields {
62 my $this=shift;
63
64 return keys %{$this->{_fields}};
65 }
66
67 =head2 clearall
68
69 Clears all the fields of the object.
70
71 =cut
72
73 sub clearall {
74 my $this=shift;
75
76 foreach my $field (keys %{$this->{_fields}}) {
77 delete $this->{_fields}->{$field};
78 }
79 }
80
81 =head2 AUTOLOAD
82
83 Creates and calls accessor methods to handle fields.
84 This supports internationalization.
85
86 =cut
87
88 {
89 my @langs=Debconf::Template::_getlangs();
90
91 sub AUTOLOAD {
92 (my $field = our $AUTOLOAD) =~ s/.*://;
93 no strict 'refs';
94 *$AUTOLOAD = sub {
95 my $this=shift;
96
97 return $this->{_fields}->{$field}=shift if @_;
98
99 # Check to see if i18n should be used.
100 if ($Debconf::Template::i18n && @langs) {
101 foreach my $lang (@langs) {
102 # Lower-case language name because
103 # fields are stored in lower case.
104 return $this->{_fields}->{$field.'-'.lc($lang)}
105 if exists $this->{_fields}->{$field.'-'.lc($lang)};
106 }
107 }
108 return $this->{_fields}->{$field};
109 };
110 goto &$AUTOLOAD;
111 }
112 }
113
114 =head1 AUTHOR
115
116 Joey Hess <[email protected]>
117
118 =cut
119
120 1