pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joshua D. Drake <jd@commandprompt.com>
To: KeithW@narrowpathinc.com
Cc: PostgreSQL Interfaces <pgsql-interfaces@postgresql.org>
Subject: Re: plperl user function
Date: Thu, 21 Apr 2005 12:52:32 -0700
Message-ID: <42680480.7080805@commandprompt.com> (raw)
In-Reply-To: <20050421194438.M68704@narrowpathinc.com>
References: <20050421194438.M68704@narrowpathinc.com>


> 
> This leaves me with two questions.
> 1)  Why can I not use "use strict;" or "use warnings;" as they are apparently
> good perl programming practice.

Actually it is excellent practice so don't feel bad. The reason you 
can't use them is that in order to support them in plPerl you have to 
acces the module. THe module itself is a file on the filesystem.

You can not open files on the filesystem via a plperl function unless 
you install it as untrusted.

Try installing plperlU and then running your function.


   I say apparently because if you remember I
> started learning this language 3 days ago and must be considered a neophyte. :-)
> 2)  What is wrong with the use of RETURN?

Somebody else is going to have to help you with that one.

Sincerely,

Joshua D. Drake


> 
> Kind Regards,
> Keith
> 
> -- Function: func_extract_size(varchar)
> 
> -- DROP FUNCTION func_extract_size("varchar");
> 
> CREATE OR REPLACE FUNCTION func_extract_size("varchar")
>  RETURNS int AS
> $BODY$
> #  A function to extract the net size.
> #  One input argument.  description  Case insensitive.
>  
> #  Define a subroutine that accepts a dimension string (xx' xx") and returns
> #  feet and inch strings.
>   sub sub_parse_dimension{
> #     Initialize local variables.
>      my $v_feet_str = "";
>      my $v_inch_str = "";
>      my $v_dim_str = shift(@_);
> #     Split the dimension into feet and inch parts using pattern matching
> #     and parentheses to capture the desired parts.
>      $v_dim_str =~ /(?:([\d\.\s\-\/]+)')?\s*(?:([\d\.\s\-\/]+)")?/;
>      $v_feet_str = defined $1 ? $1 : 0;
>      $v_inch_str = defined $2 ? $2 : 0;
>      return ($v_feet_str, $v_inch_str);
>   }
>  
> #  Define a subroutine that accepts a mixed number string and returns
> #  a decimal number.
>   sub sub_xform_mixed_number{
> #     Initialize local variables.
>      my $v_decimal = 0;
>      my $v_dim_str = shift(@_);
> #     Check for fraction in dimension string.
>      if ($v_dim_str =~ /\//){
> #        There is a fraction to deal with.
> #        Parse the fraction using whitespace or a hyphen (-) and the forward
> #        slash (/) character.
>         $v_dim_str =~ /(?:([\d\.]+))?[\-\s]*(?:(\d+))?\/(?:(\d+))?/;
>         my $v_whole = defined $1 ? $1 : 0;
>         my $v_numer = defined $2 ? $2 : 0;
>         my $v_denom = defined $3 ? $3 : 0;
>         $v_decimal = $v_whole + $v_numer/$v_denom;
>      } else {
> #        There is no fraction present.  Set the output equal to the input.
>         $v_decimal = $v_dim_str;
>      }
>      return $v_decimal;
>   }
> 
> #  Begining of the program.
>   my $v_description = shift(@_);
>   my $v_border_id = "";
>   my $v_dim1_total = 0;
>   my $v_dim2_total = 0;
>   my $v_tag = "";
>  
> #  Perform a case insensitive check for the proper data format.  Capture the
> #  desired parts of the data using parentheses.
>   if (/.*border:\s*(.*)\s*size:\s*(.*)\s*tag:\s*(.*)\s*/i){
> #     Store the capture patterns in variables to avoid unpredictable results.
>      my ($v_border_str, $v_size_str, $v_tag_str) = ($1, $2, $3);
> #     Check for no border.
>      if ($v_border_str =~ /none/i){
>         $v_border_id = "";
>      } else {
>         $v_border_id = $v_border_str;
>      }
> #     Parse up the size string.
>      if ($v_size_str =~ /\d+\s*['"]\s*x\s*\d+\s*['"]/i){
> #        It looks like a size string so continue to process.
>         my $v_dim1_str = "";
>         my $v_dim2_str = "";
>         my $v_feet_str = "";
>         my $v_inch_str = "";
> #        Split the size string into its two parts.
>         ($v_dim1_str, $v_dim2_str) = split(/\s*x\s*/i, $v_size_str);
> #        Now split dimension one into feet and inch parts.
>         ($v_feet_str, $v_inch_str) = sub_parse_dimension($v_dim1_str);
> #        Merge the components of the dimension into a single value.
>         $v_dim1_total = ( ( 12 * sub_xform_mixed_number($v_feet_str) )
>                           + sub_xform_mixed_number($v_inch_str)
>                         );
> #        Now split dimension two into feet and inch parts.
>         ($v_feet_str, $v_inch_str) = sub_parse_dimension($v_dim2_str);
> #        Merge the components of the dimension into a single value.
>         $v_dim2_total = ( ( 12 * sub_xform_mixed_number($v_feet_str) )
>                           + sub_xform_mixed_number($v_inch_str)
>                         );
>      }
> #     Check for no tag.
>      if ($v_tag_str =~ /none/i){
>         $v_tag = "";
>      } else {
>         $v_tag = $v_tag_str;
>      }
>   } else {
>      $v_border_id = "";
>      $v_dim1_total = 0;
>      $v_dim2_total = 0;
>      $v_tag = "";
>   }
> 
>   RETURN $v_dim1_total;
> $BODY$
>  LANGUAGE 'plperl' STABLE STRICT;
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
>                http://archives.postgresql.org


-- 
Your PostgreSQL solutions company - Command Prompt, Inc. 1.800.492.2240
PostgreSQL Replication, Consulting, Custom Programming, 24x7 support
Managed Services, Shared and Dedication Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/



view thread (10+ messages)  latest in thread

Message-ID: <42680480.7080805@commandprompt.com>
Permalink:  ../42680480.7080805@commandprompt.com/
Also on:    postgresql.org/message-id/42680480.7080805@commandprompt.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jd@commandprompt.com, KeithW@narrowpathinc.com
  Subject: Re: plperl user function
  In-Reply-To: <42680480.7080805@commandprompt.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox