Syntax::Construct - Explicitly state which non-feature constructs are used in the code.
Version 1.033
For some new syntactic constructs, there is the feature pragma. For the rest, there is Syntax::Construct.
use Syntax::Construct qw( // ... /r ); my $x = shift // 'default'; my $y = $x =~ s/de(fault)/$1/r; if ($y =~ /^fault/) { ... }
There are two subroutines (not exported) which you can use to query the lists of constructs programmatically: introduced and removed (see below).
introduced
removed
my @constructs = Syntax::Construct::introduced(); say "$_ was introduced in ", Syntax::Construct::introduced($_) for @constructs;
This module provides a simple way of specifying syntactic constructs that are not implemented via the feature pragma, but are still not compatible with older versions of Perl.
It's the programmer's responsibility to track the constructs and list them (but see Perl::MinimumVersion on how to extract the information from existing code).
Using use Syntax::Construct qw( // ); doesn't really change anything if you're running Perl 5.010+, but it gives much better error messages in older versions:
use Syntax::Construct qw( // );
Unsupported construct //
instead of
Search pattern not terminated
Three groups of people can benefit from the module:
The authors of the module using Syntax::Construct win, as they have all the constructs in one place (i.e. Syntax::Construct's documentation) and they don't waste their time searching through perldeltas and other places.
Users of their modules win as they get meaningful error messages telling them which Perl version they need to upgrade to.
The programmer they hired to work around the problem wins as they know what constructs to replace in the code to make it run in the ancient version.
Some programmers just use all the non-features their current Perl version provides without any notice. This leads to weird error messages in older Perl versions.
Some other programmers will place use 5.022; towards the top of the script, even if the only non-feature they use is the // operator available in 5.010 already. This prevents users of older versions of Perl to run the script, even if it would otherwise be easily possible.
use 5.022;
//
The kindest programmers will add use 5.010; # // towards the top of the script. But it means they have to remember or find out what version introduced the non-feature they use.
use 5.010; # //
Syntax::Construct liberates you from the need to remember all the non-features together with Perl versions that introduced them. It makes it easier for users of older Perl versions to migrate your code to their system. And finally, it improves the error messages they get.
Similarly, it's a good practice to keep specifying use feature qw{ postderef }; even if it's a no-op since 5.024: it makes your script available for people running older Perl versions. The same applies to use charnames in 5.016 and later, etc.
use feature qw{ postderef };
use charnames
Nothing. Using Syntax::Construct with no parameters is an error, giving it an empty list is a no-op (but you can then access the introduced and removed subroutines).
Without arguments, returns a list of all the supported constructs. With an argument, returns the version in which the given construct was introduced.
Same as introduced, but for removed constructs (e.g. auto-deref in 5.024).
See s-utf8-delimiters. The hack doesn't seem to work in 5.008 and older. Removed in 5.020.
"Recursive sort subs" in perl5100delta.
"Defined-or operator" in perl5100delta or "Logical Defined-Or" in perlop.
Alias: defined-or
"Recursive Patterns" under "Regular expressions" in perl5100delta or "(?PARNO) (?-PARNO) (?+PARNO) (?R) (?0)" in perlre.
Alias: regex-recursive-subpattern
"Named Capture Buffers" under "Regular expressions" in perl5100delta or "(?<NAME>pattern)" in perlre.
Alias: regex-named-capture-group
Not mentioned in any Delta. See (?|pattern) in "Extended Patterns" in perlre.
Alias: regex-reset-branch
"Possessive Quantifiers" under "Regular expressions" in perl5100delta or "Quantifiers" in perlre.
Aliases: regex-possessive-quantifier regex-possessive-match
"Backtracking control verbs" under "Regular expressions" in perl5100delta or "Special Backtracking Control Verbs" in perlre.
"\K escape" under "Regular expressions" in perl5100delta or "Look-Around Assertions" in perlre.
Alias: regex-keep-left
Covers \V and \H, too. See "Vertical and horizontal whitespace, and linebreak" under "Regular expressions" in perl5100delta or "Misc" in perlrebackslash.
\V
\H
Aliases: \H \V regex-generic-linebreak regex-horizontal-whitespace regex-vertical-whitespace
"Relative backreferences" under "Regular expressions" in perl5100delta or "Capture groups" in perlre.
Alias: regex-relative-backreference
"Default argument for readline()" in perl5100delta.
Alias: readline-argv
"Stacked filetest operators" in perl5100delta.
/p (preserve) modifier and ${^PREMATCH}, ${^MATCH} and ${^POSTMATCH} variables. Not mentioned in any Delta. See "Variables related to regular expressions" in perlvar.
/p
${^PREMATCH}
${^MATCH}
${^POSTMATCH}
Alias: regex-preserve-match-captures
"Lexical $_" in perl5100delta. Removed in 5.024.
Alias: lexical-default-variable
See "Byte-order-modifiers-for-pack()-and-unpack()" in perl5100delta
Alias: pack-byte-order-modifiers
"New package NAME VERSION syntax" in perl5120delta
"The ... operator" in perl5120delta or "The Ellipsis Statement" in perlsyn
Aliases: yada-yada triple-dot statement-ellipsis
"each, keys, values are now more flexible" in perl5120delta
"delete local" in perl5120delta
See the ninth bullet in "Other potentially incompatible changes" in perl5120delta.
"\N experimental regex escape" in perl5120delta.
Alias: regex-non-newline
readdir in a while-loop condition populates $_. Not mentioned in any delta, but see readdir in perlfunc.
readdir
$_
"Regular Expressions" in perl5140delta.
Alias: regex-use-default-modifiers
"Regular Expressions" in perl5140delta and "Modifiers" in perlre.
Aliases: non-destructive-subst non-destructive-substitution
Alias: regex-compile-as-default
Alias: regex-compile-as-locale
Aliases: regex-unicode-strings regex-compile-as-unicode-strings
Alias: regex-restrict-ascii-range
"Array and hash container functions accept references" in perl5140delta. See also push, pop, shift, unshift, splice, keys, values, and each in perlfunc. Removed in 5.024.
push
pop
shift
unshift
splice
keys
values
each
See New global variable ${^GLOBAL_PHASE} under "Other Enhancements" in perl5140delta.
Alias: global-phase
Alias: octal-escape
See package block syntax under "Syntactical Enhancements" in perl5140delta.
See srand() now returns the seed under "Other Enhancements" in perl5140delta.
See "Single-term-prototype" in perl5140delta.
See "use charnames is no longer needed for \N{name}" in perl5160delta.
"Computed Labels" in perl5180delta
See in "Selected Bug Fixes" in perl5180delta or each in perlfunc.
See [perl #105922] in "Selected Bug Fixes" in perl5180delta.
"subs now take a prototype attribute" in perl5200delta
Alias: attribute-prototype
"rand now uses a consistent random number generator" in perl5200delta. Note that on OpenBSD, Perl 5.020+ uses the system's own drand48 unless seeded.
drand48
"New slice syntax" in perl5200delta
Alias: hash-slice
"Unicode 6.3 now supported" in perl5200delta
Alias: unicode-6.3
See New \p{Unicode} regular expression pattern property in "Core Enhancements" in perl5200delta.
Alias: regex-property-unicode
See use locale now works on UTF-8 locales in "Core Enhancements" in perl5200delta.
See "Regular Expressions" in perl5200delta: in older Perl versions, a hack around was possible: to specify the delimiter twice in substitution. Use s-utf8-delimiters-hack if your code uses it.
s-utf8-delimiters-hack
Alias: wide-char-delimiters
"New double-diamond operator" in perl5220delta
Aliases: double-diamond operator-double-diamond
"New \b boundaries in regular expressions" in perl5220delta
Aliases: regex-unicode-grapheme-cluster-boundary regex-unicode-boundary regex-unicode-word-boundary regex-unicode-sentence-boundary regex-unicode-line-break-boundary \b{sb} \b{wb} \b{gcb}
"Non-Capturing Regular Expression Flag" in perl5220delta
Alias: regex-non-capturing
See Unicode 7.0 (with correction) is now supported in "Core Enhancements" in perl5220delta.
Alias: unicode-7.0
"New :const subroutine attribute" in perl5220delta
Alias: attribute-const
"fileno now works on directory handles" in perl5220delta
"Assignment to list repetition" in perl5220delta
Alias: list-repetition-assignment
"Floating point parsing has been improved" in perl5220delta
Alias: hexadecimal-floating-numbers
"Packing infinity or not-a-number into a character is now fatal" in perl5220delta
Alias: pack-inf
"List slices returning empty lists" in perl5220delta
See qr/foo/x now ignores all Unicode pattern white space in "Incompatible Changes" in perl5220delta.
Aliases: regex-x-unicode regex-x-handles-unicode
"Unicode 8.0 is now supported" in perl5240delta.
Alias: unicode-8.0
"New \b{lb} boundary in regular expressions" in perl5240delta.
"printf and sprintf now allow reordered precision arguments" in perl5240delta.
Aliases: printf-precision-argument-reorder sprintf-precision-argument-reorder
"Indented Here documents" in perl5260delta.
Alias: heredoc-indent
"New-regular-expression-modifier-/xx" in perl5260delta.
Alias: regex-xx
See @{^CAPTURE}, %{^CAPTURE}, and %{^CAPTURE_ALL} in perl5260delta.
@{^CAPTURE}
%{^CAPTURE}
%{^CAPTURE_ALL}
Alias: capture-variable
"Unicode 9.0 is now supported" in perl5260delta.
Alias: unicode-9.0
See "Use of \p{script} uses the improved Script_Extensions property" in perl5260delta.
See "scalar(%hash) return signature changed" in perl5260delta. Specifying this construct means the 5.026+ behaviour, i.e. scalar %hash returns the number of keys.
scalar %hash
Alias: scalar-hash
See "delete-on-key/value-hash-slices" in perl5280delta.
Alias: hash-delete-slice
See "Unicode 10.0 is supported" in perl5280delta.
Alias: unicode-10.0
See "Initialisation-of-aggregate-state-variables" in perl5280delta.
Aliases: state-array state-hash list-context-state
"Unicode 12.1 is supported" in perl5300delta
Alias: unicode-12.1
"Wildcards in Unicode property value specifications are now partially supported" in perl5300delta
"qr'\N{name}' is now supported" in perl5300delta
Alias: named-char-in-single-quoted-regex
See "Turkic UTF-8 locales are now seamlessly supported" in perl5300delta. Beware: the actual behaviour depends on the operating system's locale support. E.g. FreeBSD, DragonFly, and Solaris are known not to support it.
Not mentioned in any Delta. See "${^RE_COMPILE_RECURSION_LIMIT}" in perlvar.
Alias: re-compile-recursion-limit
"Unicode 13.0 is supported" in perl5320delta
Alias: unicode-13.0
"Chained comparisons capability" in perl5320delta
"New Unicode properties Identifier_Status and Identifier_Type supported" in perl5320delta
Alias: unicode-identifier-type
It is now possible to write \p{Name=...} in perl5320delta
"qr/{,n}/ is now accepted" in perl5340delta
Alias: empty-left-quantifier
"New octal syntax 0oddddd" in perl5340delta
Alias: octal-literals
"Blanks freely allowed within but adjacent to curly braces" in perl5340delta
"Unicode 14.0 is supported" in perl5360delta
"Unicode 15.0 is supported" in perldelta
"%{^HOOK} API introduced" in perldelta
Alias: keyword-hook
"Defined-or and logical-or assignment default expressions in signatures" in perldelta
"@INC Hook Enhancements and $INC and INCDIR" in perldelta
"Optimistic Eval in Patterns" in perldelta
Alias: optimistic-eval
"REG_INF has been raised from 65,536 to 2,147,483,647" in perldelta
"New regexp variable ${^LAST_SUCCESSFUL_PATTERN}" in perldelta
Only constructs not mentioned above are listed here, i.e. constructs that were introduced before 5.008001.
Removed in 5.022. See "Support for ?PATTERN? without explicit operator has been removed" in perl5220delta.
Removed in 5.022, not documented. Before that, if the first argument to shift, unshift, pop, push, splice, keys, values, and each was a global variable, it was possible to omit its sigil, e.g.
push arr, 12; # same as push @arr, 12
Removed in 5.018. See "qw(...)-can-no-longer-be-used-as-parentheses" in perl5180delta.
Removed in 5.012, but documented in 5.014. See "split()-and-@_" in perl5140delta.
Alias: split-populates-@_
Some features have been accepted in Perl (postderef and postderef_qq in 5.024, lexical_subs in 5.026). In the spirit of Syntax::Construct, you should still declare them, even if their usage has no effect in newer Perl versions to provide meaningful error messages to users of older versions.
postderef
postderef_qq
lexical_subs
E. Choroba, <choroba at cpan.org>
<choroba at cpan.org>
Gabor Szabo, JJ Merelo, tynovsky, Chris White, Mohammad S Anwar, Branislav Zahradnik
Please report any bugs or feature requests to the GitHub repository, see below.
In development versions of Perl, the removal of constructs is tested against the coming stable version -- e.g., 5.023 forbids all the removed constructs of 5.024. The behaviour of the module in such circumstances might still be, um, unstable.
You can find documentation for this module with the perldoc command.
perldoc Syntax::Construct
You can also look for information at:
GitHub Repository
https://github.com/choroba/syntactic-construct
Feel free to report issues and submit pull requests.
MetaCPAN, Open Source Search Engine for CPAN
https://metacpan.org/pod/Syntax::Construct
Search CPAN
http://search.cpan.org/dist/Syntax-Construct/
Perl::MinimumVersion, Perl::MinimumVersion::Fast
Copyright 2013 - 2023 E. Choroba.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install Syntax::Construct, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Syntax::Construct
CPAN shell
perl -MCPAN -e shell install Syntax::Construct
For more information on module installation, please visit the detailed CPAN module installation guide.