This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Silence system warning during tests (only on win32 for now)
[perl5.git] / t / comp / opsubs.t
CommitLineData
02c3ec76
AL
1#!./perl -T
2
3use warnings;
4use strict;
5$|++;
6
7=pod
8
9Even if you have a C<sub q{}>, calling C<q()> will be parsed as the
10C<q()> operator. Calling C<&q()> or C<main::q()> gets you the function.
11This test verifies this behavior for nine different operators.
12
13=cut
14
15use Test::More tests => 36;
16
17sub m { return "m-".shift }
18sub q { return "q-".shift }
19sub qq { return "qq-".shift }
20sub qr { return "qr-".shift }
21sub qw { return "qw-".shift }
22sub qx { return "qx-".shift }
23sub s { return "s-".shift }
24sub tr { return "tr-".shift }
25sub y { return "y-".shift }
26
27# m operator
28can_ok( 'main', "m" );
29SILENCE_WARNING: { # Complains because $_ is undef
30 no warnings;
31 isnt( m('unqualified'), "m-unqualified", "m('unqualified') is oper" );
32}
33is( main::m('main'), "m-main", "main::m() is func" );
34is( &m('amper'), "m-amper", "&m() is func" );
35
36# q operator
37can_ok( 'main', "q" );
38isnt( q('unqualified'), "q-unqualified", "q('unqualified') is oper" );
39is( main::q('main'), "q-main", "main::q() is func" );
40is( &q('amper'), "q-amper", "&q() is func" );
41
42# qq operator
43can_ok( 'main', "qq" );
44isnt( qq('unqualified'), "qq-unqualified", "qq('unqualified') is oper" );
45is( main::qq('main'), "qq-main", "main::qq() is func" );
46is( &qq('amper'), "qq-amper", "&qq() is func" );
47
48# qr operator
49can_ok( 'main', "qr" );
50isnt( qr('unqualified'), "qr-unqualified", "qr('unqualified') is oper" );
51is( main::qr('main'), "qr-main", "main::qr() is func" );
52is( &qr('amper'), "qr-amper", "&qr() is func" );
53
54# qw operator
55can_ok( 'main', "qw" );
56isnt( qw('unqualified'), "qw-unqualified", "qw('unqualified') is oper" );
57is( main::qw('main'), "qw-main", "main::qw() is func" );
58is( &qw('amper'), "qw-amper", "&qw() is func" );
59
60# qx operator
61can_ok( 'main', "qx" );
b7c34493
YO
62eval "qx('unqualified'".
63 ($^O eq 'MSWin32' ? " 2>&1)" : ")");
9994ed7c
RGS
64SKIP: {
65 skip("external command not portable on VMS", 1) if $^O eq 'VMS';
66 TODO: {
67 local $TODO = $^O eq 'MSWin32' ? "Tainting of PATH not working of Windows" : $TODO;
68 like( $@, qr/^Insecure/, "qx('unqualified') doesn't work" );
69 }
83176b6e 70}
02c3ec76
AL
71is( main::qx('main'), "qx-main", "main::qx() is func" );
72is( &qx('amper'), "qx-amper", "&qx() is func" );
73
74# s operator
75can_ok( 'main', "s" );
76eval "s('unqualified')";
77like( $@, qr/^Substitution replacement not terminated/, "s('unqualified') doesn't work" );
78is( main::s('main'), "s-main", "main::s() is func" );
79is( &s('amper'), "s-amper", "&s() is func" );
80
81# tr operator
82can_ok( 'main', "tr" );
83eval "tr('unqualified')";
84like( $@, qr/^Transliteration replacement not terminated/, "tr('unqualified') doesn't work" );
85is( main::tr('main'), "tr-main", "main::tr() is func" );
86is( &tr('amper'), "tr-amper", "&tr() is func" );
87
88# y operator
89can_ok( 'main', "y" );
90eval "y('unqualified')";
91like( $@, qr/^Transliteration replacement not terminated/, "y('unqualified') doesn't work" );
92is( main::y('main'), "y-main", "main::y() is func" );
93is( &y('amper'), "y-amper", "&y() is func" );
94
95=pod
96
97from irc://irc.perl.org/p5p 2004/08/12
98
99 <kane-xs> bug or feature?
100 <purl> You decide!!!!
101 <kane-xs> [kane@coke ~]$ perlc -le'sub y{1};y(1)'
102 <kane-xs> Transliteration replacement not terminated at -e line 1.
103 <Nicholas> bug I think
104 <kane-xs> i'll perlbug
105 <rgs> feature
106 <kane-xs> smiles at rgs
107 <kane-xs> done
108 <rgs> will be closed at not a bug,
109 <rgs> like the previous reports of this one
110 <Nicholas> feature being first class and second class keywords?
111 <rgs> you have similar ones with q, qq, qr, qx, tr, s and m
112 <rgs> one could say 1st class keywords, yes
113 <rgs> and I forgot qw
114 <kane-xs> hmm silly...
115 <Nicholas> it's acutally operators, isn't it?
116 <Nicholas> as in you can't call a subroutine with the same name as an
117 operator unless you have the & ?
118 <kane-xs> or fqpn (fully qualified package name)
119 <kane-xs> main::y() works just fine
120 <kane-xs> as does &y; but not y()
121 <Andy> If that's a feature, then let's write a test that it continues
122 to work like that.
123
124=cut