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

From Alex Hunsaker
Subject Re: arrays as pl/perl input arguments [PATCH]
Date
Msg-id AANLkTim-ANJTH-4j1KrEC=3O_AVqjPCAZZitgmWY8buq@mail.gmail.com
Whole thread Raw
In response to Re: arrays as pl/perl input arguments [PATCH]  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-hackers
On Wed, Jan 12, 2011 at 14:45, Andrew Dunstan <andrew@dunslane.net> wrote:
>
> What I was casting a bit of doubt on upthread was whether or not this would
> work without possibly breaking some code, in possibly silent or obscure
> ways.

I can't see how it would break, unless we did it wrong...

> If I'm wrong about that, then by all means let's use some perl Magic
> (that's a technical term) to achieve this. IIRC Alex recently posted some
> code that might be instructive about this.

There might be a more gutsy way to do this so that 'ref' gives you
back what you expected (would that be 'ARRAY' or 'SCALAR' ?), but
there is a simple pure perl solution using overload:
package PLPerl::ArgArray;
use overload '""' => \&to_str;

sub new
{ # note we bless an arrayref here instead of the usual hashref so # you can use this 'object' as a normal array return
bless([@_],... );
 
}

sub to_str
{ my $self = shift; # yeah this is not right not correct :P # we could also die here with something like "You are
tryingto use
 
an Array as a string" or whatever return join(',', map { '{'. $_ .'}' } @{$self});
}
---------


pgsql-hackers by date:

Previous
From: Anthony Gentile
Date:
Subject: N-grams
Next
From: Fujii Masao
Date:
Subject: Re: Change pg_last_xlog_receive_location not to move backwards