File: scalar.t

package info (click to toggle)
libtemplate-perl 2.24-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,660 kB
  • sloc: perl: 14,518; makefile: 15; sh: 5
file content (113 lines) | stat: -rw-r--r-- 2,625 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#============================================================= -*-perl-*-
#
# t/scalar.t
#
# Test the Scalar plugin which allows object methods to be called in
# scalar context.
#
# Written by Andy Wardley <abw@wardley.org>
#
# Copyright (C) 1996-2008 Andy Wardley.  All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================

use strict;
use warnings;
use lib qw( ./lib ../lib ../blib/lib ../blib/arch );
use Template::Test;

#------------------------------------------------------------------------
# definition of test object class
#------------------------------------------------------------------------

package Template::Test::HashObject;

sub new {
    bless {}, shift;
}

sub bar {
    return wantarray
         ? qw( hash object method called in array context )
         :    'hash object method called in scalar context';
}

package Template::Test::ListObject;

sub new {
    bless [], shift;
}

sub bar {
    return wantarray
         ? qw( list object method called in array context )
         :    'list object method called in scalar context';
}


#-----------------------------------------------------------------------
# main
#-----------------------------------------------------------------------

package main;

my $vars = { 
    hashobj => Template::Test::HashObject->new,
    listobj => Template::Test::ListObject->new,
    subref  => sub {
        return wantarray
            ? (qw( subroutine called in array context ), @_)
            :    'subroutine called in scalar context ' . join(' ', @_);
    }
};

test_expect(\*DATA, undef, $vars);



#------------------------------------------------------------------------
# test input
#------------------------------------------------------------------------

__DATA__
-- test -- 
[% hashobj.bar.join %]
-- expect --
hash object method called in array context

-- test --
[% USE scalar -%]
[% hashobj.scalar.bar %]
-- expect --
hash object method called in scalar context

-- test -- 
[% listobj.bar.join %]
-- expect --
list object method called in array context

-- test --
[% USE scalar -%]
[% listobj.scalar.bar %]
-- expect --
list object method called in scalar context

-- test --
[% hash = { a = 10 }; 
   TRY; hash.scalar.a; CATCH; error; END;
%]
-- expect --
scalar error - invalid object method: a

-- test --
[% subref(10, 20).join %]
-- expect --
subroutine called in array context 10 20

-- test --
[% USE scalar; scalar.subref(30, 40) %]
-- expect --
subroutine called in scalar context 30 40