|  line  | 
 stmt  | 
 bran  | 
 cond  | 
 sub  | 
 pod  | 
 time  | 
 code  | 
| 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 package Perlude::Open;  | 
| 
2
 | 
14
 | 
 
 | 
 
 | 
  
14
  
 | 
 
 | 
60
 | 
 use strict;  | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
17
 | 
    | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
477
 | 
    | 
| 
3
 | 
14
 | 
 
 | 
 
 | 
  
14
  
 | 
 
 | 
50
 | 
 use warnings;  | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
16
 | 
    | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
368
 | 
    | 
| 
4
 | 
14
 | 
 
 | 
 
 | 
  
14
  
 | 
 
 | 
163
 | 
 use 5.10.0;  | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
34
 | 
    | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
560
 | 
    | 
| 
5
 | 
14
 | 
 
 | 
 
 | 
  
14
  
 | 
 
 | 
68
 | 
 use Exporter qw< import >;  | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
17
 | 
    | 
| 
 
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
3199
 | 
    | 
| 
6
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our @EXPORT  = qw< open_file >;  | 
| 
7
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub _source_for_nargs (_) {  | 
| 
9
 | 
4
 | 
  
 50
  
 | 
 
 | 
  
4
  
 | 
 
 | 
9
 | 
     my $nargs = shift or die;   | 
| 
10
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
7
 | 
     my $args =  join ',', map {'$a'.$_} 1..$nargs;  | 
| 
 
 | 
10
 | 
 
 | 
 
 | 
 
 | 
 
 | 
23
 | 
    | 
| 
11
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
390
 | 
     sprintf 'sub {  | 
| 
12
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
         my (%s) = @_;  | 
| 
13
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
         open my $fh, %s  | 
| 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
             or die "$! while open_file %s";  | 
| 
15
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
         $fh; }', ($args)x3;  | 
| 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 };  | 
| 
17
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
18
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub _prepare_special_forms {  | 
| 
19
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
6
 | 
     +{ map {  | 
| 
20
 | 
1
 | 
 
 | 
 
 | 
  
1
  
 | 
 
 | 
3
 | 
         my $cb = eval _source_for_nargs;  | 
| 
21
 | 
4
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
12
 | 
         $@ and die $@;  | 
| 
22
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
15
 | 
         $_ => $cb; } 1..4 };  | 
| 
23
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
24
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
25
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub _callback_for_nargs(_) {  | 
| 
26
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     # build callbacks for number of arguments from 1 to 4  | 
| 
27
 | 
2
 | 
 
 | 
 
 | 
  
2
  
 | 
 
 | 
4
 | 
     state $open_with = _prepare_special_forms;  | 
| 
28
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     # the 4 arguments form callback should work for 4 and more.  | 
| 
29
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     # i copy this ref on demand whenever i see a new number of arguments   | 
| 
30
 | 
2
 | 
 
 | 
  
 33
  
 | 
 
 | 
 
 | 
52
 | 
     $open_with->{ +@_ } ||= $open_with->{4};   | 
| 
31
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
32
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
33
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub open_file {  | 
| 
34
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     # if the file (can be $_) is open, just return  | 
| 
35
 | 
5
 | 
  
100
  
 | 
  
 33
  
 | 
  
5
  
 | 
  
0
  
 | 
549
 | 
     return shift if ref ($_[0]||=$_);  | 
| 
36
 | 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
6
 | 
     ( _callback_for_nargs +@_ )->(@_)  | 
| 
37
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }   |