Hi!

I'm having some difficulty with the PHP and Perl translations when it comes
to the simple "typeof" feature found in most other languages.  Ok, it's
maybe something I dislike about both these languages but please don't bash
them.  :-)  They have no typeof feature.  They are both very much
"roll-your-own" philosophy about some things like this.  Perl much more so
than PHP, but PHP is a bit more accomodating with several is_type($var)
functions.  There's even a gettype, but its use is highly discouraged as the
strings returned are likely to change and it's slow.  (But I wonder about
the logic behind this, if everyone has to write their own typeof).

Anyways, how about the addition of a ws__typeof function, and if your
language has a typeof, then it's just a simple wrapper.  But also, to have a
more unified mechanism for possible input and output types...

Here's some Perl and PHP code I am working on.  Any thoughts of suggestions
on how to approach the problem and get a more unified interface across the
different languages?

Leif

========================
Perl
========================
# ws__typeof - used to estimate the data type
#
# Input Data Types (language specific):
#     SCALAR (number, string), ARRAY, HASH, or CODE
#
# Output Data Types (JavaScript / generic):
#     number, boolean, function, string, object, array, date, default
#
# Takes a reference as input, determines type of reference,
# if SCALAR, dereferences and determines format of data
#
sub ws__typeof
{
#    my $self = shift; # for OO, disregard for now
    my $ref = shift || carp "Not enough args (0 of 1) sent to ws__typeof:
$!";

    if ( $ref =~ /^SCALAR/ )
    {
        if ( $$ref =~ /^\d+$/ )
        {
            return( "number" );
        }
        elsif ( $$ref =~ m/\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/ )
        {
            return( "date" );
        }
        elsif ( $$ref =~ /^[\d\s\w]+([\d\s\w])*$/ ) # not binary string
friendly
        {
            return( "string" );
        }
        else
        {
            return( "unknown" );
        }
    }
    elsif ( $ref =~ /^ARRAY/ )
    {
        return( "array" );
    }
    elsif ( $ref =~ /^HASH/ )
    {
        return( "object" );
    }
    elsif ( $ref =~ /^CODE/ )
    {
        return( "code" );
    }
    else
    {
        return( "unknown" );
    }
}

========================
PHP
========================
/*
 * ws__typeof - used to estimate the data type
 *
 * Input Data Types (language specific):
 *     boolean, array, object, null
 *     scalar (numeric {integer, float}, string)
 *
 * Output Data Types (JavaScript / generic):
 *     number, boolean, function, string, object, array, date, default
 *
 * Takes a reference as input, determines type of reference,
 * if SCALAR, dereferences and determines format of data
 */

function ws__typeof ( $v )
{
    if ( is_bool( $v ) )
    {
        return "boolean";
    }
    else if ( is_scalar( $v ) )
    {
        if ( is_numeric( $v ) )
        {
            if ( is_integer( $v ) )
            {
                return "integer";
            }
            else if ( is_float( $v ) )
            {
                return "float";
            }
            else // imaginary?  ;-)
            {
                return "unknown number";
            }
        }
        else if ( is_string( $v ) )
        {
            if ( preg_match( "/\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/",
$v ) )
            {
                return "date";
            }
            else if ( preg_match( "/^\w+([\w\d])*$/", $v ) )
            {
                return "string";
            }
            else
            {
                return "unknown string";
            }
        }
        else
        {
            return "unknown scalar";
        }
    }
    else if ( is_array( $v ) )
    {
        return "array";
    }
    else if ( is_object( $v ) )
    {
        return "object";
    }
    else if ( is_null( $v ) )
    {
        return "null";
    }
    else
    {
        return "unknown";
    }
}




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
_______________________________________________
Dynapi-Dev mailing list
[EMAIL PROTECTED]
http://www.mail-archive.com/[EMAIL PROTECTED]/

Reply via email to