Ron Savage > HTML-Parser-Simple > HTML::Parser::Simple::Attributes

Download:
HTML-Parser-Simple-2.01.tgz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 2.01   Source  

NAME ^

HTML::Parser::Simple::Attributes - A simple HTML attribute parser

Synopsis ^

        #!/usr/bin/env perl

        use strict;
        use warnings;

        use HTML::Parser::Simple::Attributes;

        # -------------------------

        # Method 1:

        my($parser) = HTML::Parser::Simple::Attributes -> new(' height="20" width=20 ');

        # Get all the attributes as a hashref.
        # This triggers a call to parse(), if necessary.

        my($attr_href) = $parser -> get;

        # Get the value of a specific attribute.
        # This triggers a call to parse(), if necessary.

        my($height) = $parser -> get('height');

        # Method 2:

        my($parser) = HTML::Parser::Simple::Attributes -> new;

        $parser -> parse(' height="20" width=20 ');

        # Get all attributes, or 1, as above.

        my($attr_href) = $parser -> get;
        my($height)    = $parser -> get('height');

        # Get the attribute string passed to new() or to parse().

        my($a_string) = $parser -> a_string;

        # Get the parsed attributes as a hashref, if parse() has been called.
        # If parse() has not been called, this returns {}.

        my($a_hashref) = $parser -> a_hashref;

Description ^

HTML::Parser::Simple::Attributes is a pure Perl module.

It parses HTML V 4 attribute strings, and turns them into a hashrefs.

Also, convenience methods "hashref2string($hashref)" and "string2hash($string)" are provided, which deal with Perl hashrefs formatted as strings.

See also HTML::Parser::Simple and HTML::Parser::Simple::Reporter.

Distributions ^

This module is available as a Unix-style distro (*.tgz).

See http://savage.net.au/Perl-modules.html for details.

See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing.

Constructor and initialization ^

new(...) returns an object of type HTML::Parser::Simple::Attributes.

This is the class contructor.

Usage: HTML::Parser::Simple::Attributes -> new.

This method takes a hash of options.

Call new() as new(option_1 => value_1, option_2 => value_2, ...).

Available options (each one of which is also a method):

o a_string => $a_HTML_attribute_string

This takes a string as formatted for HTML attribites.

E.g.: ' height="20" width=20 '.

Default: '' (the empty string).

Methods ^

a_hashref()

Returns a hashref of parsed attributes, if parse() has been called.

Returns {} if parse() has not been called.

a_string()

Returns the attribute string passed to new(), or to parse($attr_string).

Returns '' (the empty string) if parse() has not been called.

'a_string' is a parameter to "new()". See "Constructor and Initialization" for details.

get([$name])

Here, the [] indicate an optional parameter.

        my($hashref) = $parser -> get;
        my($value)   = $parser -> get('attr_name');

If you do not pass in an attribute name, this returns a hashref with the attribute names as keys and the attribute values as the values.

If you pass in an attribute name, it will return the value for just that attribute.

Returns undef if you supply the name of a non-existant attribute.

hashref2string($hashref)

Returns a string suitable for printing.

Warning: The hashref is formatted as we would normally do in Perl, i.e. with commas and fat commas.

        {height => 20, width => 20} is returned as 'height => 20, width => 20'

This is not how HTML attributes are written.

The output string can be parsed by "string2hashref($string)".

This is a convenience method.

new()

This is the constructor. See "Constructor and initialization" for details.

parse($attr_string)

        $attr_href = $parser -> parse($attr_string);

Or

        $parser    = HTML::Parser::Simple::Attributes -> new(a_string => $attr_string);
        $attr_href = $parser -> parse;

Parses a string of HTML attributes and returns the result as a hashref, or dies if the string is not a valid attribute string.

Attribute values may be quoted with double quotes or single quotes. Quotes may be omitted if there are no spaces in the value.

Returns an empty hashref if $attr_string was not supplied to new(), nor to parse().

string2hashref($string)

Returns a hashref by (simplistically) parsing the string.

        'height => 20, width => 20' is returned as {height => 20, width => 20}

Warning: This string must have been output by "hashref2string($hashref)", because it deals with a string of hashrefs as we normally think of them in Perl, i.e. with commas and fat commas.

This is not how HTML deals with a string of attributes.

This is a convenience method.

Author ^

HTML::Parser::Simple::Attributes was written by Mark Stosberg <mark@summersault.com> in 2009.

The code has be re-worked by Ron Savage.

Home page: http://mark.stosberg.com/.

Copyright ^

Copyright (c) 2009 Mark Stosberg.

        All Programs of mine are 'OSI Certified Open Source Software';
        you can redistribute them and/or modify them under the terms of
        The Artistic License, a copy of which is available at:
        http://www.opensource.org/licenses/index.html
syntax highlighting: