R
Rainer Weikusat
Are the other gotchas with named nested subroutines than the 'variable
will not stay shared' issue?
will not stay shared' issue?
Rainer said:Are the other gotchas with named nested subroutines than the 'variable
will not stay shared' issue?
John W. Krahn said:Named subroutines cannot be "nested" because their names are package
variables and so do not follow lexical scoping rules.
Ben Morrow said:[...]
sub howl
{
my $x;
$x = 2;
{
sub yodel { return $x; }
}
return sub { ++$x; };
}
my $inc = howl();
print yodel, "\n";
$inc->();
print yodel, "\n";
$inc->();
print yodel, "\n";
my $again = howl();
say(yodel), $again->() for 1..3;
You only called &howl once, so there was only ever one copy of $x and
therefore no problem. The problem arises when you call it again, so
there are now two copies of $x, so it's no longer clear which copy the
global &yodel should refer to.
The subs are actually defined at (Perl) compile time, so depending on
exactly when Pg passes the string to perl to be compiled you may find
you don't need to *run* setup_subs() at all, just CREATE it. You may
also find this is unpredictable and depends on exactly which perl
instance you end up with each time; I don't know the details of when Pg
passes which functions to which Perl instances, but it may be that if
you haven't run setup_subs() in this session this Perl interpreter won't
have seen the definition at all.
[Being an evil-minded person, it occurs to me at this point to wonder
what happens if you say
CREATE FUNCTION setup_subs() RETURNS void AS $$
};
sub foo { ... }
sub bar { ... }
sub {
$$ LANGUAGE plperl;
Little Bobby Tables, and so on...]
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.