[ create a new paste ] login | about

Link: http://codepad.org/tsNvDoM8    [ raw code | output | fork ]

Perl, pasted on Jul 4:
use strict;
use warnings;
{
    package MyScalar;
    use Carp;
    sub TIESCALAR {
        my $pkg = shift;
        carp "TIESCALAR('$pkg')";
        bless \do { my $scalar }, $pkg;
    }
    sub FETCH($) {
        my $this = shift;
        carp "FETCH($this)";
        return $$this;
    }
    sub STORE($$) {
        my ( $this, $value ) = @_;
        carp "STORE($this, $value)";
        $$this = $value;
    }
    sub DESTROY {
        my $this = shift;
        carp "DESTROY($this)";
    }
}

{
    tie my $scalar, 'MyScalar';
    $scalar = 1;
    print $scalar, "\n";
    $scalar++;
    print $scalar, "\n";
    print 'tied($scalar) = ', tied($scalar), "\n";
    tied($scalar)->STORE(3);
    print $scalar, "\n";
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
TIESCALAR('MyScalar') at t.pl line 28
STORE(MyScalar=SCALAR(0x815d6bc), 1) at t.pl line 29
FETCH(MyScalar=SCALAR(0x815d6bc)) at t.pl line 30
1
FETCH(MyScalar=SCALAR(0x815d6bc)) at t.pl line 31
STORE(MyScalar=SCALAR(0x815d6bc), 2) at t.pl line 31
FETCH(MyScalar=SCALAR(0x815d6bc)) at t.pl line 32
2
tied($scalar) = MyScalar=SCALAR(0x815d6bc)
STORE(MyScalar=SCALAR(0x815d6bc), 3) at t.pl line 34
FETCH(MyScalar=SCALAR(0x815d6bc)) at t.pl line 35
3
DESTROY(MyScalar=SCALAR(0x815d6bc)) at t.pl line 28


Create a new paste based on this one


Comments: