Git Product home page Git Product logo

perl-minimumversion-fast's Introduction

NAME

Perl::MinimumVersion::Fast - Find a minimum required version of perl for Perl code

SYNOPSIS

use Perl::MinimumVersion::Fast;

my $p = Perl::MinimumVersion::Fast->new($filename);
print $p->minimum_version, "\n";

DESCRIPTION

"Perl::MinimumVersion::Fast" takes Perl source code and calculates the minimum version of perl required to be able to run it. Because it is based on goccy's Compiler::Lexer, it can do this without having to actually load the code.

Perl::MinimumVersion::Fast is an alternative fast & lightweight implementation of Perl::MinimumVersion.

This module supports only Perl 5.8.1+. If you want to support Perl 5.6, use Perl::MinimumVersion instead.

In 2013, you don't need to support Perl 5.6 in most of case.

METHODS

  • my $p = Perl::MinimumVersion::Fast->new($filename);

  • my $p = Perl::MinimumVersion::Fast->new(\$src);

    Create new instance. You can create object from $filename and \$src in string.

  • $p->minimum_version();

    Get a minimum perl version the code required.

  • $p->minimum_explicit_version()

    The minimum_explicit_version method checks through Perl code for the use of explicit version dependencies such as.

      use 5.006;
      require 5.005_03;
    

    Although there is almost always only one of these in a file, if more than one are found, the highest version dependency will be returned.

    Returns a version object, undef if no dependencies could be found.

  • $p->minimum_syntax_version()

    The minimum_syntax_version method will explicitly test only the Document's syntax to determine it's minimum version, to the extent that this is possible.

    Returns a version object, undef if no dependencies could be found.

  • version_markers

    This method returns a list of pairs in the form:

      ($version, \@markers)
    

    Each pair represents all the markers that could be found indicating that the version was the minimum needed version. @markers is an array of strings. Currently, these strings are not as clear as they might be, but this may be changed in the future. In other words: don't rely on them as specific identifiers.

BENCHMARK

Perl::MinimumVersion::Fast is faster than Perl::MinimumVersion. Because Perl::MinimumVersion::Fast uses Compiler::Lexer, that is a Perl5 lexer implemented in C++. And Perl::MinimumVersion::Fast omits some features implemented in Perl::MinimumVersion.

But, but, Perl::MinimumVersion::Fast is really fast.

                            Rate Perl::MinimumVersion Perl::MinimumVersion::Fast
Perl::MinimumVersion       5.26/s                   --                       -97%
Perl::MinimumVersion::Fast  182/s                3365%                         --

LICENSE

Copyright (C) tokuhirom.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

THANKS TO

Most of documents are taken from Perl::MinimumVersion.

AUTHOR

tokuhirom [email protected]

SEE ALSO

This module using Compiler::Lexer as a lexer for Perl5 code.

This module is inspired from Perl::MinimumVersion.

perl-minimumversion-fast's People

Contributors

azawawi avatar briandfoy avatar charsbar avatar karenetheridge avatar tokuhirom avatar tux avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

perl-minimumversion-fast's Issues

Stacked comparison not yet detected

$ perlver-fast -ve'10 < $a < 40 and exit 0'
-e: 5.006 / 5.006

Stacked comparison was introduced in 5.31.10. If we exclude devel versions, it should report 5.32.0 instead of 5.006

$ perl5.30.3 -we '10 < $a < 42 and exit 0' (y|n|e|a)? yes
syntax error at -e line 1, near "$a <"
Execution of -e aborted due to compilation errors.
$ perl5.32.0 -we '10 < $a < 42 and exit 0'
Use of uninitialized value $a in numeric lt (<) at -e line 1.

should you not be perl 5.012004

package Perl::MinimumVersion::Fast;
use 5.008005;

as:

package Compiler::Lexer;
use 5.012004;

should you not be the same

are you not constrained by your dependencies :(

perlver CLI is missing \

Pipe usage is missing a \ so it checks code as being a file. Additionally you'' get an undefined warning on $_

--- perlver-fast    2020-12-07 10:51:32.525958806 +0100
+++ perlver-fast    2020-12-07 10:51:58.686278929 +0100
@@ -10,6 +10,6 @@ if (@ARGV) {
     }
 } else {
     my $src = join("", <>);
-    my $v = Perl::MinimumVersion::Fast->new($src);
-    printf "%s: %s\n", $_, $v->minimum_version;
+    my $v = Perl::MinimumVersion::Fast->new(\$src);
+    printf "%s: %s\n", "STDIN", $v->minimum_version;
 }

And as addition, I want support for -e :)
So my current version looks like

#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(:config bundling passthrough);
use Perl::MinimumVersion::Fast;

GetOptions (
    "e:s" => \my $expr,
    "v"   => \my $verbose,
    ) or die;

if (@ARGV) {
    report ($_, $_) for @ARGV;
    }
