fetch - returning a lazily-computed scalar, in perl -
I am trying to add some functionality in my code base using binding scalers.
We have a function that is specified to return the scales, I thought I can add some features to the system before returning to these scalers, but it seems that the FETCH method returns It is already said, resulting in an unwanted scanner returning.
Is there any way around this?
I really want to maintain the subroutine interface (returning scalar) if this is possible.
Use strict; Use warnings; Main(); Sub GetThing {My $ thing; Tie $ cheese, 'mithing', @_; Talk of return; } Sub Main {My% m; $ M {Pre} = GetThing ('Fred'); Print "1 \ n"; Print $ m {east}; Print "2 \ n"; Print $ m {east}; Print "3 \ n"; } Package mything; Tie requirement: Scalar; My @ ISA = Qua (Tie :: STScallar); Sub TIESCALAR {my $ class = shift; Bless {name = & gt; Change || 'Nom',}, $ square; } All Effitt {my $ self = shift; Print "Access Warning! \ N"; Return "NAME: '$ self- & gt; {name}' \ n"; }
desired output:
1 access alert! Name: 'Fred' 2 access alert! Name: 'Fred' 3
I can get desired output by returning a reference and dereferencing it at each access, but it disrupts our installed interface, Much more confuse for
- Bk
As DVK said, tie on containers Applies , therefore not useful for back values .
For this, you use overloading, an example (not all possible overloaded operations are given; see):
Use strict; Use warnings; Main(); Sub GetThing {My $ thing; $ Thing = "mything" - & gt; new (@_); Talk of return; } Sub Main {My% m; $ M {Pre} = GetThing ('Fred'); Print "1 \ n"; Print $ m {east}; Print "2 \ n"; Print $ m {east}; Print "3 \ n"; } Package mything; Use overload 'fallback' & gt; 1, '""' = & gt; 'Fetch; Sub new {my $ class = shift; Bless {name = & gt; Change || 'Nom',}, $ square; } All Effitt {my $ self = shift; Print "Access Warning! \ N"; Return "NAME: '$ self- & gt; {name}' \ n"; }
Comments
Post a Comment