parsed.org

Tips by tag: functions

Autoloading and Reloading Functions by cygnus on Jan 19, 2005 04:09 PM

To have zsh autoload functions, you can place them in a directory and have zsh search the path when the functions are called. For example, consider a function foo defined in ~/functions/foo. In your ~/.zshrc add:

FPATH=~/functions:$FPATH
autoload foo

Then, whenever you call foo, zsh will attempt to read ~/functions/foo for the function definition. To reload a function after you have changed its source file, run:

unfunction foo
autoload foo

The next invocation of the function will cause a reload of ~/functions/foo.

autoloadfunctionspathreloadzsh

Create a custom type which describes the structure of the rows returned:

CREATE TYPE func_return_type AS (column_name column_type, ...);

Then create the function:

CREATE OR REPLACE FUNCTION some_func (param_type, ...)
RETURNS SETOF func_return_type AS '
 SELECT columns FROM table WHERE condition;
' LANGUAGE 'SQL';
customfunctionsplpgsqlpostgresqlsqltypes
Returning Multiple Records by cygnus on Mar 22, 2005 09:10 AM

You can use RETURNS SETOF record in your plpgsql functions to return a set of rows. Here is an example of using pgsql to build a query using a table name parameter:

CREATE OR REPLACE FUNCTION test(text) RETURNS SETOF record AS '
DECLARE
  _table ALIAS FOR $1;
  _mycursor refcursor;
  _row record;
BEGIN
  OPEN _mycursor FOR EXECUTE ''SELECT * FROM '' || _table;
  FETCH _mycursor INTO _row;
  WHILE FOUND LOOP
    RETURN NEXT _row;
    FETCH _mycursor INTO _row;
  END LOOP;
  RETURN;
END
' LANGUAGE plpgsql;

Here is an example of calling such a function (the names and types of the result columns are required when returning SETOF record):

mydb=> SELECT * FROM test('mytable') AS (col1 integer, col2 text, col3 date);
functionsobscureplpgsqlpostgresqlpsqlsetofsql
Strip Non-printable Characters by xinu on Jul 03, 2005 12:44 AM

You can use PHP's trim function to remove non-printable characters:

$string = trim($string, "\x7f..\xff\x0..\x1f");
charactersfunctionslanguagesnon-printablephpprogrammingtrim
Turning Off PHP Caching by cygnus on Dec 31, 2005 04:07 PM

When using sessions in PHP, the default PHP configuration causes PHP to send a no-cache header to your browser so that session-managed pages are not cached. Sometimes this is not the desired behavior, such as when writing a script whose output is binary data such as images which are stored in a database. If this is the case, you can turn off the caching by using functions to modify the PHP configuration on a per-request basis. Call these functions before calling the session_start() function:

// Let the browser and proxies cache output
session_cache_limiter('public');

// One-day (60 * 24, in minutes) cache expiration time for output
session_cache_expire(60 * 24);

(Note: this behavior is documented at http://www.php.net/manual/en/ref.session.php.)

functionsheadershttplanguagesphpprogrammingsession
RSS