SAMPLE CODING STYLE:

 
Perl 5 Sample Coding/Documentation Style
  Sample of functions to perform dynamic method dispatch.
#==========================================================================
#
# $Id: core-db-private.lib,v 1.1 2002/01/03 21:51:55 brent Exp $
#
# NAME          : core-db-private.lib - Private functions used by core database layer
# AUTHOR        : Brent S.A. Cowgill
# RIGHTS
#
# Copyright (C) 1999-2001 interactivetools.com, All Rights Reserved
# http://www.interactivetools.com/ (used with permission)
#
# This programming library is the copyrighted property of
# interactivetools.com This library may only be used with the program it was
# originally distributed with. Any other uses are strictly prohibited
# without the written permission of interactivetools.com and all other
# rights are reserved.
#
# SYNOPSIS      : none of your business, it's a private library,
#                 figure it out from the code :-)
#
# DESCRIPTION
#
# This library implements the private functions that the DB layer calls
# in order to go about its business in an orderly fashion.
#
# See additional notes in core-db.lib
#
# CAVEAT CALLER:
#
# Note to users of this package. All methods in this library are subject to
# change at the whim of the core-db.lib maintainer.  See additional warnings
# in core-db.lib
#
#==========================================================================

#---------------------------------------------------------------------------
# Function     : DB_private_DispatchTable($rhTblDef, ...)
# Usage        : See SYNOPSIS
# Description  : Dispatch a database call based on where a table is located
#                and validate that the DB is connected
# Globals      : None
# Dependencies : None
# Parameters   : $rhTblDef see STRUCTURES
# Returns      :
# Exceptions   :
#---------------------------------------------------------------------------
sub DB_private_DispatchTable {
  my ($rhTblDef) = @_;

  # Get name of calling function and extract just the function w/o package prefix
  my $full_function = (caller(1))[3];
  my $function = $full_function;
  $function =~ /DB_(\w+)$/;
  $function = $1;

  # Check the first two parameters before we use them
  &DB_private_ValidateTableDef($rhTblDef, 'connected');

  # simulated dynamic dispatch because we can't use objects
  my @pkgs = ('DB_FlatFile');
  if (&DB_IsSQLDatabase($rhTblDef->{'where_located'})) {
    my $rhDB = &DB_private_GetDB($rhTblDef->{'where_located'});
    my $type = $rhDB->{'db_type'};
    $type =~ /^file$/ && die "$full_function\: no SQL database is configured but table '$rhTblDef->{'name'}' requires it\n";
       if ($type =~ /^mysql$/i)      { @pkgs = ('DB_MySQL');    }
    elsif ($type =~ /^oracle$/i)     { @pkgs = ('DB_Oracle');   }
    elsif ($type =~ /^postgresql$/i) { @pkgs = ('DB_PostgreSQL'); }
    elsif ($type =~ /^mssql$/i)      { @pkgs = ('DB_MSSQL');    }
    else  { die("$full_function\: invalid database type '$type'\n");  }
    push(@pkgs, 'DB_SQL'); # if custom don't have it, try SQL sub package
  } # else
  no strict 'refs';
  my $the_function = &DB_private_Dispatch_WhichFunc($function, @pkgs);
  $the_function->(@_);
} # DB_private_DispatchTable()

#---------------------------------------------------------------------------
# Function     : DB_private_DispatchWhichFunc($function, @pkgs)
# Usage        : See SYNOPSIS
# Description  : Given a function name and a list of packages to check,
#                locates the first function that exists. This is a method
#                of dynamic dispatch used because the compressor can't handle
#                objects.
# Globals      : None
# Dependencies : None
# Parameters   :
#   $function    Base name of the function which is being called
#   @pkgs      ' package' name - actuall a prefix which is added to $function
#                to lookup the function to call.
# Returns      :
# Exceptions   :
#---------------------------------------------------------------------------
sub DB_private_Dispatch_WhichFunc
{
  my ($function, @pkgs) = @_;
  # Invoke the custom DB layer version of the function if it has been
  # defined, otherwise, we use the DB__Function private implementation
  my $pkg;
  my $pk = "main";
  foreach $pkg (@pkgs)
  {
     my $the_function = "${pkg}_$function";
     if ($pk->can($the_function)) {
#        &DB_private_Trace("invoke: $the_function");
        return $the_function;
     }
  } # foreach
  # If no function found, call our own private implementation
  $function = "DB_private_default_$function";
#  &DB_private_Trace("invoke: $function");

  return $function;
} # DB_private_Dispatch_WhichFunc()
              
HOME | RÉSUMÉ | WORK EXPERIENCE | PORTFOLIO | DOCUMENTATION & WRITING | TOOLS OF THE TRADE | CONTACT ME