File: caller.t

package info (click to toggle)
libcontextual-return-perl 0.003001-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 324 kB
  • ctags: 52
  • sloc: perl: 1,187; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 1,176 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use Contextual::Return;

*foo = sub {
    return
        LIST      { [caller()], [caller(1)] }
        SCALAR    { (caller()||q{}) . '|' . (caller(1)||q{}) }
    ;
};

*bar = sub {
    return [caller()], [caller(1)] if wantarray;
    return (caller()||q{}) . '|' . (caller(1)||q{});
};

# This has to be on one line so the caller lines are the same...
*foo_2 = sub { return &foo; }; *bar_2 = sub { return &bar; };

package Other;
use Test::More 'no_plan';

# This has to be on one line so the caller lines are the same...
my @caller_foo = ::foo(); *::foo = *::bar; my @caller_bar = ::foo();

is_deeply [ \@caller_foo ], [ \@caller_bar ]  => 'Caller same both ways';

# This has to be on one line so the caller lines are the same...
my @caller_foo_2 = ::foo_2(); *::foo_2 = *::bar_2; my @caller_bar_2 = ::foo_2();

is_deeply [ \@caller_foo_2 ], [ \@caller_bar_2 ]  => 'Caller 2 same both ways';

my $caller_foo = ::foo(); *::foo = *::bar; my $caller_bar = ::foo();
is $caller_foo, $caller_bar  => 'Scalar caller same both ways';

my $caller_foo_2 = ::foo_2(); *::foo = *::bar; my $caller_bar_2 = ::foo_2();
is $caller_foo_2, $caller_bar_2  => 'Scalar caller 2 same both ways';