Baldrick::Util
From CunningPlans
Contents |
[edit] package Baldrick::Util
Baldrick::Util is not a class and no instances can be created. It is a package of miscellaneous functions that are used extensively by other packages.
[edit] Usage
use Baldrick::Util;
This must be done in every file where you wish to use the functions provided by Baldrick::Util; you must "use" it directly so that it can export symbols into the caller's namespace.
[edit] Interface
[edit] Debugging functions
None of the functions in this section should be called from a production application. They are for debugging only. See Baldrick::Response and Baldrick::Dogsbody::sendOutput() for the proper way to print things to the user's screen.
- webhead ( [ ctype => content-type ] )
- Print a Content-Type header, and the extra linefeed that ends an HTTP header block. This is called automatically by webprint(), webmoan(), and webdump(). Not for production use - use response->doHeaders() instead. This does not coordinate with the Response(), thus its usage may cause duplicate headers and failure to set cookies.
- webprint (text, [ no_line => 0|1] )
- Print text to the browser, followed with a BR tag unless no_line is nonzero. Not for production use - use dogsbody->sendOutput() or response->sendText() instead.
- webmoan (text)
- Like webprint except it is wrapped in div class="error" for better visibility (assuming you have an 'error' style defined, of course... mine is "font-weight:bold; color: #a00; font-size:110%;")
- webdump (object, [shallow => 0|1] )
- arg shallow: if true, show only the top level of the object, don't recurse into its children.
- print the contents of any object via Data::Dumper. Wrap it in a DIV that uses javascript to make it collapsible.
- deprecated ( [ message ] )
- example: deprecated("Don't call Dogsbody::setCookie(), use Baldrick::Response::setCookie() instead.")
- Put this inside any function you don't like anymore. It will print to STDERR (generally the httpd log file) a message that the function is deprecated, the module and line number of the caller, followed by the optional message given.
- dumpObject() - deprecated
[edit] Argument-Handling functions
- requireArg (hashref args, scalar key-name)
- my $db = requireArg(\%args, 'database')
- Most functions in Baldrick receive their parameters as a hash of named arguments. requireArg() is a convenience method for requiring and extracting such a named argument. If the argument named is undefined, a fatal error occurs. (Note that zero or null are acceptable values and will not cause a fatal error)
- requireArgs (hashref args, listref key-name-list)
- requireArgs(\%args, [ qw(name rank serialnumber) ])
- Cause a fatal error unless every argument named in the list is present. Does not return anything useful.
- requireAny (hashref args, listref key-name-list)
- my $what = requireAny(\%args, [ qw[file directory handle] ])
- print ("You gave me a $what whose value is $args{$what}");
- Require that at least one of the named arguments be present in \%args, otherwise a fatal error results. The first key (not value) found is returned; see usage of $args{$what} above. This is useful for a function that can be passed multiple object names or operation names; it is often followed by a construct like "if ($what eq '...') { doSomething($args{$what}) } elsif ($what eq '...') { } ..."
[edit] Hash-manipulating functions
- listToHash
- mergeHashTrees
[edit] Object and Package functions
- cloneObject
- loadClass
- createObject
[edit] Time and date
- easydate
- easytime
- easyfulltime
- replaceDateWords
- parseTimeSpan
[edit] Text formatting
- escapeURL
- escapeHTML
- wordsplit
