Postgresql equivalent to sqlite pragma - postgresql

What would be the equivalent of the sqlite pragma below ?
PRAGMA journal_mode = WAL;
PRAGMA auto_vacuum = 1;
PRAGMA temp_store = MEMORY;

Related

Dependency between perl modules

Suppose I have three perl modules as given below :
Test.pm
package Test;
use strict;
use warnings;
use Check;
our $data = Check->getX;
1;
Initialize.pm
package Initialize;
use Check;
use Test;
Check->setX(10);
our $t = $Test::data;
print $t;
1;
Check.pm
package Check;
my $x = 12;
sub setX {
my ($self,$value) = #_;
$x = $value;
}
sub getX
{
return $x;
}
1;
Now, when I run Initialize.pm, I am initializing $x in Check.pm to 10 and $x is assigned to $data in Test.pm. But the the actual valuethat is assigned to $data is 12 which is the initial value given in Check.pm.
So, When are the global variables initialized in perl? How can I enforce that the new value set by me in the Initialize.pm to x is what is loaded into $data?
Now if I replace the statement use Test in Initalize.pm with require Test; and move the statement Check->setX(10) before this require statement then $data is correctly initialized to the new value 10. What is it that is happening differently in this case ?
In general modules have little or no executable code. Object-oriented modules just define the object methods, and sometimes some class data.
When you use Test the whole of Test.pm is compiled and executed, so the value of $data is set at this point.
The call to setX happens straight afterwards, but is too late to affect the asignment of $data.
As I said in my comment your code has a very odd structure, and modules shouldn't have a time dependency on each other at all. You should really remove all executable statements from your modules, but to force your code to do what you want you can write
use strict;
use warnings;
use Check;
BEGIN {
Check->setX(10);
}
use Test;
our $t = $Test::data;
print $t;
But don't do that!
Perl executes a use statement prior to executing anything else in the file.
So the execution order is:
use Check;
$x = 12;
use Test;
use Check; -This only does importing as the file is already executed
$data = Check->getX();
Check->setX(10);
If you replace use with require the instruction is evaluated at the same time as the rest of the instructions and if you move Check->setX(10); before the require it will be evaluated before the get in Test

Perl::Critic "Don't use this method"-type rule

We have been using Perl::Critic here at work to enforce our code conventions. Recently we ran into issues with /tmp directory getting filled up due to the Temp::File::tempdir function. tempdir cleans up when the Perl process terminates, but since our entire backend is a Perl process, this only occurs when the server itself gets restarted (not very often). We want to encourage developers to use the newdir object method in the future, which cleans up after itself as soon as the object goes out of scope.
Basically, we're trying to mark Temp::File::tempdir as a code convention violation, but I can't seem to find any rule that would be similar on CPAN. I understand that this is hard to enforce in a dynamically-typed language without introducing false positives, but I would expect someone has ran into similar issue in the past with another deprecated function. We're not expecting to catch all the tricky cases either, only the most obvious uses of Temp::File::tempdir. The idea is to discourage accidental use of tempdir when newdir could do the job, not to catch all attempts to fool the critic (developer could always just use ## no critic). It would probably be enough to complain when tempdir is used if use Temp::File is defined (preferably checking that nothing else redefines tempdir) and when Temp::File::tempdir is used.
Is there already something similar, or should I start from scratch? Thanks
There isn't anything in Perl::Critic at present to provide what you need, but it's quite possible add a policy to do something like it. Unfortunately PPI isn't comprehensive enought to identify properly what each token in the program is doing, so it takes more coding than it might.
This program checks for a use File::Temp statement that tries to import tempdir using any of
use File::Temp 'tempdir';
use File::Temp q(tempdir);
use File::Temp "tempdir";
use File::Temp qq(tempdir);
use File::Temp qw/ tempdir /;
(with any delimiter for the q, qq, and qw forms). It also checks for a PPI::Token::Word node that looks like a function call and is equal to File::Temp::tempdir.
package Perl::Critic::Policy::Prohibit_tempdir;
use strict;
use warnings;
use Perl::Critic::Utils qw{ is_function_call :severities };
use Scalar::Util 'blessed';
use base 'Perl::Critic::Policy';
my $DESC = 'Temp::File::tempdir function';
my $EXPL = 'The tempdir function from Temp::File is deprecated. Use newdir method instead';
sub default_severity { $SEVERITY_HIGH };
sub applies_to{ qw/ PPI::Statement::Include PPI::Token::Word / }
sub violates {
my ($self, $elem) = #_;
if ($elem->isa('PPI::Statement::Include')) {
return unless $elem->type eq 'use';
my $module = $elem->module;
return unless $module and $module eq 'File::Temp';
for my $kid ($elem->children) {
next unless blessed($kid) =~ /^PPI::Token::Quote/;
if ($kid->can('string') and $kid->string eq 'tempdir'
or $kid->can('literal') and grep $_ eq 'tempdir', $kid->literal) {
return $self->violation($DESC, $EXPL, $elem);
}
}
}
else {
if (is_function_call($elem) and $elem eq 'File::Temp::tempdir') {
return $self->violation($DESC, $EXPL, $elem);
}
}
return;
}
1;
with this code
use strict;
use warnings;
use File::Temp 'tempdir';
use File::Temp "tempdir";
use File::Temp qw/ tempdir /;
my $dir = tempdir();
$dir = tempdir;
$dir = File::Temp::tempdir;
my $ft = File::Temp->new;
$dir = $ft->newdir;
generates this output from perlcritic -4 test.pl
Code not contained in explicit package at line 1, column 1. Violates encapsulation. (Severity: 4)
Temp::File::tempdir function at line 4, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 5, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 6, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 10, column 8. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Module does not end with "1;" at line 13, column 1. Must end with a recognizable true value. (Severity: 4)

