SYNOPSIS

    # In your .css file
    H1 { color: blue }
    H2 { color: red; font-family: Arial }
    .this, .that { color: yellow }

    # In your program
    use CSS::Tiny;

    # Create a CSS stylesheet
    my $CSS = CSS::Tiny->new();

    # Open a CSS stylesheet
    $CSS = CSS::Tiny->read( 'style.css' );

    # Reading properties
    my $header_color = $CSS->{H1}->{color};
    my $header2_hashref = $CSS->{H2};
    my $this_color = $CSS->{'.this'}->{color};
    my $that_color = $CSS->{'.that'}->{color};

    # Changing styles and properties
    $CSS->{'.newstyle'} = { color => '#FFFFFF' }; # Add a style
    $CSS->{H1}->{color} = 'black';                # Change a property
    delete $CSS->{H2};                            # Delete a style

    # Save a CSS stylesheet
    $CSS->write( 'style.css' );

    # Get the CSS as a <style>...</style> tag
    $CSS->html;

DESCRIPTION

\*(C`CSS::Tiny\*(C' is a perl class to read and write .css stylesheets with as little code as possible, reducing load time and memory overhead. \s-1CSS\s0.pm requires about 2.6 meg or ram to load, which is a large amount of overhead if you only want to do trivial things. Memory usage is normally scoffed at in Perl, but in my opinion should be at least kept in mind.

This module is primarily for reading and writing simple files, and anything we write shouldn't need to have documentation/comments. If you need something with more power, move up to \s-1CSS\s0.pm. With the increasing complexity of \s-1CSS\s0, this is becoming more common, but many situations can still live with simple \s-1CSS\s0 files.

\s-1CSS\s0 Feature Support

\*(C`CSS::Tiny\*(C' supports grouped styles of the form \*(C`this, that { color: blue }\*(C' correctly when reading, ungrouping them into the hash structure. However, it will not restore the grouping should you write the file back out. In this case, an entry in the original file of the form

H1, H2 { color: blue }

would become

H1 { color: blue } H2 { color: blue }

\*(C`CSS::Tiny\*(C' handles nested styles of the form \*(C`P EM { color: red }\*(C' in reads and writes correctly, making the property available in the form

$CSS->{'P EM'}->{color}

\*(C`CSS::Tiny\*(C' ignores comments of the form \*(C`/* comment */\*(C' on read correctly, however these comments will not be written back out to the file.

CSS FILE SYNTAX

Files are written in a relatively human-orientated form, as follows:

H1 { color: blue; } .this { color: red; font-size: 10px; } P EM { color: yellow; }

When reading and writing, all property descriptors, for example \*(C`color\*(C' and \*(C`font-size\*(C' in the example above, are converted to lower case. As an example, take the following \s-1CSS\s0.

P { Font-Family: Verdana; }

To get the value 'Verdana' from the object $CSS, you should reference the key \*(C`$CSS->{P}->{font-family}\*(C'.

METHODS

new

The constructor \*(C`new\*(C' creates and returns an empty \*(C`CSS::Tiny\*(C' object. The \*(C`read\*(C' constructor reads a \s-1CSS\s0 stylesheet, and returns a new \*(C`CSS::Tiny\*(C' object containing the properties in the file.

Returns the object on success, or \*(C`undef\*(C' on error. The \*(C`read_string\*(C' constructor reads a \s-1CSS\s0 stylesheet from a string.

Returns the object on success, or \*(C`undef\*(C' on error.

clone

The \*(C`clone\*(C' method creates an identical copy of an existing \*(C`CSS::Tiny\*(C' object.

write_string

Generates the stylesheet for the object and returns it as a string.

write

The \*(C`write $filename\*(C' generates the stylesheet for the properties, and writes it to disk. Returns true on success. Returns \*(C`undef\*(C' on error.

html

The \*(C`html\*(C' method generates the \s-1CSS\s0, but wrapped in a \*(C`style\*(C' \s-1HTML\s0 tag, so that it can be dropped directly onto a \s-1HTML\s0 page.

xhtml

The \*(C`html\*(C' method generates the \s-1CSS\s0, but wrapped in a \*(C`style\*(C' \s-1XHTML\s0 tag, so that it can be dropped directly onto an \s-1XHTML\s0 page.

errstr

When an error occurs, you can retrieve the error message either from the $CSS::Tiny::errstr variable, or using the \*(C`errstr\*(C' method.

CAVEATS

\s-1CSS\s0 Rule Order

While the order of rules in \s-1CSS\s0 is important, this is one of the features that is sacrificed to keep things small and dependency-free. If you need to preserve order yourself, we recommend that you upgrade to the more powerful \s-1CSS\s0 module.

If this is not possible in your case, alternatively it can be done with the help of another module such as Tie::IxHash:

my $css = CSS::Tiny->new; tie %$css, 'Tie::IxHash'; $css->read('style.css');

Note: You will also need to remember to add the additional dependency to your code or module in this case.

SUPPORT

Bugs should be reported via the \s-1CPAN\s0 bug tracker at

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CSS-Tiny>

For other issues, or commercial enhancement or support, contact the author.

AUTHOR

Adam Kennedy <[email protected]>

RELATED TO CSS::Tiny…

\s-1CSS\s0, <http://www.w3.org/TR/REC-CSS1>, Config::Tiny, <http://ali.as/>

COPYRIGHT

Copyright 2002 - 2010 Adam Kennedy.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.