This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Rename a variable
[perl5.git] / t / op / gmagic.t
index 9abde47..0c85955 100644 (file)
@@ -2,8 +2,8 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
     require './test.pl';
+    set_up_inc('../lib');
 }
 
 use strict;
@@ -56,6 +56,30 @@ ok($s eq *strat,
    'Assignment should not ignore magic when the last thing assigned was a glob');
 expected_tie_calls(tied $c, 1, 1);
 
+package o { use overload '""' => sub { "foo\n" } }
+$c = bless [], o::;
+chomp $c;
+expected_tie_calls(tied $c, 1, 2, 'chomping a ref');
+
+{
+    no warnings 'once'; # main::foo
+    my $outfile = tempfile();
+    open my $h, ">$outfile" or die  "$0 cannot close $outfile: $!";
+    binmode $h;
+    print $h "bar\n";
+    close $h or die "$0 cannot close $outfile: $!";    
+
+    $c = *foo;                                         # 1 write
+    open $h, $outfile;
+    binmode $h;
+    sysread $h, $c, 3, 7;                              # 1 read; 1 write
+    is $c, "*main::bar", 'what sysread wrote';         # 1 read
+    expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
+    close $h or die "$0 cannot close $outfile: $!";
+
+    unlink_all $outfile;
+}
+
 # autovivication of aelem, helem, of rv2sv combined with get-magic
 {
     my $true = 1;
@@ -64,37 +88,37 @@ expected_tie_calls(tied $c, 1, 1);
     $$s = undef;
     $$s->[0] = 73;
     is($$s->[0], 73);
-    expected_tie_calls(tied $$s, 2, 2);
+    expected_tie_calls(tied $$s, 3, 2);
 
     my @a;
     tie $a[0], "Tie::Monitor";
     $a[0] = undef;
     $a[0][0] = 73;
     is($a[0][0], 73);
-    expected_tie_calls(tied $a[0], 2, 2);
+    expected_tie_calls(tied $a[0], 3, 2);
 
     my %h;
     tie $h{foo}, "Tie::Monitor";
     $h{foo} = undef;
     $h{foo}{bar} = 73;
     is($h{foo}{bar}, 73);
-    expected_tie_calls(tied $h{foo}, 2, 2);
+    expected_tie_calls(tied $h{foo}, 3, 2);
 
     # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
     $$s = undef;
     ${ (), $$s }[0] = 73;
     is( $$s->[0], 73);
-    expected_tie_calls(tied $$s, 2, 2);
+    expected_tie_calls(tied $$s, 3, 2);
 
     $$s = undef;
     ( ! $true ? undef : $$s )->[0] = 73;
     is( $$s->[0], 73);
-    expected_tie_calls(tied $$s, 2, 2);
+    expected_tie_calls(tied $$s, 3, 2);
 
     $$s = undef;
     ( $true ? $$s : undef )->[0] = 73;
     is( $$s->[0], 73);
-    expected_tie_calls(tied $$s, 2, 2);
+    expected_tie_calls(tied $$s, 3, 2);
 }
 
 # A plain *foo should not call get-magic on *foo.
@@ -137,6 +161,50 @@ ok($wgot == 0, 'a plain *foo causes no set-magic');
   &$rsub;
   expected_tie_calls $tied_to, 1, 0,
     'mortal magic var is explicitly returned in recursive autoviv context';
+
+  $tied_to = tie $_{elem}, "Tie::Monitor";
+  my $x = \sub { delete $_{elem} }->();
+  expected_tie_calls $tied_to, 1, 0,
+     'mortal magic var is implicitly returned to refgen';
+  is tied $$x, undef,
+     'mortal magic var is copied when implicitly returned';
+
+  $tied_to = tie $_{elem}, "Tie::Monitor";
+  $x = \sub { return delete $_{elem} }->();
+  expected_tie_calls $tied_to, 1, 0,
+     'mortal magic var is explicitly returned to refgen';
+  is tied $$x, undef,
+     'mortal magic var is copied when explicitly returned';
+
+  $tied_to = tie $_{elem}, "Tie::Monitor";
+  $x = \do { 1; delete $_{elem} };
+  expected_tie_calls $tied_to, 1, 0,
+     'mortal magic var from do passed to refgen';
+  is tied $$x, undef,
+     'mortal magic var from do is copied';
+}
+
+# For better or worse, the order in which concat args are fetched varies
+# depending on their number. In A .= B.C.D, they are fetched in the order
+# BCDA, while for A .= B, the order is AB (so for a single concat, the LHS
+# tied arg is FETCH()ed first). Make sure multiconcat preserves current
+# behaviour.
+
+package Increment {
+    sub TIESCALAR {  bless [0, 0] }
+    # returns a new value for each FETCH, until the first STORE
+    sub FETCH { my $x = $_[0][0]; $_[0][0]++ unless $_[0][1]; $x }
+    sub STORE { @{$_[0]} = ($_[1],1) }
+
+    my $t;
+    tie $t, 'Increment';
+    my $r;
+    $r = $t . $t;
+    ::is $r, '01', 'Increment 01';
+    $r = "-$t-$t-$t-";
+    ::is $r, '-2-3-4-', 'Increment 234';
+    $t .= "-$t-$t-$t-";
+    ::is $t, '8-5-6-7-', 'Increment 8567';
 }
 
 done_testing();