At the very least, this is worth reading up on: Less CSS.
The less refers to factoring out common logic.
I'm a big fan of LessCSS. I used to do some of the same kind of stuff in Template Toolkit, but Less is much nicer for dealing with CSS.
There's also a Perl based parser for .less files on CPAN (though I haven't tried it)
I try to write all code in Perl, but find I end up writing in bash, CSS, HTML, JS, and SQL, and doing database design, just to get anything done...