elsif ($expr) {
    report ("-e", \$expr);
    }
else {
    my $src = do { local $/; <> };
    report ("STDIN", \$src);
    }

sub report {
    my ($in, $src) = @_;
    my $v = Perl::MinimumVersion::Fast->new ($src);
    printf "%s: %s / %s\n", $in, $v->minimum_version, $v->minimum_syntax_version;
    $verbose or return;
    my @markers = $v->version_markers;
    while (@markers) {
        my ($pv, $m) = splice @markers, 0, 2;
        printf "%-10s %s\n", $pv, $_ for @$m;
        }
    }

Demo

$ perlver-fast -e '$b = ($a // "foo) =~ s{f\K}{i}r'
-e: 5.010 / 5.010
$ perlver-fast -ve '$b = ($a // "foo) =~ s{f\K}{i}r'
-e: 5.010 / 5.010
5.010      // operator

But it still does not report the use of \K and s{}{}r

Stacked file-test was introduced in 5.10.0

$ perlver-fast -e'-e -f -s ".exrc" and print "1\n"'
-e: 5.006 / 5.006

Stacked file-test was introduced in 5.10.0. It should report 5.10.0 instead of 5.006

$ perl5.8.9 -we '-e -f -s ".exrc" and print "1\n"'
Use of uninitialized value in -e at -e line 1.
$ perl5.10.0 -we '-e -f -s ".exrc" and print "1\n"'
1

Starting with Perl 5.14, "unshift" can take a scalar EXPR, which must hold a reference to an unblessed array.

This check tripped me up in Dist::Zilla::Plugin::Authority v1.008 when using Perl::MinimumVersion and I wanted to check this module just in case you reproduced the bug too.

Unfortunately, this module only fared better as it determined the following code's minimum is 5.008 when it actually should be 5.14! Perl::MinimumVersion did worse, returning 5.006 so please don't feel bad ;)

https://github.com/apocalypse/perl-dist-zilla-plugin-authority/blob/master/lib/Dist/Zilla/Plugin/Authority.pm#L239 which was

unshift $block->{children},
PPI::Token::Whitespace->new("\n"),
$self->_make_authority( $package ),
PPI::Token::Whitespace->new("\n");

I'm not sure if it is "easy" to check if $block->{children} is an arrayref or not? Maybe that's the reason why it failed to detect this? Thanks for looking into this!

Detects empty regex as defined-or

This file is said to require 5.010 because // operator:

use strict;
use warnings;

my $what = <>;
my @chars = split //, $what;
print "@chars\n";

requires Compiler::Lexer

This module requires Compiler::Lexer which requires a C++ compiler which is not available on many systems, esp if wanted on esoteric OS's like AIX or HP-UX.

As Compiler::Lexer gives no (easy) path to select a C++ compiler that is not in your $PATH (or is not recognized by default), the very useful module (Perl-MinimumVersion-Fast) cannot be used on systems where Compiler::Lexer cannot be built. Not your fault, but for sure annoying.

Hope you can find a working alternative that does not require C++ or G++

normal when should be 5.010 not 5.012

the first when is treated as postfix
the second when as normal

sample from http://perldoc.perl.org/5.14.1/functions/given.html

#!/usr/bin/env perl 
use v5.10;
given ($fruit) {
  when (/apples?/) {
    print "I like apples."
  }
  when (/oranges?/) {
    print "I don't like oranges."
  }
  default {
    print "I don't like anything"
  }
}

see for your self with following

#!/usr/bin/env perl 
use 5.012004;
use strict;
use warnings FATAL => 'all';

use English qw( -no_match_vars );    # Avoids reg-ex performance penalty
local $OUTPUT_AUTOFLUSH = 1;

use utf8;
use Carp::Always::Color;
use Compiler::Lexer;
use Data::Printer {caller_info => 1, colored => 1,};
use autodie qw(open close);

my $filename = $ARGV[0];

open my $fh, '<', $filename;
my $script = do { local $INPUT_RECORD_SEPARATOR; <$fh> };

# p $script;

my $lexer = Compiler::Lexer->new($filename);

# p $lexer;

my $tokens = $lexer->tokenize($script);
#p @tokens;
my @tokens = map { @$$_ } $tokens;

for my $i (0..@tokens-1) {
  my $token = $tokens[$i];
#p $token;

  if ($token->{name} eq 'WhenStmt') {
p $token;


    if (
      $i >= 1
      && ( $tokens[$i - 1]->{name} ne 'SemiColon'
        && $tokens[$i - 1]->{name} ne 'RightBrace')
      )
    {
      say 'postfix when => 5.012';
    }
    else {
      say 'normal when => 5.010';
    }
  }

}

#p $lexer->get_groups_by_syntax_level($$tokens, Compiler::Lexer::SyntaxType::T_Stmt);

## p $lexer->get_used_modules($script);

  exit(0);

__END__

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.