my %function_for; my %scalar_ref_for; sub TIESCALAR { my ( $class, $function ) = @_; if ( ref $function ne 'CODE' ) { croak('Argument to tie must be a code reference'); } my $self = bless \do { my $anon_scalar }, $class; $function_for{ ident $self } = $function; $scalar_ref_for{ ident $self } = q{}; return $self; } sub STORE { my ( $self, @args ) = @_; $function_for{ ident $self }->(@args); } sub FETCH { my( $self ) = @_; return $scalar_ref_for{ ident $self }; } #### my $tied_variable; tie $tied_variable, 'Tie::Scalar::Function' => sub { ok( defined $_[0], "got something -> $_[0]" ) }; $tied_variable = "assignment"; #### my $tied_variable; tie $tied_variable, 'Tie::Scalar::Function' => sub { ok( defined $_[0], "got something -> $_[0]" ) }; warn 'here1:',$tied_variable; open(my $fh, '>', \$tied_variable); print {$fh} 'this is a test'; warn 'here2:',$tied_variable;