Skip site navigation (1) Skip section navigation (2)

Re: arrays as pl/perl input arguments [PATCH]

From: "Stephen J(dot) Butler" <stephen(dot)butler(at)gmail(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Alex Hunsaker <badalex(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Alexey Klyukin <alexk(at)commandprompt(dot)com>, pgsql-hackers(at)postgresql(dot)org, Andrew Dunstan <andrew(at)dunslane(dot)net>
Subject: Re: arrays as pl/perl input arguments [PATCH]
Date: 2011-01-14 00:15:29
Message-ID: AANLkTi=9Urg4gaRfk14Morm+fO5qZQjiiOGZMAt0g9LL@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-hackers
On Thu, Jan 13, 2011 at 2:06 AM, Martijn van Oosterhout
<kleptog(at)svana(dot)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.

In response to

Responses

pgsql-hackers by date

Next:From: Kevin GrittnerDate: 2011-01-14 00:21:43
Subject: Re: SSI patch version 8
Previous:From: Tom LaneDate: 2011-01-14 00:15:07
Subject: Re: Error code for "terminating connection due to conflict with recovery"

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group