5 use parent qw(Exporter);
9 our @EXPORT_OK = qw(_hashify camelify snakify trim);
14 croak "Invalid number of entries to keep" if 0 > $keep;
16 return @_[0..scalar(@_) - 1] if $keep >= scalar(@_);
17 return ($keep ? @_[0..$keep - 1] : (),
18 ((1 + $keep) == scalar(@_)) && ((ref($_[$keep]) || '') eq 'HASH') ? %{ $_[$keep] } : @_[$keep..scalar(@_) - 1]);
23 $str =~ s/_+([[:lower:]])/uc($1)/ge;
29 $str =~ s/_([[:upper:]])/'_' . lc($1)/ge;
30 $str =~ s/(?<!^)([[:upper:]])/'_' . lc($1)/ge;
36 $value =~ s{^ \p{WSpace}+ }{}xg if defined($value);
37 $value =~ s{ \p{WSpace}+ $}{}xg if defined($value);
50 SL::Util - Assorted utility functions
54 Most important things first:
56 DO NOT USE C<@EXPORT> HERE! Only C<@EXPORT_OK> is allowed!
62 =item C<_hashify $num, @args>
64 Hashifies the very last argument. Returns a list consisting of two
67 The first part are the first C<$num> elements of C<@args>.
69 The second part depends on the remaining arguments. If exactly one
70 argument remains and is a hash reference then its dereferenced
71 elements will be used. Otherwise the remaining elements of C<@args>
72 will be returned as-is.
74 Useful if you want to write code that can be called from Perl code and
75 Template code both. Example:
77 use SL::Util qw(_hashify);
80 my ($self, %params) = _hashify(1, @_);
81 # Now do stuff, obviously!
84 =item C<camilify $string>
86 Returns C<$string> converted from underscore-style to
87 camel-case-style, e.g. for the string C<stupid_example_dude> it will
88 return C<StupidExampleDude>.
90 L</snakify> does the reverse.
92 =item C<snakify $string>
94 Returns C<$string> converted from camel-case-style to
95 underscore-style, e.g. for the string C<EvenWorseExample> it will
96 return C<even_worse_example>.
98 L</camilify> does the reverse.
100 =item C<trim $string>
102 Removes all leading and trailing whitespaces from C<$string> and
103 returns it. Whitespaces within the string won't be changed.
105 This function considers everything matching the Unicode character
106 property "Whitespace" (C<WSpace>) to be a whitespace.
116 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>