xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/autouse.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gateuse Test;
9*0Sstevel@tonic-gateBEGIN { plan tests => 10; }
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gateBEGIN {
12*0Sstevel@tonic-gate    require autouse;
13*0Sstevel@tonic-gate    eval {
14*0Sstevel@tonic-gate        "autouse"->import('List::Util' => 'List::Util::first(&@)');
15*0Sstevel@tonic-gate    };
16*0Sstevel@tonic-gate    ok( !$@ );
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate    eval {
19*0Sstevel@tonic-gate        "autouse"->import('List::Util' => 'Foo::min');
20*0Sstevel@tonic-gate    };
21*0Sstevel@tonic-gate    ok( $@, qr/^autouse into different package attempted/ );
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate    "autouse"->import('List::Util' => qw(max first(&@)));
24*0Sstevel@tonic-gate}
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gatemy @a = (1,2,3,4,5.5);
27*0Sstevel@tonic-gateok( max(@a), 5.5);
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate# first() has a prototype of &@.  Make sure that's preserved.
31*0Sstevel@tonic-gateok( (first { $_ > 3 } @a), 4);
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate# Example from the docs.
35*0Sstevel@tonic-gateuse autouse 'Carp' => qw(carp croak);
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate{
38*0Sstevel@tonic-gate    my @warning;
39*0Sstevel@tonic-gate    local $SIG{__WARN__} = sub { push @warning, @_ };
40*0Sstevel@tonic-gate    carp "this carp was predeclared and autoused\n";
41*0Sstevel@tonic-gate    ok( scalar @warning, 1 );
42*0Sstevel@tonic-gate    ok( $warning[0], "this carp was predeclared and autoused\n" );
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate    eval { croak "It is but a scratch!" };
45*0Sstevel@tonic-gate    ok( $@, qr/^It is but a scratch!/);
46*0Sstevel@tonic-gate}
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate# Test that autouse's lazy module loading works.  We assume that nothing
50*0Sstevel@tonic-gate# involved in this test uses Text::Soundex, which is pretty safe.
51*0Sstevel@tonic-gateuse autouse 'Text::Soundex' => qw(soundex);
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gatemy $mod_file = 'Text/Soundex.pm'; # just fine and portable for %INC
54*0Sstevel@tonic-gateok( !exists $INC{$mod_file} );
55*0Sstevel@tonic-gateok( soundex('Basset'), 'B230' );
56*0Sstevel@tonic-gateok( exists $INC{$mod_file} );
57*0Sstevel@tonic-gate
58