NAME

Tie::Hash::Attribute - Print hash values as rotating HTML attributes.

SYNOPSIS

 use Tie::Hash::Attribute;

 tie my %tag, 'Tie::Hash::Attribute';
 %tag = (
     table => { border => 0 },
     tr => {
         style => { color => 'red', align => 'right' },
     },
     td => {
         style => {
             align => [qw( left right )],
             color => [qw( red blue green )],
     },
 };

 print $tag{-table};
   # border: 0

 print $tag{-tr};
   # style="align: right; color: red;"

 print $tag{-td} for 1 .. 4;
   # style="align: left; color: red;"',
   # style="align: right; color: blue;"',
   # style="align: left; color: green;"',
   # style="align: right; color: red;"',

 # or emit all attributes at once
 tie my %tr_tag, 'Tie::Hash::Attribute';
 %tr_tag = ( style => { color => [qw(red blue green)] } );
 print scalar %tr_tag;
   # style="align: right; color: red;"

DESCRIPTION

This module will translate the keys and values into HTML tag attributes. You just need to provide the tags.

Hash values can be scalars, arrays, hashes or hashes of hashes.

To emit values as an HTML attribute string, fetch the key with a dash prepended to it:

%hash = ( foo => 1, bar => 2, baz => 3);

print $hash{foo};     # returns original value
print $hash{-foo};    # returns value as HTML attribute string

Or access the entire hash as a scalar:

print scalar %hash;

You can use this to to aide in the creation of HTML tags:

print '<table>';
for my $row (@rows) {
    printf '<tr%s>', scalar %tr;
    for my $col (@$row) {
        printf '<td%s>%s</td>', scalar %td, $col;
    }
    print '</tr>';
}

The decision on which style to apply to a row is deferred to the tied hash. Just assign an array reference to the key and each value will be rotated.

%tr_tag = ( class => [qw( odd even )] );

BUGS AND LIMITATIONS

Assignment stops at the second nested key, which will use the third as its value:

$tag{foo}{1st}{2nd}{3rd} = '4th';
print $tag{-foo}, "\n";

# yields 1st="2nd: 3rd;"

This is an intended limitation. If there are other keys in the second nested hash, then the first key in alphabetical order wins.

There are other limitations that will be corrected over a short time:

  • Need to apply correct encoding to keys.

  • Need to detect when " is in a value.

Please report any bugs or feature requests to either

GITHUB

The Github project is https://github.com/jeffa/Tie-Hash-Attribute

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Tie::Hash::Attribute

You can also look for information at:

SEE ALSO

AUTHOR

Jeff Anderson, <jeffa at cpan.org>

LICENSE AND COPYRIGHT

Copyright 2015 Jeff Anderson.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.