1Test2::Util(3) User Contributed Perl Documentation Test2::Util(3)
2
3
4
6 Test2::Util - Tools used by Test2 and friends.
7
9 Collection of tools used by Test2 and friends.
10
12 All exports are optional. You must specify subs to import.
13
14 ($success, $error) = try { ... }
15 Eval the codeblock, return success or failure, and the error
16 message. This code protects $@ and $!, they will be restored by the
17 end of the run. This code also temporarily blocks $SIG{DIE}
18 handlers.
19
20 protect { ... }
21 Similar to try, except that it does not catch exceptions. The idea
22 here is to protect $@ and $! from changes. $@ and $! will be
23 restored to whatever they were before the run so long as it is
24 successful. If the run fails $! will still be restored, but $@ will
25 contain the exception being thrown.
26
27 CAN_FORK
28 True if this system is capable of true or pseudo-fork.
29
30 CAN_REALLY_FORK
31 True if the system can really fork. This will be false for systems
32 where fork is emulated.
33
34 CAN_THREAD
35 True if this system is capable of using threads.
36
37 USE_THREADS
38 Returns true if threads are enabled, false if they are not.
39
40 get_tid
41 This will return the id of the current thread when threads are
42 enabled, otherwise it returns 0.
43
44 my $file = pkg_to_file($package)
45 Convert a package name to a filename.
46
47 $string = ipc_separator()
48 Get the IPC separator. Currently this is always the string '~'.
49
50 $string = gen_uid()
51 Generate a unique id (NOT A UUID). This will typically be the
52 process id, the thread id, the time, and an incrementing integer
53 all joined with the "ipc_separator()".
54
55 These ID's are unique enough for most purposes. For identical ids
56 to be generated you must have 2 processes with the same PID
57 generate IDs at the same time with the same current state of the
58 incrementing integer. This is a perfectly reasonable thing to
59 expect to happen across multiple machines, but is quite unlikely to
60 happen on one machine.
61
62 This can fail to be unique if a process generates an id, calls
63 exec, and does it again after the exec and it all happens in less
64 than a second. It can also happen if the systems process id's cycle
65 in less than a second allowing 2 different programs that use this
66 generator to run with the same PID in less than a second. Both
67 these cases are sufficiently unlikely. If you need universally
68 unique ids, or ids that are unique in these conditions, look at
69 Data::UUID.
70
71 ($ok, $err) = do_rename($old_name, $new_name)
72 Rename a file, this wraps "rename()" in a way that makes it more
73 reliable cross-platform when trying to rename files you recently
74 altered.
75
76 ($ok, $err) = do_unlink($filename)
77 Unlink a file, this wraps "unlink()" in a way that makes it more
78 reliable cross-platform when trying to unlink files you recently
79 altered.
80
81 ($ok, $err) = try_sig_mask { ... }
82 Complete an action with several signals masked, they will be
83 unmasked at the end allowing any signals that were intercepted to
84 get handled.
85
86 This is primarily used when you need to make several actions atomic
87 (against some signals anyway).
88
89 Signals that are intercepted:
90
91 SIGINT
92 SIGALRM
93 SIGHUP
94 SIGTERM
95 SIGUSR1
96 SIGUSR2
97
99 5.10.0
100 Perl 5.10.0 has a bug when compiled with newer gcc versions. This
101 bug causes a segfault whenever a new thread is launched. Test2 will
102 attempt to detect this, and note that the system is not capable of
103 forking when it is detected.
104
105 Devel::Cover
106 Devel::Cover does not support threads. CAN_THREAD will return false
107 if Devel::Cover is loaded before the check is first run.
108
110 The source code repository for Test2 can be found at
111 http://github.com/Test-More/test-more/.
112
114 Chad Granum <exodist@cpan.org>
115
117 Chad Granum <exodist@cpan.org>
118 Kent Fredric <kentnl@cpan.org>
119
121 Copyright 2020 Chad Granum <exodist@cpan.org>.
122
123 This program is free software; you can redistribute it and/or modify it
124 under the same terms as Perl itself.
125
126 See http://dev.perl.org/licenses/
127
128
129
130perl v5.32.1 2021-01-27 Test2::Util(3)