Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In one of my subs, I need to create a new CSS element, such as:
.p {text-align: left;}
The problem is with the element name (the '.p' in the example). Because I have many predefined CSS elements, I need to make sure that my element name is original and not already in use. The sub contains the predefined CSS in a heredoc.

Also the new element name correlates with a value passed into the sub. So if someone says:

some_sub(foo, bar);
Then in some_sub, I need to create distinct CSS elements that somehow correlate to 'foo' and 'bar' respectively. (But I can't name them '.foo' and '.bar')

How best to do this?

Replies are listed 'Best First'.
Re: Dynamically creating CSS elements
by GrandFather (Saint) on Oct 18, 2005 at 22:45 UTC

    One thing you could do is generate a hash of the names in use keyed by name and with a usage count as the value. Then form the next css name as ".$name" . $names{$name} + 1. Remembering of course to ++$names{$name} afterwards.


    Perl is Huffman encoded by design.
Re: Dynamically creating CSS elements
by Cody Pendant (Prior) on Oct 19, 2005 at 03:58 UTC
    I can't help but think that there's something wrong with the way you're doing CSS altogether -- want to explain some more? -- but surely the simplest way would be to start with a $current_class_name of "a" and increment it every time you need a new one. You can't run out of class names because when you get to "z" you increment to "aa" and so on.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print