CSS::Parser - parser for CSS-style syntax


CSS::Parser - parser for CSS-style syntax


 use CSS::Parser;
 my $css = CSS::Parser->new(
                           handlers => {
                                        css_comment      => \&css_com,
                                        selector_string  => \&sel,
                                        block_start      => \&blk_s,
                                        property         => \&prop,
                                        value            => \&val,
                                        block_end        => \&blk_e,
                                        at_rule          => \&atr,
                                        at_symbol        => \&ats,
                                        error            => \&error
 sub css_com {
    my $self = shift;
    my $comment = shift;
    print "css comment:\n\t$comment\n";


The CSS::Parser deals simply with fairly loose CSS syntax. It doesn't know anything about what it is parsing, for that see other classes. This allows it to be the base for CSS::CSSn, CSS::STTSn and any other parser for a css-compatible syntax.

Chances are, you will want to use one of the existing subclasses or to subclass it yourself.

The interface to CSS::Parser is:

my $css = CSS::Parser->new([style => $parse_style],[handlers => $hashref_of_handlers]);
The constructor takes a variety of options provided in a hash.

style defines the style of parsing that you want from the parser. As of now the only style is 'callback' (default), however this may evolve in the future.

handlers specifies a hash of callbacks to be called when a given token has been met. Callbacks are CODEREFS. The callback sub will receive the parser object as it's first argument, and optionally the token when it makes sense (eg: selector_string will be passed the selector, but block_start won't receive the '{').

One may use 'default' to activate them all. Note that you can set a callback to the empty string (not undef) if you wish to set a default for all but to deactivate that specific callback.

$css->parse( $string_ref );
Tells the parser to go ahead and parse the provided reference to a string. Note that the string will be empty after the parser has finished, unless the stylesheet contains an error. It returns 1 upon success and undef upon failure.


the XML::Parser manpage, the HTML::Parser manpage, the HTML::TreeBuilder manpage


Copyright 1998-1999 Robin Berjon (robin@knowscape.com) All rights reserved.

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

 CSS::Parser - parser for CSS-style syntax