Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to Intercept Variable Access

by ikegami (Patriarch)
on Sep 16, 2024 at 10:38 UTC ( [id://11161762]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11161762]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-10-09 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (44 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.