Re: plperl user function

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: KeithW(at)narrowpathinc(dot)com
Cc: PostgreSQL Interfaces <pgsql-interfaces(at)postgresql(dot)org>
Subject: Re: plperl user function
Date: 2005-04-21 19:52:32
Message-ID: 42680480.7080805@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces


>
> 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/

In response to

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Keith Worthington 2005-04-21 20:07:27 Re: plperl user function
Previous Message Keith Worthington 2005-04-21 19:46:08 plperl user function