File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/FormatSpecialVariables.pm
Criterion Covered Total %
statement 22 27 81.4
branch 2 2 100.0
condition 5 6 83.3
subroutine 11 12 91.6
pod 3 5 60.0
total 43 52 82.6


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Variables::FormatSpecialVariables;
2              
3 17     17   11217 use 5.006001;
  17         49  
4 17     17   79 use strict;
  17         24  
  17         354  
5 17     17   72 use warnings;
  17         19  
  17         416  
6 17     17   238 use Readonly;
  17         29  
  17         1042  
7              
8 17     17   88 use Perl::ToPerl6::Utils qw{ :characters :severities };
  17         25  
  17         931  
9              
10 17     17   4164 use base 'Perl::ToPerl6::Transformer';
  17         27  
  17         7762  
11              
12             our $VERSION = '0.03';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{Transform @ARGV to @*ARGS};
17             Readonly::Scalar my $EXPL => q{Perl6 changes many special variables};
18              
19             #-----------------------------------------------------------------------------
20              
21             my %map = (
22             'STDIN' => '$*IN',
23             'STDOUT' => '$*OUT',
24             'STDERR' => '$*ERR',
25             '$ARG' => '$_',
26             #$_[1],$_[2].. => $^a,$^b # Say whaa?
27             #'$a' => -, # XXX Needs some work
28             #- => '$/',
29             '$`' => '$/.prematch',
30             '$PREMATCH' => '$/.prematch',
31             '${^PREMATCH}' => '$/.prematch',
32             '$&' => '~$/',
33             '$MATCH' => '~$/',
34             '${^MATCH}' => '~$/',
35             '$\'' => '$/.postmatch',
36             '$POSTMATCH' => '$/.postmatch',
37             '${^POSTMATCH}' => '$/.postmatch',
38             '$+' => '$/[$/.end]', # Ouch?
39             '$^N' => '$/[*-1]', # Likewise.
40             '@+' => '(map {.from},$/[*])',
41             '@-' => '(map {.to},$/[*])',
42             #'@-' # $-[0] => $0.from, ergo $-[$n] = $/[$n].from # XXX special
43             #'@+' # $+[0] => $0.to, ergo $+[$n] = $/[$n].to # XXX special
44             '$.' => '$*IN.ins()',
45             '$NR' => '$*IN.ins()',
46             '$/' => '$*IN.input-line-separator()',
47             '$RS' => '$*IN.input-line-separator()',
48             '$!' => '$*OUT.autoflush()', # xxx May need some work
49             '$,' => '$*OUT.output-field-separator()',
50             '$OFS' => '$*OUT.output-field-separator()',
51             '$\\' => '$*OUT.output-record-separator()',
52             '$$' => '$*PID',
53             '$PID' => '$*PID',
54             '$(' => '$*GID',
55             '$GID' => '$*GID',
56             '$<' => '$*UID',
57             '$UID' => '$*UID',
58             '$>' => '$*EUID',
59             '$EUID' => '$*EUID',
60             '$)' => '$*EGID',
61             '$GID' => '$*EGID',
62             '$0' => '$*PROGRAM-NAME',
63             '$PROGRAM_NAME' => '$*PROGRAM-NAME',
64             '$^C' => '$*COMPILING',
65             '$COMPILING' => '$*COMPILING',
66             '$^D' => '$*DEBUGGING',
67             '$DEBUGGING' => '$*DEBUGGING',
68             '$^F' => '$*SYS_FD_MAX', # XXX ?
69             '$SYS_FD_MAX' => '$*SYS_FD_MAX', # XXX ?
70             '$^I' => '$*INPLACE_EDIT', # XXX ?
71             '$INPLACE_EDIT' => '$*INPLACE_EDIT', # XXX ?
72             '$^M' => '$*EMERGENCY_MEMORY', # XXX ?
73             '$^O' => '$*KERNEL.name',
74             '$^OSNAME' => '$*KERNEL.name',
75             '$^P' => '$*PERLDB',
76             '$PERLDB' => '$*PERLDB',
77             '$^R' => '$*LAST_REGEXP_CODE_RESULT', # XXX ?
78             '$^T' => '$*INITTIME', # Temporal::Instant
79             '$BASETIME' => '$*INITTIME', # Temporal::Instant
80             '$^V' => '$?PERL.version',
81             '$]' => '$?PERL.version',
82             '$^W' => '$*WARNINGS',
83             '${^WARNING_BITS}' => '$*WARNINGS',
84             '$^X' => '$?COMPILER',
85             'ARGV' => '$*ARGFILES',
86             # $*ARGFILES Note the P6 idiom for this handle:
87             # for lines() {
88             # # each time through loop
89             # # proc a line from files named in ARGS
90             # }
91             '@ARGV' => '@*ARGS', # XXX Remember $ARGV[...]
92             # 'ARGVOUT' # XXX ?
93             # '$ARGV' # XXX ?
94             '@F' => '@_', # XXX May require translation?
95             '%ENV' => '%*ENV', # XXX remember $ENV{...}
96             '@INC' => '@*INC', # XXX remember $INC[...]
97             '$SIG{__WARN__}' => '$*ON_WARN', # XXX Note it's not the actual %SIG
98             '$SIG{__DIE__}' => '$*ON_DIE', # XXX Note it's not the actual %SIG
99             '$@' => '$!', # XXX May not be as appropriate.
100             );
101              
102             #-----------------------------------------------------------------------------
103              
104 41     41 0 1412 sub supported_parameters { return () }
105 29     29 1 114 sub default_severity { return $SEVERITY_HIGHEST }
106 25     25 1 82 sub default_themes { return qw(core bugs) }
107             sub applies_to {
108             return sub {
109             ( $_[1]->isa('PPI::Token::Symbol') or
110             $_[1]->isa('PPI::Token::Word') or
111             $_[1]->isa('PPI::Token::Magic') ) and
112 61 100 100 61   1206 $map{$_[1]->content}
      66        
113             }
114 4     4 1 21 }
115              
116             #-----------------------------------------------------------------------------
117              
118             # Keep track of these because they might be useful notes.
119             my %all_new = (
120             '$!' => 1, # current exception
121             );
122              
123             my %eliminated = (
124             '%!' => 1, # Don't forget $!{...}
125             '$[' => 1,
126             '$*' => 1,
127             '$#' => 1, # XXX Don't confuse with $#a
128             '$^H' => 1, # Yipes?
129             '%^H' => 1, # Yipes?
130            
131             '$!' => 1, # => $! maybe
132             '$ERRNO' => 1, # => $! maybe
133             '$OS_ERROR' => 1, # => $! maybe
134             '$?' => 1, # => $! maybe
135             '$CHILD_ERROR' => 1, # => $! maybe
136             '$@' => 1, # => $! maybe
137             '$^E' => 1,
138             '$^S' => 1,
139             '$"' => 1,
140             '$LIST_SEPARATOR' => 1,
141             '$;' => 1,
142             '$SUBSEP' => 1,
143             '%INC' => 1, # XXX This is in a CompUnitRepo, whatever that is.
144             '%SIG' => 1, # XXX Different than the manpage - event filters plus exception translation
145             '${^OPEN}' => 1, # Supposedly internal-only.
146             );
147              
148             #
149             # @ARGV --> @*ARGS
150             # $1 --> $0 and so on.
151             #
152             sub transform {
153 0     0 0   my ($self, $elem, $doc) = @_;
154 0           my $old_content = $elem->content;
155              
156 0           my $new_content = $map{$old_content};
157              
158 0           $elem->set_content( $new_content );
159              
160 0           return $self->transformation( $DESC, $EXPL, $elem );
161             }
162              
163             1;
164              
165             #-----------------------------------------------------------------------------
166              
167             __END__
168              
169             =pod
170              
171             =head1 NAME
172              
173             Perl::ToPerl6::Transformer::Variables::FormatSpecialVariables - Format special variables such as @ARGV
174              
175              
176             =head1 AFFILIATION
177              
178             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
179             distribution.
180              
181              
182             =head1 DESCRIPTION
183              
184             Perl6 renames many special variables, this changes most of the common variable names, including replacing some of the more obscure variables with new Perl6 equivalent code:
185              
186             @ARGV --> @*ARGS
187             @+ --> (map {.from},$/[*])
188              
189             Other variables are no longer used in Perl6, but will not be removed as likely they have expressions attached to them. These cases will probably be dealt with by adding comments to the expression.
190              
191             Transforms special variables outside of comments, heredocs, strings and POD.
192              
193             =head1 CONFIGURATION
194              
195             This Transformer is not configurable except for the standard options.
196              
197             =head1 AUTHOR
198              
199             Jeffrey Goff <drforr@pobox.com>
200              
201             =head1 COPYRIGHT
202              
203             Copyright (c) 2015 Jeffrey Goff
204              
205             This program is free software; you can redistribute it and/or modify
206             it under the same terms as Perl itself.
207              
208             =cut
209              
210             ##############################################################################
211             # Local Variables:
212             # mode: cperl
213             # cperl-indent-level: 4
214             # fill-column: 78
215             # indent-tabs-mode: nil
216             # c-indentation-style: bsd
217             # End:
218             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :