Apache::Wyrd::Services::SAK - Swiss Army Knife of common subs


        use Apache::Wyrd::Services::SAK qw(:hashes spit_file);


"Swiss Army Knife" of functions used in Apache::Wyrd. These are mostly internal to the base classes of Wyrds, and are probably better implemented elsewhere in CPAN, but reducing the number of external modules was a goal of the Apache::Wyrd project.

(format: (returns) name (arguments)) for regular functions.

(format: (returns) $wyrd->name (arguments)) for methods


Functions for working with databases. Designed for use with a combination of Apache::Wyrd::Interfaces::Setter and the DBI-compatible database stored in Apache::Wyrd::DBL.

(scalarref) $wyrd->cgi_query(scalar)

For turning strings with conditional variables into queries parseable by the SQL interpreter. First sets all conditional variables in the query that are known, then set all unknown variables to NULL. The query is then executes and the DBI handle to the query is returned.

     $sh = $wyrd->cgi_query(
       'select names from people where name=$:name'

        $wyrd->cgi_query('delete from people where id=$:id');
(scalarref) $wyrd->do_query(scalar, [hashref])

Shorthand for creating and executing a DBI statement handle, returning the handle. If the optional hashref is supplied, it will perform a substitution in the manner of Apache::Wyrd::Interfaces::Setter. Unknown variables will be made NULL for the query. The query is then executes and the DBI handle to the query is returned.

    $sh = $wyrd->do_query(
      'select names from people where name=$:name', {name => $name}

    $wyrd->do_query('delete from people');
(scalar) _exists_in_table (hashref)

Determines if there exists in a table an entry with a given value in a given column. Accepts a hashref with the keys "table", "column", and "value". These should all be scalar string values. Returns the number of matching cases.

(scalar) set_clause(array)

Shorthand for setting up a query to be settable per Apache::Wyrd::Interfaces::Setter when given an array of column names.

FILES (:file)

Old-style file routines and file-related methods.

(scalarref) file_attribute(scalar, scalar, scalar)

Convert and check a file attribute based on tests. The tests are 'r' for read, 'w' for write, 'f' for exists (file) and 'd' for exists (directory), similar to the builtin -<foo> tests of the same name. If the file does not exist, but the test is w and not f or d, this method will check if the item is in a writeable directory.

If the file path under the attribute is not absolute, the relative path will be calculated first from the current location (of the file in which the wyrd is located) or from the document root, in that order.

The method returns undef on failure and the resolved path on success, leaving the attribute intact.

(scalarref) slurp_file(scalar)

get whole contents of a file. The only argument is the whole path and filename. A scalarref to the contents of the file is returned.

(scalar) spit_file(scalar, scalar)

Opposite of slurp_file. The second argument is the contents of the file. A positive response means the file was successfully written.

HASHES (:hash)

Helpful routines for handling hashes.

(scalar) array_4_get (array)

create the query portion of a URL as a get request out of the current CGI environment values for those elements. When multiple values of an element exist, they are appended.

(scalar) data_clean(scalar)

Shorthand for turning a string into "all lower case with underlines for whitespace".

(scalar) env_4_get([array/hashref])

attempt to re-create the current CGI environment as the query portion of a GET request. Either a hash or an array of variables to ignore can be supplied.

(hashref) data_clean(hashref)

Shorthand for turning a hashref into a lower-case version of itself. Will randomly destroy one value of any key for which multiple keys of different case are given.

(scalar, scalar) sort_by_ikey(a_hashref, b_hashref, array of keys)

Sort hashes by key. To be used in conjunction with the sort function:

    sort {sort_by_ikey($a, $b, 'lastname', 'firstname')} @array
(scalar, scalar) sort_by_key(a_hashref, b_hashref, array of keys)

Case-insensitive version of sort_by_ikey

    sort {sort_by_key($a, $b, 'lastname', 'firstname')} @array
(hashref) token_hash(scalar, [scalar])

Shorthand for performing token_hash on a string and returning a hash with positive values for every token. Useful for making a hash that can be easily used to check against the existence of a token in a string.

(array) token_parse(scalar, [regexp])

given a string made up of tokens it will split the tokens into an array of these tokens separated. It defaults to separating by commas, or if there are no commas, by whitespace. The optional regexp overrides the normal behavior.

        token_parse('each peach, pear, plum')


        (q/each peach/, q/pear/, q/plum/)


        token_parse('every good boy does fine')


        qw(every good boy does fine)
(array of hashrefs) uniquify_by_ikey(scalar, array of hashrefs)

given a key and an array of hashrefs, returns an array in the same order, dropping any hashrefs with duplicate values in the given key. Items are evaluated in a case-insensitive manner.

(array of hashrefs) uniquify_by_key(scalar, array of hashrefs)

case sensitive version of uniquify_by_ikey.

(array of hashrefs) uri_escape(scalar, array of hashrefs)

Quick and dirty shorthand for encoding a get request within a get request.

(scalar) normalize_href(objectref DBL, scalar href)

Given a href-style URL, returns the full URL that is implied from the fragment.

MAIL (:mail)

Quick and dirty interfaces to sendmail

(null) send_mail (hashref)

Send an email. Assumes that the apache process is a trusted user (see sendmail documentation). The hash should have the following keys: to, from, subject, and body. Unless sendmail is in /usr/sbin, the path key should also be set.

Strings (:string)

String manipulations.

(scalar) commify (array)

Add commas to numbers, thanks to the perlfaq.

(scalar) strip_html(scalar)

Escape out entities and strip tags from a given string.

(scalar) utf8_force(scalar)

Attempt to decode the text into UTF-8 by trying different common encodings until one returns valid UTF-8.

(scalar) utf8_to_entities(scalar)

Seek through the given text for Unicode byte sequences and replace them with numbered entities for that unicode character. Assumes the text is properly- formatted UTF8.

TAGS (:tag)

Tag-generation tools.

(scalar) attopts_template (array)

Creates a template of attribute options, given an array of the attributes.


Barry King <>



General-purpose HTML-embeddable perl object


Copyright 2002-2007 Wyrdwright, Inc. and licensed under the GNU GPL.

See LICENSE under the documentation for Apache::Wyrd.