harangzsolt33 has asked for the wisdom of the Perl Monks concerning the following question:

LanX mentioned Tie::File recently, and my curiosity made me looked up the source code of that module to see how it works, but I couldn't find what I was looking for. See: https://perldoc.perl.org/Tie::File.txt If someone is trying to read an element of an array, then the file read function is activated. Okay, so it looks like there is a caching mechanism also. But my question is how is this tied to the array though? It looks like the read operation is performed by the _fetch() function, but I could not find that single line of code in the source code that is responsible for intercepting the array read operation and redirecting it to the _fetch() function. An array can also be modified in many ways, and how do we intercept those operations?

Edit: Here is an example what I am trying to achieve: For example, my $CD; I want this to be tied to the current directory, so when I do print $CD; then when it tries to read the scalar value, it first calls _MyGetCWD() function and then prints the value returned by this function. And likewise, if I try to set this variable $CD = "C:\\WINDOWS"; then when it sees that I am trying to modify this scalar, then it immediately calls _MySetCWD($NewValue) function to do the job. How do I do this?

Replies are listed 'Best First'.
Re: How to Intercept Variable Access
by ikegami (Patriarch) on Sep 16, 2024 at 10:38 UTC

    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;

      thanks, an eye-opener.

Re: How to Intercept Variable Access
by LanX (Saint) on Sep 16, 2024 at 01:32 UTC
    I haven't mentioned Tie::File recently, and you seem to want to use Tie::Scalar

    You'll need a new FETCH and STORE doing your magic.

    perltie has some examples. Take the code at "Tying Scalars" and adapt it to your needs.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Oh, THANK YOU!! And sorry, I just noticed that this is an old thread from 2021 that popped up on the Recent Threads:
      https://perlmonks.com/?node_id=11126426 This is where I saw you mention Tie::File.

      I think, it popped up because someone commented on it just now.

        Your welcome, have fun experimenting with tied variables.

        FWIW, your use case is not something I would solve with tie but just a function CD()

        • returning the cwd, like print CD;
        • setting the cwd when called with an argument, like CD('..')
        Even the CD="/tmp" syntax can be achieved with an attribute :lvalue see perlsub

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery