xref: /openbsd/gnu/usr.bin/perl/t/op/args.t (revision 4bdff4be)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
9plan( tests => 23 );
10
11# test various operations on @_
12
13sub new1 { bless \@_ }
14{
15    my $x = new1("x");
16    my $y = new1("y");
17    is("@$y","y", 'bless');
18    is("@$x","x", 'bless');
19}
20
21sub new2 { splice @_, 0, 0, "a", "b", "c"; return \@_ }
22{
23    my $x = new2("x");
24    my $y = new2("y");
25    is("@$x","a b c x", 'splice');
26    is("@$y","a b c y", 'splice');
27}
28
29sub new3 { goto &new1 }
30{
31    my $x = new3("x");
32    my $y = new3("y");
33    is("@$y","y", 'goto: single element');
34    is("@$x","x", 'goto: single element');
35}
36
37sub new4 { goto &new2 }
38{
39    my $x = new4("x");
40    my $y = new4("y");
41    is("@$x","a b c x", 'goto: multiple elements');
42    is("@$y","a b c y", 'goto: multiple elements');
43}
44
45# see if cx_popsub() gets to see the right pad across a dounwind() with
46# a reified @_
47
48sub methimpl {
49    my $refarg = \@_;
50    die( "got: @_\n" );
51}
52
53sub method {
54    &methimpl;
55}
56
57my $failcount = 0;
58sub try {
59    eval { method('foo', 'bar'); };
60    print "# $@" if $@;
61    $failcount++;
62}
63
64for (1..5) { try() }
65is($failcount, 5,
66    'cx_popsub sees right pad across a dounwind() with reified @_');
67
68# bug #21542 local $_[0] causes reify problems and coredumps
69
70sub local1 { local $_[0] }
71my $foo = 'foo'; local1($foo); local1($foo);
72is($foo, 'foo',
73    "got 'foo' as expected rather than '\$foo': RT \#21542");
74
75sub local2 { local $_[0]; last L }
76L: { local2 }
77pass("last to label");
78
79$|=1;
80
81sub foo { local(@_) = ('p', 'q', 'r'); }
82sub bar { unshift @_, 'D'; @_ }
83sub baz { push @_, 'E'; return @_ }
84for (1..3) {
85    is(join('',foo('a', 'b', 'c')),'pqr', 'local @_');
86    is(join('',bar('d')),'Dd', 'unshift @_');
87    is(join('',baz('e')),'eE', 'push @_');
88}
89
90# [perl #28032] delete $_[0] was freeing things too early
91
92{
93    my $flag = 0;
94    sub X::DESTROY { $flag = 1 }
95    sub f {
96	delete $_[0];
97	ok(!$flag, 'delete $_[0] : in f');
98    }
99    {
100	my $x = bless [], 'X';
101	f($x);
102	ok(!$flag, 'delete $_[0] : after f');
103    }
104    ok($flag, 'delete $_[0] : outside block');
105}
106
107
108