How can I dump a string in perl to see if there are any character differences?

I've occasionally had problems with strings being subtly different, in some cases utf8::all changed the behavior, so I assume the subtle differences are unicode. I'd like to dump strings in such a way that the differences will be visual to me. What are my options for doing this?
I recommend the Dump function in the Devel::Peek module in the Perl core:
$ perl -MDevel::Peek -e 'Dump "abc"'
SV = PV(0x10441500) at 0x10491680
REFCNT = 1
FLAGS = (PADTMP,POK,READONLY,pPOK)
PV = 0x10442224 "abc"\0
CUR = 3
LEN = 4
$ perl -MDevel::Peek -e 'Dump "\x{FEFF}abc"'
SV = PV(0x10441050) at 0x10443be0
REFCNT = 1
FLAGS = (PADTMP,POK,READONLY,pPOK,UTF8)
PV = 0x10449bc0 "\357\273\277abc"\0 [UTF8 "\x{feff}abc"]
CUR = 6
LEN = 8
(You see how FLAGS contains UTF8 in the second example, because of the wide character, but not in the first?)
For most uses, Data::Dumper with Useqq will do.
use utf8;
use Data::Dumper;
local $Data::Dumper::Useqq = 1;
print(Dumper("foo–bar"));
print(Dumper("foo-bar"));
Output:
$VAR1 = "foo\x{2013}bar";
$VAR1 = "foo-bar";
If you want internal details (such as the UTF8 flag), use Devel::Peek.
use utf8;
use Devel::Peek;
Dump("foo–bar");
Dump("foo-bar");
Output:
SV = PV(0x328ccc) at 0x1d6a0c4
REFCNT = 1
FLAGS = (PADTMP,POK,READONLY,pPOK,UTF8)
PV = 0x1d6d52c "foo\342\200\223bar"\0 [UTF8 "foo\x{2013}bar"]
CUR = 9
LEN = 12
SV = PV(0x328dcc) at 0x32b594
REFCNT = 1
FLAGS = (PADTMP,POK,READONLY,pPOK)
PV = 0x1d6d50c "foo-bar"\0
CUR = 7
LEN = 12
Have you tried Test::LongString? Even though it's really a test module, it is handy for showing you where the differences in a string occur. It focuses on the parts that are different instead of showing you the whole string, and it make \x{} escapes for specials.
I'd like to see an example where utf8::all changed the behavior, even if just to see an interesting edge case.
All you need to dump out any string is:
printf "U+%v04X\n", $string;
You could use this to format a string:
($print_string = $string) =~ s/([^\x20-\x7E])/sprintf "\\x{%x}", $1/ge;
or even
use charnames ();
($print_string = $string) =~ s/([^\x20-\x7E])/sprintf "\\N{%s}", charnames::viacode(ord $1)/ge;
I have no idea why in the wolrd you would use the misleadingly named utf8::all. It’s not a core module, and you seem to be having some sort of trouble with knowing what it is really doing. If you explicitly used the individual core pieces that go into it, maybe you would understand it all better.

