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

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

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: 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-13 08:06:45
Message-ID: 20110113080645.GA2494@svana.org (view raw or flat)
Thread:
Lists: pgsql-hackers
On Thu, Jan 13, 2011 at 12:06:33AM -0700, Alex Hunsaker wrote:
> > I had supposed that it would be possible to do the string conversion
> > lazily, ie, only if the string value was actually demanded.
> 
> Yep, In-fact if we wanted we could even die (or throw an exception in
> other language speak :) ) when the string value is demanded.

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 was thinking however, if the parameters if the function have names
you can use, then you can make it work. $_[0] would still go the old
way, but the named parameters could be the array.

====================== cut ======================
#!/usr/bin/perl -w
use strict;
no strict 'vars';
package MyClass;

sub TIESCALAR {
   my $class = shift;
   my $self = shift;
   return bless $self, $class;
}

sub FETCH {
   my $self = shift;
   return join(",", @$self);
}

my @a=(1,2);

tie $a, "MyClass", \(at)a;

print "\$a='$a'\n";
print "\$a[0]='$a[0]'\n";


-- 
Martijn van Oosterhout   <kleptog(at)svana(dot)org>   http://svana.org/kleptog/
> Patriotism is when love of your own people comes first; nationalism,
> when hate for people other than your own comes first. 
>                                       - Charles de Gaulle

In response to

Responses

pgsql-hackers by date

Next:From: Fujii MasaoDate: 2011-01-13 08:28:50
Subject: Bug in walreceiver
Previous:From: Fujii MasaoDate: 2011-01-13 07:40:01
Subject: Re: Change pg_last_xlog_receive_location not to move backwards

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