Use it like so:# delays determination of a scalar's value until needed package Tie::Scalar::Once; require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( delay ); use strict; sub delay { tie $_[0], __PACKAGE__, \$_[0], $_[1]; } sub TIESCALAR { my ($class, $var, $code) = @_; bless [ $var, $code ], $class; } sub FETCH { my ($self) = @_; my $val = $self->[1]->(); untie ${ $self->[0] }; ${ $self->[0] } = $val; } sub STORE { require Carp; Carp::croak("Can't store $_[1] to a constant"); } 1;
use Tie::Scalar::Once; delay $x => sub { sqrt 2 }; # ... if ($you_need_to) { print $x }
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
In reply to Re: Pre-caching large constants
by japhy
in thread Pre-caching large constants
by sifukurt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |