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;