in reply to How to Intercept Variable Access

A Variable::Magic solution:

use Cwd qw( getcwd ); use Variable::Magic qw( cast wizard ); my $wiz = wizard( get => sub { ${ $_[0] } = getcwd(); }, set => sub { chdir( ${ $_[0] } ) or die( "chdir `${ $_[0] }`: $!" ); }, ); cast my $CD, $wiz;

With an alias:

use experimental qw( refaliasing declared_refs ); use Cwd qw( getcwd ); use Variable::Magic qw( cast wizard ); my $wiz = wizard( get => sub { my \$sv = shift; $sv = getcwd(); }, set => sub { my \$sv = shift; chdir( $sv ) or die( "chdir `$sv`: $!" ); }, ); cast my $CD, $wiz;

Replies are listed 'Best First'.
Re^2: How to Intercept Variable Access
by bliako (Abbot) on Sep 16, 2024 at 11:16 UTC

    thanks, an eye-opener.