Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:30:37+00:00 2026-06-02T12:30:37+00:00

Could someone please assist to rectify the errors in this piece of code. (this

  • 0

Could someone please assist to rectify the errors in this piece of code.

(this is just a simplified version of my code, but it identifies the issues).

DROP FUNCTION perl_func(VARIADIC params character varying[]);

CREATE OR REPLACE FUNCTION perl_func(VARIADIC params character varying[])
  RETURNS character varying AS
$BODY$   
   $val = spi_query("array_to_string($1,'###');");
   $s = `echo $val`;
   return $s; 
$BODY$
  LANGUAGE plperlu VOLATILE
  COST 100;

SELECT * from perl_func('a','d');

This returns a syntax error:

ERROR:  syntax error at or near "," at line 2.
CONTEXT:  PL/Perl function “perl_func”

The main aim: Is to formulate the input params as a string, and use it to call some command-line program, which returns a String. Then output this string as the return of this function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:30:38+00:00Added an answer on June 2, 2026 at 12:30 pm

    Original attempt:

    The variable $1 means something different in Perl than it does in plpgsql functions. Try

    CREATE OR REPLACE FUNCTION perl_func(VARIADIC params character varying[])
      RETURNS character varying AS
    $BODY$
       use strict;
       use warnings;
       my $val = join("###",@_);
       my $s = `echo $val`;
       chomp($s);
       return $s; 
    $BODY$
      LANGUAGE plperlu VOLATILE
      COST 100;
    

    Unfortunately, this doesn’t work (as noted below) because it doesn’t split the strings properly. Unfortunately, plperl considers the arguments to be a single string:

    CREATE OR REPLACE FUNCTION perl_str(VARIADIC text[])
      RETURNS text AS
    $BODY$
       use strict;
       use warnings;
       my $array_str=shift;
       return $array_str;
    $BODY$
      LANGUAGE plperl VOLATILE;
    

    And when we select it:

    > select perl_str(ARRAY['abc','def']);
     perl_str
    -----------
     {abc,def}
    (1 row)
    

    And to find out for certain that it’s a single string, we can turn to our old friend Data::Dumper:

    CREATE OR REPLACE FUNCTION perl_dump(VARIADIC text[])
      RETURNS text AS
    $BODY$
       use strict;
       use warnings;
       use Data::Dumper;
       my $array_str=shift;
       return Dumper($array_str);
    $BODY$
      LANGUAGE plperl VOLATILE; 
    

    And this returns:

    > select perl_dump(ARRAY['abc','def']);
          perl_dump
    ----------------------
     $VAR1 = '{abc,def}';
    
    (1 row)
    

    So, the output is considered an actual string with curly braces on the end and the entries separated by commas. Okay, well…this is annoying, but at least we can deal with it:

    CREATE OR REPLACE FUNCTION final_perl_func(VARIADIC text[])
      RETURNS text AS
    $BODY$
       use strict;
       use warnings;
       my $array_string=shift;
       $array_string=substr($array_string,1,length($array_string)-2);
       my @array=split(/,/,$array_string);
       my $val=join("###",@array);
       my $s=`echo $val`;
       chomp($s);
       return $s;
    $BODY$
      LANGUAGE plperl VOLATILE;
    

    And this gets us what we want:

    > select final_perl_func(ARRAY['abc','def']);
     final_perl_func
    -----------------
     abc###def
    (1 row)
    

    Note that, for reasons I don’t understand, I had to resort to the use of substr instead of a simple regex replace ($array_string=~s/{}//g;), as the plperl function kept returning the curly brackets when I tried the regex replacement.

    I hadn’t dealt with plperl a lot before answering your questions about it, and the main thing that I’ve learned is that it’s a major pain…you might want to consider manipulating the database from Perl using the Perl DBI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please take a look at this sloppy code and explain to me
Could someone please advise me how i could make this code more generic? I've
Could someone please advice why i have a memory leak in this code? I
Could someone please tell me what is wrong with this code? It works fine
could someone please help me with this problem, sorry for the Language in the
could someone please explain this to me: I have an MVC method I want
Could someone please have a look at this: http://jsfiddle.net/tur9b/9/ It's a simple ordered list.
Could someone please provide me with some code or a tuorial in order to
Could someone please show me a small snippet of code which demonstrates how to
Could someone please describes me, where is problem and how the code could be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.