Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am amarnus on github.
  • I am amarnus (https://keybase.io/amarnus) on keybase.
  • I have a public key whose fingerprint is 4884 1418 B4B9 7CDF 1C81 CD4C 892F 1E8C BCBF 961D

To claim this, I am signing this object:

@amarnus
amarnus / eval-sub-return.pl
Last active December 21, 2015 09:16
Returning from inside an eval expression in a subroutine
#!/usr/bin/env perl
use Carp::Assert;
sub extractOne {
return 3 - 2;
}
sub returnOne {
my $one = 0;
@amarnus
amarnus / dynamic_hash.pl
Last active August 29, 2015 14:09
Maintaining Mongo sort order from Perl client.
#!/usr/bin/env perl
use strict;
use warnings;
use MongoDB;
use Tie::IxHash;
my $client = MongoDB::MongoClient->new;
my $database = $client->get_database( 'sorty' );
@amarnus
amarnus / modsecurity_mac.md
Last active November 10, 2020 12:35
How to enable and setup ModSecurity on a Mac?

Use brew to install the ModSecurity Apache module.

brew install homebrew/apache/mod_security

Ask Apache to load the module by editing the configuration file (at /etc/apache2/httpd.conf). Under the long list of LoadModule statements, add:

LoadModule security2_module /usr/local/Cellar/mod_security/2.7.7/libexec/mod_security2.so

While you are at it, load the mod_unique_id module as well. This is required by ModSecurity. Do so by uncommenting the line:

@amarnus
amarnus / Mojolicious_Hooks.md
Last active August 29, 2015 14:02
A simple Mojolicious::Lite app & test file to understand the best way to test hooks.

To run this example, you need to have Mojolicious and MojoX::Redis installed.

cpanm Mojolicious
cpanm --force MojoX::Redis

To run the app,

morbo app.pl

To run the tests,

@amarnus
amarnus / perl_regex_transform.pl
Last active August 29, 2015 14:01
A way to do find/replace in Perl without s///
#!/usr/bin/env perl
# A way to do s/<find>/<replace>/s when you have both the <find> regex and the
# <replace> expression as variables.
use strict;
use warnings;
use 5.14.0;
sub transform {
@amarnus
amarnus / app.pl
Last active August 29, 2015 14:00
A simple Mojolocious::Lite app to demonstrate a problem with a Catch-All route
#!/usr/bin/env perl
use strict;
use warnings;
use Mojolicious::Lite;
my $app = app->start;
# When I go this page, I'd ideally want to see a HTTP 401 with an empty body.
@amarnus
amarnus / requiree.php
Last active December 25, 2015 03:49
An exercise to understand variable scoping in PHP when the require_once function is involved. This turned out to be the cause of an issue involving two Drupal modules: amazons3_cors and libraries when used together. https://drupal.org/node/2015731#comment-7948661
<?php
$variable = 'Overriding value set by requiree.';
global $variable2;
$variable2 = 'New global variable set by requiree.';
<?php
define('BROWSER_LINE_SEPARATOR', '<br>');
define('CLI_LINE_SEPARATOR', "\n");
$separator = (php_sapi_name() == 'cli') ? CLI_LINE_SEPARATOR : BROWSER_LINE_SEPARATOR;
// A factory method that returns a object that can be mutated by the caller.
// The mutations are persisted across different calls to the function.
function factory1() {
<?php
// Fetch data from the database
function my_mod_fetch_from_db() {
// Remember the number of dimensions of the static cache depends
// on the number of parameters that this function can take.
$cache = &drupal_static(__FUNCTION__, array());
// Static cache miss
if (empty($cache)) {
// Check persistent cache