1 # <a name="Package <code>TWiki::Attrs="></a> Package =TWiki::Attrs
3 Class of attribute sets, designed for parsing and storing attribute values from a TWiki tag e.g. `%TAG{"joe" fred="bad" joe="mad"}%`
5 An attribute set is a hash containing an entry for each parameter. The default parameter (unnamed quoted string) is named `_DEFAULT` in the hash.
7 Attributes declared later in the string will override those of the same name defined earlier. The one exception to this is the \_DEFAULT key, where the _first_ instance is always taken.
9 As well as the default TWiki syntax (parameter values double-quoted) this class also parses single-quoted values, unquoted spaceless values, spaces around the =, and commas as well as spaces separating values. The extended syntax has to be enabled by passing the `$friendly` parameter to `new`.
13 <li><a href="#Package =TWiki::Attrs="> Package TWiki::Attrs</a><ul>
14 <li><a href="#ClassMethod <strong>new</strong> ($string,$frie"> ClassMethod new <tt>($string,$friendly)=>\%attrsObjectRef</tt></a></li>
15 <li><a href="#ObjectMethod <strong>isEmpty</strong> () -> boo"> ObjectMethod isEmpty <tt>() -> boolean</tt></a></li>
16 <li><a href="#ObjectMethod <strong>remove</strong> ($key) ->"> ObjectMethod remove <tt>($key) -> $value</tt></a></li>
17 <li><a href="#ObjectMethod <strong>stringify</strong> () -> $"> ObjectMethod stringify <tt>() -> $string</tt></a></li>
23 ## <a name="ClassMethod <strong>new</strong> ($string,$frie"></a> [[ClassMethod]] **new** `($string,$friendly)=>\%attrsObjectRef`
25 - `$string` - String containing attribute specification
26 - `$friendly` - if true, the parse will be according to the extended syntax pioneered by the original Contrib::Attrs. Otherwise it will be strict as per traditional TWiki syntax.
28 Parse a standard attribute string containing name=value pairs and create a new attributes object. The value may be a word or a quoted string. If there is an error during parsing, the parse will complete but $attrs->\{\_ERROR\} will be set in the new object. $attrs->\{\_RAW\} will always contain the full unprocessed $string.
30 Extended syntax example:
32 my $attrs = new TWiki::Attrs('the="time \\"has come", "the walrus" said to=speak of=\'many \\'things\', 1);
36 - `the` will be `time "has come`
37 - `__default__` will be `the walrus`
39 - `to` will be `speak`
40 - `of` will be `many 'things`
42 Only " and ' are escaped.
44 Traditional syntax is as old TWiki, except that the whole string is parsed (the old parser would only recognise default values in position 1, nowhere else)
46 ## <a name="ObjectMethod <strong>isEmpty</strong> () - bool"></a> [[ObjectMethod]] **isEmpty** `() -> boolean`
48 Return false if attribute set is not empty.
50 ## <a name="ObjectMethod <strong>remove</strong> ($key) - $"></a> [[ObjectMethod]] **remove** `($key) -> $value`
52 - `$key` - Attribute to remove
54 Remove an attr value from the map, return old value. After a call to `remove` the attribute is no longer defined.
56 ## <a name="ObjectMethod <strong>stringify</strong> () - $s"></a> [[ObjectMethod]] **stringify** `() -> $string`
58 Generate a printed form for the map, using strict attribute syntax, with only the single-quote extension syntax observed (no \{\} brackets, though).