Adjust some comments about structure properties in pg_stat.h
[pgsql.git] / src / pl / plperl / plc_perlboot.pl
blobf6593f032a9fc0876263c62bbd613b0a117febd2
2 # Copyright (c) 2021-2024, PostgreSQL Global Development Group
4 # src/pl/plperl/plc_perlboot.pl
6 use strict;
7 use warnings FATAL => 'all';
9 use vars qw(%_SHARED $_TD);
11 PostgreSQL::InServer::Util::bootstrap();
13 # globals
15 sub ::is_array_ref
17 return ref($_[0]) =~ m/^(?:PostgreSQL::InServer::)?ARRAY$/;
20 sub ::encode_array_literal
22 my ($arg, $delim) = @_;
23 return $arg unless (::is_array_ref($arg));
24 $delim = ', ' unless defined $delim;
25 my $res = '';
26 foreach my $elem (@$arg)
28 $res .= $delim if length $res;
29 if (ref $elem)
31 $res .= ::encode_array_literal($elem, $delim);
33 elsif (defined $elem)
35 (my $str = $elem) =~ s/(["\\])/\\$1/g;
36 $res .= qq("$str");
38 else
40 $res .= 'NULL';
43 return qq({$res});
46 sub ::encode_array_constructor
48 my $arg = shift;
49 return ::quote_nullable($arg) unless ::is_array_ref($arg);
50 my $res = join ", ",
51 map { (ref $_) ? ::encode_array_constructor($_) : ::quote_nullable($_) }
52 @$arg;
53 return "ARRAY[$res]";
57 #<<< protect next line from perltidy so perlcritic annotation works
58 package PostgreSQL::InServer; ## no critic (RequireFilenameMatchesPackage)
59 #>>>
60 use strict;
61 use warnings FATAL => 'all';
63 sub plperl_warn
65 (my $msg = shift) =~ s/\(eval \d+\) //g;
66 chomp $msg;
67 &::elog(&::WARNING, $msg);
68 return;
70 $SIG{__WARN__} = \&plperl_warn;
72 sub plperl_die
74 (my $msg = shift) =~ s/\(eval \d+\) //g;
75 die $msg;
77 $SIG{__DIE__} = \&plperl_die;
79 sub mkfuncsrc
81 my ($name, $imports, $prolog, $src) = @_;
83 my $BEGIN = join "\n", map {
84 my $names = $imports->{$_} || [];
85 "$_->import(qw(@$names));"
86 } sort keys %$imports;
87 $BEGIN &&= "BEGIN { $BEGIN }";
89 return qq[ package main; sub { $BEGIN $prolog $src } ];
92 sub mkfunc
94 ## no critic (ProhibitNoStrict, ProhibitStringyEval);
95 no strict; # default to no strict for the eval
96 no warnings; # default to no warnings for the eval
97 my $ret = eval(mkfuncsrc(@_));
98 $@ =~ s/\(eval \d+\) //g if $@;
99 return $ret;
100 ## use critic
108 package PostgreSQL::InServer::ARRAY;
109 use strict;
110 use warnings FATAL => 'all';
112 use overload
113 '""' => \&to_str,
114 '@{}' => \&to_arr;
116 sub to_str
118 my $self = shift;
119 return ::encode_typed_literal($self->{'array'}, $self->{'typeoid'});
122 sub to_arr
124 return shift->{'array'};