Ref-Util-Rewriter
view release on metacpan or search on metacpan
---
abstract: 'Rewrite your code to use Ref::Util'
author:
- 'Sawyer X'
build_requires:
File::Slurper: '0'
File::Temp: '0'
Pod::Coverage::TrustPod: '0'
Test::EOL: '0'
Test::More: '0.92'
configure_requires:
ExtUtils::MakeMaker: '0'
Makefile.PL view on Meta::CPAN
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.011.
use strict;
use warnings;
use ExtUtils::MakeMaker;
my %WriteMakefileArgs = (
"ABSTRACT" => "Rewrite your code to use Ref::Util",
"AUTHOR" => "Sawyer X",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "Ref-Util-Rewriter",
"LICENSE" => "mit",
"NAME" => "Ref::Util::Rewriter",
"PREREQ_PM" => {
"Exporter" => 0,
"List::Util" => 0,
"PPI" => 0,
"Safe::Isa" => 0
},
"TEST_REQUIRES" => {
"File::Slurper" => 0,
"File::Temp" => 0,
"Pod::Coverage::TrustPod" => 0,
This archive contains the distribution Ref-Util-Rewriter,
version 0.100:
Rewrite your code to use Ref::Util
This software is Copyright (c) 2019 by Sawyer X.
This is free software, licensed under:
The MIT (X11) License
This README file was generated by Dist::Zilla::Plugin::Readme v6.011.
lib/Ref/Util/Rewriter.pm view on Meta::CPAN
package Ref::Util::Rewriter;
# ABSTRACT: Rewrite your code to use Ref::Util
use strict;
use warnings;
use PPI;
use Safe::Isa;
use Exporter qw< import >;
use List::Util qw< first >;
our @EXPORT_OK = qw< rewrite_string rewrite_file >;
lib/Ref/Util/Rewriter.pm view on Meta::CPAN
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Ref::Util::Rewriter - Rewrite your code to use Ref::Util
=head1 VERSION
version 0.100
=head1 SYNOPSIS
use Ref::Util::Rewriter qw< rewrite_string >;
my $new_string = rewrite_string(
q! if ( ref($foo) eq 'HASH' ) { ... } !
);
# $new_string = q! if ( is_hashref($foo) ) { ... } !;
use Ref::Util::Rewriter qw< rewrite_file >;
rewrite_file("script.pl"); # file was now rewritten
=head1 DESCRIPTION
B<Warning:> You should take into account that the meaning of
L<Ref::Util>'s functions are subject to change with regards to
blessed objects. This might change the rewriter code in the future
to be smarter. This might also mean this won't necessarily achieve
what you're expecting.
Run it, check the diff, check your code, run your code, then
(maybe) - knowing the risk you take and absolutely no liability on
me, my family, nor my pets - merge it.
This module rewrites Perl code to use L<Ref::Util> instead of your
regular calls to C<ref>. It is much substantially faster and avoids
several mistakes that haunt beginning and advanced Perl developers.
Please review L<Ref::Util> to fully understand the possible implications
of using it in your case instead of the built-in C<ref> function.
The following constructs of code are supported:
=over 4
=item * Simple statement conditions
ref($foo) eq 'CODE'; # -> is_coderef($foo)
ref $foo eq 'CODE'; # -> is_coderef($foo)
lib/Ref/Util/Rewriter.pm view on Meta::CPAN
=item * C<REF> = C<is_refref>
=back
=head1 SUBROUTINES
=head2 rewrite_string($perl_code_string)
Receive a string representing Perl code and return a new string in which
all C<ref> calls are replaced with the appropriate calls to L<Ref::Util>.
=head2 rewrite_file($filename)
Receive a filename as a string and rewrite the file in place (thus the
file is altered) in which all C<ref> calls are replaced with the
appropriate calls to L<Ref::Util>.
Careful, this function changes your file in place. It is advised to put
your file in some revision control so you could see what changes it has
done and commit them if you accept them.
This does B<not> add a new statement to use L<Ref::Util>, you will still
need to do that yourself.
=head2 rewrite_doc
The guts of the module which uses a direct L<PPI::Document> object and
works on that. It is used internally, but you may call it yourself if
you so wish.
=head1 AUTHOR
t/rewrite.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 16;
BEGIN {
use_ok('Ref::Util::Rewriter');
}
can_ok(
Ref::Util::Rewriter::,
qw<rewrite_doc rewrite_string rewrite_file>,
);
my @tests = (
q{ref $foo eq 'ARRAY';} => q{is_arrayref($foo);},
q{ref $foo eq 'CODE';} => q{is_coderef($foo);},
q{ref $foo eq 'CODE' && ref $foo eq 'ARRAY';} => q{is_coderef($foo) && is_arrayref($foo);},
q{ref($foo) eq 'ARRAY';} => q{is_arrayref($foo);},
q{ref ($foo) eq 'ARRAY';} => q{is_arrayref($foo);},
q{ref($foo) or} => q{is_ref($foo) or},
t/rewrite.t view on Meta::CPAN
q[eval q{ref $foo eq 'ARRAY'}] => q[eval q{is_arrayref($foo)}],
q[eval q/ref $foo eq 'ARRAY'/] => q[eval q/is_arrayref($foo)/],
q[eval "ref $foo eq 'ARRAY'"] => q[eval "is_arrayref($foo)"],
# not supported (yet?)
#qq{ref(\$foo) # comment\nor} => q{is_ref($foo) or # comment},
);
while ( my ( $input, $expect ) = splice @tests, 0, 2 ) {
my $test_name = $input;
my $output = Ref::Util::Rewriter::rewrite_string($input);
is( $output, $expect, $test_name, ); # or diag main::dump($output);
}
sub dump {
my $s = shift or return;
require PPI::Dumper;
require PPI::Document;
my $str = eval {
t/rewrite_file.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 2;
use File::Temp qw/ tempdir /;
use File::Slurper qw/ read_text write_text /;
use Ref::Util::Rewriter qw/ rewrite_file /;
my $tmp = tempdir( CLEANUP => 1 );
my $test_pm = "$tmp/MyPackage.pm";
write_text( $test_pm, <<'CONTENT' );
package MyPackage;
sub run {
my $do = shift;
$do->() f ref $do eq 'CODE';
( run in 1.192 second using v1.01-cache-2.11-cpan-4d50c553e7e )