The Ghost Phylactery: Perl Variable Tying as an Observer
Leverage Perl's arcane variable binding mechanism (tie) to implement the Gang of Four Observer patternβintercepting scalar reads and writes transparently without modifying the caller syntax.
package ObservedScalar;
sub TIESCALAR { my ($class, $cb) = @_; bless { val => undef, cb => $cb }, $class; }
sub FETCH { my ($self) = @_; return $self->{val}; }
sub STORE { my ($self, $new) = @_; $self->{cb}->($self->{val}, $new); $self->{val} = $new; }
# In client code:
tie my $mana, 'ObservedScalar', sub {
my ($old, $new) = @_;
print "[OBSERVER] Mana shifted from $old to $new\n";
};
$mana = 100; # Fires observer automatically!
The Lineage to the Gang of Four Observer
In 1994, the Gang of Four defined the Observer Pattern:
βDefine a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.β β Design Patterns, p. 293
In modern reactive frameworks (e.g., Vue Reactivity, MobX, Svelte stores), state mutations trigger UI re-renders automatically. However, in standard languages, observing a variable requires wrapping it in getter/setter methods:
# β οΈ THE TEDIOUS GETTER/SETTER PATTERN
$player->set_health(95); # Must remember to call method rather than assign
Perl possesses one of the most exotic metaprogramming mechanisms in computing history: tie.
Through tie, a standard Perl variable ($scalar, @array, or %hash) can be bound to an underlying object package. Whenever standard Perl syntax reads or assigns to that variable ($mana = 50), Perl routes the operation through FETCH and STORE methods.
The Ghost Phylactery uses tie to turn any plain scalar variable into a fully reactive Observer Subject.
The Complete Perl Script
Save this as /usr/local/bin/phylactery_observer.pl and run with perl:
#!/usr/bin/env perl
# ==============================================================================
# SCRIPT: phylactery_observer.pl
# PATTERN: Observer Pattern via Variable Tying (Gang of Four Behavioral)
# ARCANUM: Necromancy // The Tied Ghost-Variable Phylactery
# DESCRIPTION: Transparent mutation interception using Perl's tie mechanism.
# ==============================================================================
use strict;
use warnings;
use feature 'say';
# ------------------------------------------------------------------------------
# 1. THE TIED OBSERVER PACKAGE (THE PHYLACTERY ENGINE)
# Implements the standard TIESCALAR, FETCH, and STORE interfaces.
# ------------------------------------------------------------------------------
package ReactivePhylactery {
sub TIESCALAR {
my ($class, %args) = @_;
return bless {
value => $args{initial} // 0,
observers => $args{observers} // [],
name => $args{name} // "GhostVariable",
}, $class;
}
sub FETCH {
my ($self) = @_;
return $self->{value};
}
sub STORE {
my ($self, $new_val) = @_;
my $old_val = $self->{value};
$self->{value} = $new_val;
# Broadcast mutation to all registered observers
for my $observer (@{ $self->{observers} }) {
$observer->($self->{name}, $old_val, $new_val);
}
return $new_val;
}
sub add_observer {
my ($self, $cb) = @_;
push @{ $self->{observers} }, $cb;
}
}
# ------------------------------------------------------------------------------
# 2. DEFINING INDEPENDENT SUBSCRIBERS
# ------------------------------------------------------------------------------
my $audit_logger = sub {
my ($var_name, $old, $new) = @_;
say " [AUDIT OBSERVER] '$var_name' mutated from " . ($old // "UNDEF") . " -> $new";
};
my $critical_threshold_alarm = sub {
my ($var_name, $old, $new) = @_;
if ($new < 20) {
say " π¨ [CRITICAL ALARM] '$var_name' has fallen below safety threshold! ($new < 20)";
}
};
# ------------------------------------------------------------------------------
# 3. TYING THE RUNIC VARIABLE
# ------------------------------------------------------------------------------
say "[NECROMANCY] Binding Ghost Phylactery to standard Perl scalar...";
# Transparently tie $health to ReactivePhylactery
my $phylactery = tie my $health, 'ReactivePhylactery', (
name => "Astral_Health",
initial => 100,
observers => [$audit_logger, $critical_threshold_alarm],
);
# ------------------------------------------------------------------------------
# 4. STANDARD VARIABLE SYNTAX TRIGGERS OBSERVERS TRANSPARENTLY
# ------------------------------------------------------------------------------
say "\n--- Test 1: Normal Damage (Standard Scalar Assignment) ---";
$health = 75; # Intercepted by STORE()!
say "\n--- Test 2: Healing Spell ---";
$health += 15; # Evaluates FETCH() then STORE()!
say "\n--- Test 3: Lethal Damage Breach ---";
$health -= 75; # Health drops to 15 -> triggers Alarm observer!
say "\n--- Final Read ---";
say "Final Health Reading: $health";
Tie Interception Architecture
Perl Code: $health -= 75;
β
βββ (1) FETCH() retrieves current value (90)
β
βββ (2) STORE() receives new value (15)
β
βΌ
βββββββββββββββββββββββββββββββββ
β ReactivePhylactery::STORE β
β - Updates internal value β
β - Iterates @observers β
βββββββββββββββββ¬ββββββββββββββββ
β
ββββββββββββββββ΄βββββββββββββββ
βΌ βΌ
[Observer 1: Audit Log] [Observer 2: Alarm]
"Astral_Health: 90 -> 15" "π¨ Critical Alert!"