1CSS::Tiny(3) User Contributed Perl Documentation CSS::Tiny(3)
2
3
4
6 CSS::Tiny - Read/Write .css files with as little code as possible
7
9 # In your .css file
10 H1 { color: blue }
11 H2 { color: red; font-family: Arial }
12 .this, .that { color: yellow }
13
14 # In your program
15 use CSS::Tiny;
16
17 # Create a CSS stylesheet
18 my $CSS = CSS::Tiny->new();
19
20 # Open a CSS stylesheet
21 $CSS = CSS::Tiny->read( 'style.css' );
22
23 # Reading properties
24 my $header_color = $CSS->{H1}->{color};
25 my $header2_hashref = $CSS->{H2};
26 my $this_color = $CSS->{'.this'}->{color};
27 my $that_color = $CSS->{'.that'}->{color};
28
29 # Changing styles and properties
30 $CSS->{'.newstyle'} = { color => '#FFFFFF' }; # Add a style
31 $CSS->{H1}->{color} = 'black'; # Change a property
32 delete $CSS->{H2}; # Delete a style
33
34 # Save a CSS stylesheet
35 $CSS->write( 'style.css' );
36
37 # Get the CSS as a <style>...</style> tag
38 $CSS->html;
39
41 "CSS::Tiny" is a perl class to read and write .css stylesheets with as
42 little code as possible, reducing load time and memory overhead. CSS.pm
43 requires about 2.6 meg or ram to load, which is a large amount of
44 overhead if you only want to do trivial things. Memory usage is
45 normally scoffed at in Perl, but in my opinion should be at least kept
46 in mind.
47
48 This module is primarily for reading and writing simple files, and
49 anything we write shouldn't need to have documentation/comments. If you
50 need something with more power, move up to CSS.pm. With the increasing
51 complexity of CSS, this is becoming more common, but many situations
52 can still live with simple CSS files.
53
54 CSS Feature Support
55 "CSS::Tiny" supports grouped styles of the form "this, that { color:
56 blue }" correctly when reading, ungrouping them into the hash
57 structure. However, it will not restore the grouping should you write
58 the file back out. In this case, an entry in the original file of the
59 form
60
61 H1, H2 { color: blue }
62
63 would become
64
65 H1 { color: blue }
66 H2 { color: blue }
67
68 "CSS::Tiny" handles nested styles of the form "P EM { color: red }" in
69 reads and writes correctly, making the property available in the form
70
71 $CSS->{'P EM'}->{color}
72
73 "CSS::Tiny" ignores comments of the form "/* comment */" on read
74 correctly, however these comments will not be written back out to the
75 file.
76
78 Files are written in a relatively human-orientated form, as follows:
79
80 H1 {
81 color: blue;
82 }
83 .this {
84 color: red;
85 font-size: 10px;
86 }
87 P EM {
88 color: yellow;
89 }
90
91 When reading and writing, all property descriptors, for example "color"
92 and "font-size" in the example above, are converted to lower case. As
93 an example, take the following CSS.
94
95 P {
96 Font-Family: Verdana;
97 }
98
99 To get the value 'Verdana' from the object $CSS, you should reference
100 the key "$CSS->{P}->{font-family}".
101
103 new
104 The constructor "new" creates and returns an empty "CSS::Tiny" object.
105
106 read $filename
107 The "read" constructor reads a CSS stylesheet, and returns a new
108 "CSS::Tiny" object containing the properties in the file.
109
110 Returns the object on success, or "undef" on error.
111
112 read_string $string
113 The "read_string" constructor reads a CSS stylesheet from a string.
114
115 Returns the object on success, or "undef" on error.
116
117 clone
118 The "clone" method creates an identical copy of an existing "CSS::Tiny"
119 object.
120
121 write_string
122 Generates the stylesheet for the object and returns it as a string.
123
124 write
125 The "write $filename" generates the stylesheet for the properties, and
126 writes it to disk. Returns true on success. Returns "undef" on error.
127
128 html
129 The "html" method generates the CSS, but wrapped in a "style" HTML tag,
130 so that it can be dropped directly onto a HTML page.
131
132 xhtml
133 The "html" method generates the CSS, but wrapped in a "style" XHTML
134 tag, so that it can be dropped directly onto an XHTML page.
135
136 errstr
137 When an error occurs, you can retrieve the error message either from
138 the $CSS::Tiny::errstr variable, or using the "errstr" method.
139
141 CSS Rule Order
142 While the order of rules in CSS is important, this is one of the
143 features that is sacrificed to keep things small and dependency-free.
144 If you need to preserve order yourself, we recommend that you upgrade
145 to the more powerful CSS module.
146
147 If this is not possible in your case, alternatively it can be done with
148 the help of another module such as Tie::IxHash:
149
150 my $css = CSS::Tiny->new;
151 tie %$css, 'Tie::IxHash';
152 $css->read('style.css');
153
154 Note: You will also need to remember to add the additional dependency
155 to your code or module in this case.
156
158 Bugs should be reported via the CPAN bug tracker at
159
160 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CSS-Tiny>
161
162 For other issues, or commercial enhancement or support, contact the
163 author.
164
166 Adam Kennedy <adamk@cpan.org>
167
169 CSS, <http://www.w3.org/TR/REC-CSS1>, Config::Tiny, <http://ali.as/>
170
172 Copyright 2002 - 2010 Adam Kennedy.
173
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 The full text of the license can be found in the LICENSE file included
178 with this module.
179
180
181
182perl v5.28.1 2016-03-10 CSS::Tiny(3)