CSS::Tiny(3pm)

πŸ’– CSS::Tiny β€” the manual page but make it a group chat πŸ’–

okay so this is from the User Contributed Perl Documentation corner of the man pages, and it's called CSS::Tiny, and bestie it is SO cute and small, you're gonna be obsessed. grab a boba, we're reading a whole entire manpage together πŸ“βœ¨


🌸 NAME 🌸

CSS::Tiny β€” read and write .css files with as little code as possible πŸ’…


✨ SYNOPSIS ✨

here's the vibe, copy-paste friendly, no cap:

# 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 πŸ¦‹

so CSS::Tiny is a perl class that reads and writes .css stylesheets with the absolute bare minimum of code, which means less load time and less memory overhead. and why do we care? because CSS.pm needs like 2.6 meg or ram just to load, which is a whole LOT of overhead if all you're doing is something trivial. now, memory usage in Perl is normally something people scoff at, but honestly? in my opinion it should at least be kept in mind. main character energy tbh 🫢

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 CSS.pm. with CSS getting more and more complex, that's becoming more common, but like, so many situations can still absolutely live with simple CSS files. periodt.

πŸ‘‘ CSS Feature Support πŸ‘‘

CSS::Tiny supports grouped styles of the form this, that { color: blue } correctly when reading, ungrouping them into the hash structure. however, it will not restore the grouping should you write the file back out. so an entry in the original file that looked like

H1, H2 { color: blue }

would become

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

(not a glow-down, just how it is πŸ’…)

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

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

CSS::Tiny ignores comments of the form /* comment */ on read correctly β€” icon behaviour ✨ β€” however these comments will not be written back out to the file. they simply ✨ vanish ✨.


🌈 CSS FILE SYNTAX 🌈

files are written in a relatively human-orientated form, like this:

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

when reading and writing, all property descriptors β€” for example color and font-size in the example above β€” are converted to lower case. so for example, take this CSS:

P {
    Font-Family: Verdana;
}

to get the value 'Verdana' from the object $CSS, you should reference the key $CSS->{P}->{font-family}. lowercase. always lowercase. she does not do caps πŸŽ€


πŸ’œ METHODS πŸ’œ

new

the constructor new creates and returns an empty CSS::Tiny object. blank slate, fresh start, no baggage πŸ’–

read $filename

the read constructor reads a CSS stylesheet, and returns a new CSS::Tiny object containing the properties in the file.

returns the object on success, or undef on error. (so like, check for undef, bestie, i'm begging 😭)

read_string $string

the read_string constructor reads a CSS stylesheet from a string.

returns the object on success, or undef on error.

clone

the clone method creates an identical copy of an existing CSS::Tiny object. twin behaviour. no cap.

write_string

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

write

the write $filename generates the stylesheet for the properties, and writes it to disk. returns true on success. returns undef on error.

html

the html method generates the CSS, but wrapped in a style HTML tag, so that it can be dropped directly onto a HTML page. chef's kiss, iconic, no notes πŸ”₯

xhtml

the html method generates the CSS, but wrapped in a style XHTML tag, so that it can be dropped directly onto an XHTML page. (yes, the docs really do say html right there in the xhtml entry β€” iykyk, i'm just relaying the lore πŸ™ˆ)

errstr

when an error occurs, you can retrieve the error message either from the $CSS::Tiny::errstr variable, or using the errstr method. two ways in, same tea πŸ«–


πŸ’€ CAVEATS πŸ’€

CSS Rule Order

okay so while the order of rules in CSS is important, this is one of the features that gets sacrificed to keep things small and dependency-free. if you need to preserve order yourself, we recommend you upgrade to the more powerful CSS module.

if that's 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. don't forget, bestie, the vibes depend on it 🫢

🧚 SUPPORT 🧚

bugs should be reported via the CPAN 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 <adamk@cpan.org>


πŸ’« SEE ALSO πŸ’«

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


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 LICENSE file included with this module.


and that's the whole page, bestie!! this was perl v5.36.0, dated 2022-10-13, and it's the CSS::Tiny(3pm) manual page β€” slay, ate, left no crumbs, go write some tiny css πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from