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}+ | \p{WSpace}+ $}{}xg if defined($value);
49 SL::Util - Assorted utility functions
53 Most important things first:
55 DO NOT USE C<@EXPORT> HERE! Only C<@EXPORT_OK> is allowed!
61 =item C<_hashify $num, @args>
63 Hashifies the very last argument. Returns a list consisting of two
66 The first part are the first C<$num> elements of C<@args>.
68 The second part depends on the remaining arguments. If exactly one
69 argument remains and is a hash reference then its dereferenced
70 elements will be used. Otherwise the remaining elements of C<@args>
71 will be returned as-is.
73 Useful if you want to write code that can be called from Perl code and
74 Template code both. Example:
76 use SL::Util qw(_hashify);
79 my ($self, %params) = _hashify(1, @_);
80 # Now do stuff, obviously!
83 =item C<camilify $string>
85 Returns C<$string> converted from underscore-style to
86 camel-case-style, e.g. for the string C<stupid_example_dude> it will
87 return C<StupidExampleDude>.
89 L</snakify> does the reverse.
91 =item C<snakify $string>
93 Returns C<$string> converted from camel-case-style to
94 underscore-style, e.g. for the string C<EvenWorseExample> it will
95 return C<even_worse_example>.
97 L</camilify> does the reverse.
101 Removes all leading and trailing whitespaces from C<$string> and
102 returns it. Whitespaces within the string won't be changed.
104 This function considers everything matching the Unicode character
105 property "Whitespace" (C<WSpace>) to be a whitespace.
115 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>