The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

standard - Enforce Standard Perl syntax with Guacamole

VERSION

version 0.007

SYNOPSIS

    use standard;
    # Now you will get a warning if you don't conform to Standard Perl

DESCRIPTION

Standard Perl aims to use only the syntax that makes Perl easy to parse. A Perl static parser such as Guacamole isn't that hard if we avoid a small set of constructs that cause parser ambiguities.

These changes are described below. Over time, this documentation will explain the standard itself versus the differences between this subset and what the perl interpreter supports.

DIFFERENCES

Things not supported

This list covers constructs that the perl interpreter understands (for whatever value of "understand" is) but that that Standard Perl does not support.

  • Auto-quoting

    Perl's auto-quoting rules are... rather elaborate and awkward. Much of it is unknown and can even depend on lowercase vs. uppercase and specific letters with special meaning to the interpreter and not the user.

    Thus, a string in Standard Perl is always quoted.

        $foo{key}   # not ok
        $foo{'key'} # ok
    
        %hash = ( foo   => 'bar' ); # not ok
        %hash = ( 'foo' => 'bar' ); # ok
  • HEREDOCs

    HEREDOCs are a monstrosity for parsers and cannot be expressed with a BNF. It is thus not supported.

        # This will fail
        my $value = << '_END_OF_VALUE';
        ...
        _END_OF_VALUE
    
        # This is an alternative
        my $value =
        q{...
        };
    
        # This is another alternative:
        my $value = q{
        ...
        } =~ s/^\n//r;
  • Indirect object notation

        my $instance = new Class;    # not ok
        my $instance = Class->new(); # ok
  • Bareword filehandles

        open FOO, ...    # not ok
        open my $fh, ... # ok
        open $fh, ...    # also ok
    
        print STDOUT $foo; # also ok
        print STDERR $foo; # also ok
    
        while ( <FOO>   ) {...} # not ok
        while ( <$foo>  ) {...} # ok
        while ( <STDIN> ) {...} # also ok

    The following bareword filehandles are supported:

    • STDIN

    • STDOUT

    • STDERR

    • ARGV

    • ARGVOUT

    • DATA

  • Printing to filehandles with no brace

        print $fh $foo;   # not ok
        print {$fh} $foo; # ok
  • _ in file operations

        if ( -f $foo && -r _ )    {...} # not ok
        if ( -f $foo && -r $foo ) {...} $ ok

    _ is an ambiguous bareword identifier. For example, using it in print is parsed different than when used with -r.

  • given / when / default

    Not supported.

Things we changed

The following are limitations that Standard Perl has which the perl interpreter doesn't.

Q-Like values delimiters

Q-Like values are one of the following: q, qq, qw, qx, qr

However, the following limitations also apply to: m//, s/// tr///, and y///.

  • No nested delimiters

        $val = q< <> >;    # not ok
        $val = q< \<\> >;  # ok

    If you want to use the delimiter within delimited space, escape it.

  • Limited delimiters

    Only the following delimiters are supported:

    (), [], {}, < >, //, !!, and ||.

        $val = q(...) # ok
        $val = q[...] # ok
        $val = q{...} # ok
        $val = q<...> # ok
        $val = q/.../ # ok
        $val = q!...! # ok
        $val = q|...| # ok
    
        $val = q@...@    # not ok
        $val = q#...#    # not ok
        $val = q Z ... Z # not ok
  • No spaces between before delimiters:

        q <foo> # not ok
        q<foo>  # ok
        q ()    # not ok
        q()     # ok

Subroutines

  • All subroutines must use parentheses

        foo $bar   # not ok
        foo($bar)  # ok

    There is an exception for methods:

        $foo->bar()         # ok
        $foo->bar           # also ok
    
        $foo->bar()->baz()  # ok
        $foo->bar->baz      # also ok
  • Subroutines can have attributes and signatures

    Standard Perl accepts both attributes and signatures.

  • All subroutine prototypes must be declared using an attribute

        sub foo ($)           {...} # signature, not prototype
        sub foo :prototype($) {...} # prototype, not signature
  • Prototypes do not change the parsing rules

        first {...} @foo         # not ok
        first( sub {...}, @foo ) # ok

    We are looking into allowing developers to have their grammars hooking up to the Guacamole parser so it could allow to extend Standard Perl. This will be useful for stuff like List::Util, Dancer2, Mojolicious::Lite, Moose, etc.

    Having said that, Standard Perl doesn't accept this.

Class names

  • Left of arrow is always an invocant, never a function

        Foo->new(); # always a class, never a function "Foo"

    This is tricky because the perl interpreter might see a function called foo in the same scope and call that instead. This would mean that Standard Perl and the perl interpreter would report different results.

    We have a shim layer in standard that checks for this and alerts if this will happen, so you never hit this issue when using standard.

    We advise other parsers who use Standard Perl BNF to include this part.

  • Namespaces cannot end with a double colon

        Foo->bar();   # ok
        Foo::->bar(); # not ok

    This might be changed.

Dereferencing

  • Prefixed dereferencing is only supported with braces

        @$foo    # not ok
        @{$foo}  # ok
        $foo->@* # ok

Expressions

  • map that attempts to return a pair must use parenthesis

        map {   $_ => 1   }, @foo  # not ok
        map { ( $_ => 1 ) }, @foo  # ok

Eval

  • eval only supports a block, not an expression

        eval { ... }   # ok
        eval " ... "   # not ok

SEE ALSO

Guacamole

AUTHORS

  • Sawyer X

  • Vickenty Fesunov

COPYRIGHT AND LICENSE

This software is Copyright (c) 2020 by Sawyer X.

This is free software, licensed under:

  The MIT (X11) License