in reply to How to Intercept Variable Access

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

Replies are listed 'Best First'.
Re^2: How to Intercept Variable Access
by harangzsolt33 (Deacon) on Sep 16, 2024 at 02:00 UTC
    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

        The real fun starts with local:

        sub do_stuff { local $CD = tempdir(); croak "No Makefile found in $CD" unless -f 'Makefile'; croak "No .git directory in $CD" unless -d '.git'; ... }