Re: arrays as pl/perl input arguments [PATCH] - Mailing list pgsql-hackers

From Stephen J. Butler
Subject Re: arrays as pl/perl input arguments [PATCH]
Date
Msg-id AANLkTi=9Urg4gaRfk14Morm+fO5qZQjiiOGZMAt0g9LL@mail.gmail.com
Whole thread Raw
In response to Re: arrays as pl/perl input arguments [PATCH]  (Martijn van Oosterhout <kleptog@svana.org>)
Responses Re: arrays as pl/perl input arguments [PATCH]  ("David E. Wheeler" <david@kineticode.com>)
List pgsql-hackers
On Thu, Jan 13, 2011 at 2:06 AM, Martijn van Oosterhout
<kleptog@svana.org> wrote:
> I played with this a little and it is fairly easy to make a variable
> such that $a is the string representation and $a[0] the first value of
> the array. The problem is that you can't pass such a variable into a
> subroutine.

I played with this too:


#!/usr/bin/perl -w

use strict;

package Pg::ArrayArg;

use overload   '""'    => \&as_s,   '@{}'   => \&as_a;

sub new {   my $proto = shift;   my $class = ref $proto || $proto;
   bless {       string      => shift,       array       => shift   }, $class;
}

sub as_s {   shift->{ 'string' };
}

sub as_a {   shift->{ 'array' };
}

package main;

my $aa = Pg::ArrayArg->new( '{1,2}', [ 1, 2 ] );

printf "ref = %s\n", ref $aa;
print "string = $aa\n";
printf "string = %s\n", $aa;
printf "array index = (%s, %s)\n", $aa->[ 0 ], $aa->[ 1 ];
printf "array_ref = %s\n", scalar @$aa;

print "regexp test = ";
if ($aa =~ /^{(.*)}$/) {   print "looks like array\n";   printf "join of split = %s\n", join ';', split /,/, $1;
} else {   print "doesn't look like array\n";
}

Suppose one of these compatibility objects is passed into legacy code
as $_[0]. The problem is that 'ref $_[0]' will return 'Pg::ArrayArg'
instead of what it used to, '' (empty string). Other than that, I
think it performs as people would expect.

You could even change 'as_s' to generate the string on the fly as
requested instead of generating both representations at instantiation.

Just my $0.02.


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Error code for "terminating connection due to conflict with recovery"
Next
From: "Kevin Grittner"
Date:
Subject: Re: SSI patch version 8