Perl: explanation how to works the "uni::perl" module - loading pragmas and other modules

In my pervious question I asked how to use multiple modules with one use. Got one perfect answer, and another one what pointed me to Modern::Perl module what is really simple.
After a bit searching CPAN I found another module called uni::perl, what is really complicated - it is and equivalent for:
use strict;
use feature qw(say state switch);
no warnings;
use warnings qw(FATAL closed threads internal debugging pack substr malloc
unopened portable prototype inplace io pipe unpack regexp
deprecated exiting glob digit printf utf8 layer
reserved parenthesis taint closure semicolon);
no warnings qw(exec newline);
use utf8;
use open (:utf8 :std);
use mro 'c3';
Can someone explain/comment it how its works?
I pasted the whole code here divided into few segments and added my questions into, (with ###).
I understand than this question is really long. But, dividing it into smaller one will not help, because the whole is about the "uni::perl" module.
Please, help me understand the problematic parts.
package uni::perl;
use 5.010;
BEGIN {
### OK - these are bitmask of different warnings, they're coming from:
# paste this into perl to find bitmask
# no warnings;
# use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype
# inplace io pipe unpack regexp deprecated exiting glob digit printf
# utf8 layer reserved parenthesis taint closure semicolon);
# no warnings qw(exec newline);
# BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 };
${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
$^H |= 0x00000602; ### this mean "use strict;"
}
Setting directly the ${^WARNING_BITS} an the $^H, is faster than a common "use strict" and etc?
What doing this m{ }x.
m{
use strict;
use warnings;
}x;
use mro ();
I know the "match" operator and 'x' flag but not understand what is doing in this context.. use mro is some "dark-magic" what probably an common perl users don't need to know... ;)
What's do the local *__ANON__ line? For what is good the goto in this context?
The whole next BEGIN block is an dark magic for me. ;(
BEGIN {
for my $sub (qw(carp croak confess)) {
no strict 'refs';
*$sub = sub { ### for what need replace the global *croak (etc) with this sub?
my $caller = caller;
local *__ANON__ = $caller .'::'. $sub; ### what's mean this?
require Carp;
### This set the Carp code-refs to the global namespace?
### But who is the "caller" in the BEGIN block? (compile time)
*{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
goto &{ 'Carp::'.$sub }; ### Why need goto here?
};
}
}
Finally - some clearer things. Rewrite the import so, this will be called when use uni::perl;
sub import {
my $me = shift;
my $caller = caller;
### OK - again the bitmasks
${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
### where are these documented?
$^H |=
0x00000602 # strict
| 0x00800000 # utf8
;
# use feature
$^H{feature_switch} =
$^H{feature_say} =
$^H{feature_state} = 1;
# use mro 'c3';
mro::set_mro($caller, 'c3');
#use open (:utf8 :std);
${^OPEN} = ":utf8\0:utf8";
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
### again coderef magic. As I understand it - it will replace the
### "carp, etc" in the callers namespace with the coderef's defined
### in the above BEGIN block. But why with this complicated way?
for my $sub (qw(carp croak confess)) {
no strict 'refs';
*{ $caller .'::'. $sub } = \&$sub;
}
### and finally - I have abosolutely no idea - what do the next code
### will take arguments of "use uni::perl qw(arg)"
### but have no idea how to use it - or what is doing ;(
while (#_) {
my $feature = shift;
if ($feature =~ s/^://) {
my $package = $me. '::'. $feature;
eval "require $package; 1" or croak( "$#" );
$package->load( $caller );
}
}
}
what is doing the last while?
Plus question:
why do the same things twice? Once in BEGIN block and once in import? (import is for the "use" - but why doing nearly the same thing in the "BEGIN" block too?
Because this question has more parts, please, quote the relevant part when you giving an answer.
THANK YOU ALL IN ADVANCE.
Setting the warning bits directly is a bit faster, and has more predictable behavior (you can see everything that should happen), but it is obviously much harder to work with and maintain. It may be that the set of warnings that uni::perl is trying to load is easier done through the bitmasks.
m{ use strict; use warnings;}x; is simply a regex in void context. It would throw an error either about context or $_ not being set if warnings were enabled. I am not sure exactly why this is being done, it might be to appease some code metric system that looks for the lines "use warnings; use strict". I would have probably written it q{...} if 0; which is at least a little clearer.
This BEGIN block is creating custom versions of the functions in Carp. It is using the local *__ANON__ = ... line to set the name of any anonymous subroutines so that the Carp stack trace is easier to follow. The BEGIN block creates the wrapped Carp routines. The import subroutine then loads these new wrapped routines into the caller's namespace.
That last while seems to be loading additional plugin modules for uni::perl.
The same thing is not being done, see the answer to #3. (BEGIN creates the wrapped routines, import installs them into the caller's space)

In Perl, how do you detect if bignum support is loaded in versions before 5.9.4?

Perl's bignum bigint and bigrat pragmas helpfully contain an in_effect function that will detect if the pragma is loaded into a scope by probing the hints hash. However, this only works in version 5.9.4 and later of perl, since that's when the lexical hints hash was introduced.
Is there any good way of determining if these pragmas are in effect in earlier versions of perl? For my uses, I would like to support back to version 5.8.8.
Update: mob's solution below will work if I had access to the lexical space where bignum may be in effect. However, for my use case, I am writing a function that will be called from that space, and inside that function I need to determine if the caller's scope has bignum loaded. (ie, in my code I am calling something like bignum::in_effect(2) to look a few frames up the call stack)
sub test_sub {is_bignum_in_effect_in_the_caller}
# bignum::in_effect(1) in 5.9.4+
test_sub(); # no bignum
{use bignum; test_sub()} # yes bignum
I don't know if this qualifies as a 'good' way, but you can do a simple operation and see if you are getting the bigint/bigrat result or the conventional Perl result.
$bigint_enabled = length(1E20) == 21; # conventional result is 5
At the risk of making this answer even less good, how can you determine whether bigint is enabled in the caller's scope?
One. Require the caller to tell you whether bignum is enabled.
# your package
package Foo;
use base 'Exporter';
use bigint;
our #EXPORT = qw($BIGINT_TEST multiply);
our $BIGINT_TEST = $]>=5.009004
? "bigint::in_effect()"
: "\$bigint::VERSION<0.22 || length(1E20)==21";
sub multiply {
my ($arg1, $arg2, $bigint_enabled) = #_;
if ($bigint_enabled) {
use bigint;
return $arg1*$arg2;
} else {
no bigint;
return $arg1*$arg2;
}
}
# user program
use Foo;
use bigint;
print "Enabled: ", multiply(1E15,1E10, eval $BIGINT_TEST), "\n";
{
no bigint;
print "Disabled: ", multiply(1E15,1E10,eval $BIGINT_TEST), "\n";
}
# result
$ perl510 user_program.pl
Enabled: 10000000000000000000000000
Disabled: 1e+25
$ perl587 user_program.pl ($bignum::VERSION eq 0.07)
Enabled: 10000000000000000000000000
Disabled: 10000000000000000000000000
$ perl588 user_program.pl (includes upgrade to bignum 0.25)
Enabled: 10000000000000000000000000
Disabled: 1e+25
Two. Source filtering? Hack the op tree? Use either of these methods to insert an argument to the method call or to set a global variable prior to the method call.