<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
	<title>The Effective Perler</title>
	<link>https://www.effectiveperlprogramming.com/</link>
	<description>Effective Perl Programming - write better, more idiomatic Perl</description>
	<language>en</language>
	<atom:link href="https://www.effectiveperlprogramming.com/feed.xml" rel="self" type="application/rss+xml"/>
	<item>
		<title>Perl v5.36 new features</title>
		<link>https://www.effectiveperlprogramming.com/2024/11/perl-v5-36-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/11/perl-v5-36-new-features/</guid>
		<pubDate>Fri, 15 Nov 2024 12:32:07 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; These items are in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Unicode 14&lt;/li&gt;
&lt;li&gt;&lt;code&gt;use v5.36&lt;/code&gt; now disables &lt;code&gt;indirect&lt;/code&gt; and &lt;code&gt;multidimensional&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/10/switch-removed-from-the-v5-36-feature-bundle/&quot;&gt;switch removed from v5.36 feature bundle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/09/know-if-something-is-a-boolean/&quot;&gt;Pure booleans&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/07/automatically-turn-on-warnings/&quot;&gt;&lt;code&gt;use v5.36&lt;/code&gt; enables warnings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;More paired delimiters&lt;/li&gt;
&lt;li&gt;&lt;code&gt;finally&lt;/code&gt; blocks&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt;subroutine signatures are no longer experimental&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@_&lt;/code&gt; is now experimental within signatured subs&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2020/01/use-the-infix-class-instance-operator/&quot;&gt;The &lt;code&gt;isa&lt;/code&gt; operator is no longer experimental&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/08/slurp-a-file-from-the-command-line-with-g/&quot;&gt;the &lt;code&gt;-g&lt;/code&gt; command-line flag is a shortcut for -0777&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/06/iterate-over-multiple-elements-at-the-same-time/&quot;&gt;experimental iteration of multiple values&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;defer&lt;/code&gt; blocks&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Goodbye fake multidimensional data structures</title>
		<link>https://www.effectiveperlprogramming.com/2024/11/goodbye-fake-multidimensional-data-structures/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/11/goodbye-fake-multidimensional-data-structures/</guid>
		<pubDate>Thu, 14 Nov 2024 10:21:39 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
Perl v5.36 disables by default a Perl 3 feature that you may have never seen, which I&#39;ll cover in the next section. In a single-element access to a hash, you were able to use a list of indices to simulate a multidimensional data structure. This was long before Perl had references (and shortly before Taylor Swift was born). Yet, in v5.34, this still works:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
my %hash;
$hash{1,3} = 7;  # modern way is $array[1][3] = 7;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In v5.36, this still works as long as you specify a minimum Perl version with &lt;code&gt;require&lt;/code&gt; instead of &lt;code&gt;use&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;require v5.36;
my %hash;
$hash{1,3} = 7;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With &lt;code&gt;use&lt;/code&gt;, though, Perl loads the appropriate feature bundle, which turns off this feature:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
my %hash;
$hash{1,3} = 7;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that the &lt;code&gt;multidimensional&lt;/code&gt; feature was removed from the list:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Multidimensional hash lookup is disabled at ...
Execution of ... aborted due to compilation errors.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This version also disables &lt;a href=&quot;/2024/10/switch-removed-from-the-v5-36-feature-bundle/&quot;&gt;&lt;code&gt;switch&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;/2020/06/turn-off-indirect-object-notation/&quot;&gt;&lt;code&gt;indirect&lt;/code&gt;&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Enable  &lt;code&gt;multidimensional&lt;/code&gt; explicitly if you want to restore it, although you should only do this for legacy code since the feature is likely to disappear completely:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use warnings;
use feature qw(multidimensional);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Perl 4&#39;s fake multidimensional hashes&lt;/h2&gt;


&lt;p&gt;
Perl 4 did not have references, so it didn&#39;t have a way to include an array in an array. However, you could fake it with a special hash
key convention that Perl stole from Awk.
&lt;/p&gt;


&lt;p&gt;
To make this work, you access a single element in the hash, but have a &quot;list&quot; of comma-separated index values as the hash key:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my %hash;
$hash{0,0} = &quot;a&quot;;
$hash{1,1} = &quot;b&quot;;

for( my $row = 0; $row &amp;lt;= 1; $row++ ) {
	for( my $col = 0; $col &amp;lt;= 1; $col++ ) {
		printf &#39;% 2s&#39;, $hash{$row,$col} // 0;
		}
	print &quot;\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This outputs your tiny matrix:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt; a 0
 0 b
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To see what Perl is doing, dump that hash. The  &lt;code&gt;$Useqq&lt;/code&gt; variable from &lt;a href=&quot;https://metacpan.org/pod/Data::Dumper&quot;&gt;Data::Dumper&lt;/a&gt; encodes unprintables as their ordinal values in octal:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$Data::Dumper::Useqq = 1;
say Dumper(\%hash);
&lt;/code&gt;&lt;/pre&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
		  &quot;0\0340&quot; =&gt; &quot;a&quot;,
		  &quot;1\0341&quot; =&gt; &quot;b&quot;
		};
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That  &lt;code&gt;\034&lt;/code&gt; is (U+001C INFORMATION SEPARATOR FOUR):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;printf &#39;% 2s&#39;, $hash{ &quot;$row\034$col&quot; } // 0;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl calls that  &lt;code&gt;$;&lt;/code&gt; (or  &lt;code&gt;$SUBSCRIPT_SEPARATOR&lt;/code&gt; or  &lt;code&gt;$SUBSEP&lt;/code&gt;). &lt;span class=&quot;person&quot; data-fullname=&quot;Larry Wall&quot;&gt;Larry&lt;/span&gt; had wanted to use  &lt;code&gt;$,&lt;/code&gt; but that was already the output field separator. You can use this value directly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;printf &#39;% 2s&#39;, $hash{ &quot;$row$;$col&quot; } // 0;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can mix the forms too. This example assigns with the comma notation but outputs it by constructing the key itself:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$row = 1;
$col = 3;

$hash{$row,$col} = 7;

printf &#39;% 2s&#39;, $hash{ &quot;$row$;$col&quot; } // 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Change the separator&lt;/h2&gt;


&lt;p&gt;
If &lt;code&gt;\034&lt;/code&gt; is something that might appear in your data, change the multidimensional separator to something that won&#39;t conflict:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;local $; = &quot;\000&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;From the Perl documentation&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://perldoc.perl.org/perlsyn&quot;&gt;perlsyn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://perldoc.perl.org/perlvar&quot;&gt;perlvar&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>switch removed from the v5.36 feature bundle</title>
		<link>https://www.effectiveperlprogramming.com/2024/10/switch-removed-from-the-v5-36-feature-bundle/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/10/switch-removed-from-the-v5-36-feature-bundle/</guid>
		<pubDate>Mon, 14 Oct 2024 07:31:13 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
We&#39;re almost at the end of the tragic saga of &lt;code&gt;given-when&lt;/code&gt;, a feature added in v5.10. &lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/&quot;&gt;It lost the lexical &lt;code&gt;$_&lt;/code&gt; in v5.24&lt;/a&gt; after that feature was demoted to experimental in v5.14.  Smart matching, almost the entire point of &lt;code&gt;given-when&lt;/code&gt;, has already been demoted to experimental status. Then, &lt;a href=&quot;/2017/12/beware-of-the-removal-of-when-in-perl-v5-28/&quot;&gt;v5.28 flirted with removing &lt;code&gt;given-when&lt;/code&gt;&lt;/a&gt; before deciding that was too hasty. It&#39;s been a while, and it&#39;s on to the next step.
&lt;/p&gt;



&lt;p&gt;
The latest humiliation is that the &lt;code&gt;switch&lt;/code&gt; feature (really &lt;code&gt;given-when&lt;/code&gt;) is no longer part of the list of the default features enabled by requiring a version of perl, as in &lt;code&gt;use v5.26;&lt;/code&gt;. 
&lt;/p&gt;



&lt;p&gt;
The feature is still there if you want to use it, but you need to explicitly enable it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use feature qw(switch);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, don&#39;t start using this feature now. You&#39;ve had plenty of time to fix it and you can &lt;a href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;get most of what you need using &lt;code&gt;for&lt;/code&gt; instead&lt;/a&gt;.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Know if something is a boolean</title>
		<link>https://www.effectiveperlprogramming.com/2024/09/know-if-something-is-a-boolean/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/09/know-if-something-is-a-boolean/</guid>
		<pubDate>Sun, 15 Sep 2024 07:31:52 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
So far, Perl has not had special boolean values. Perl doesn&#39;t even have a strictly true value; it has specified false values (&lt;code&gt;0&lt;/code&gt;, &lt;code&gt;&#39;0&#39;&lt;/code&gt;, empty string, and &lt;code&gt;undef&lt;/code&gt;), and everything that is not false is true. Not only that, by using strings or numbers (or undef) interchangeably and output stringifying everything, Perl can&#39;t tell how it should represent the value in formats that have particular ideas about booleans, but now there&#39;s something that can help with that.
&lt;/p&gt;



&lt;p&gt;
Through &lt;a href=&quot;https://metacpan.org/pod/builtin&quot;&gt;builtin&lt;/a&gt;, v5.36 adds &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; functions and a way for you to know that a value is a boolean. This allows you to represent these values correctly for other formats.
&lt;/p&gt;



&lt;p&gt;
Before you get too excited, Perl may have these features but your favorite libraries probably aren&#39;t using them yet. Some of those have special ways to represent these, and they probably aren&#39;t going to change since they have to support the old and new ways.
&lt;/p&gt;


&lt;h2&gt;A JSON example&lt;/h2&gt;


&lt;p&gt;
Consider this JSON, which has &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;{ &quot;updated&quot;: true, &quot;paid&quot;: false }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now look at what happens when you encode plain, non-special Perl values as JSON. Take the Perl true and false values from the results of some comparisons:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use JSON;

my $true  = (1==1);
my $false = (0==1);

my $data = { &quot;updated&quot; =&gt; $true, &quot;paid&quot; =&gt; $false };
my $json = encode_json($data);

say $json;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that Perl created both JSON values as strings, and the false value is the empty string. Perl dumbly stringifies everything as best it can since it doesn&#39;t know about fine-grained types:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;{&quot;updated&quot;:&quot;1&quot;,&quot;paid&quot;:&quot;&quot;}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This means that any external JSON parsers will not correctly find JSON&#39;s &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; values. The string &lt;code&gt;&quot;1&quot;&lt;/code&gt; is no more special than any other string:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ echo &#39;{&quot;updated&quot;:&quot;1&quot;,&quot;paid&quot;:&quot;&quot;}&#39; |⏎jq &#39;. | select(.updated == true)&#39;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With JSON&#39;s &lt;code&gt;true&lt;/code&gt; value, the filter works:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ echo &#39;{&quot;updated&quot;:true,&quot;paid&quot;:false}&#39; |⏎jq &#39;. | select(.updated == true)&#39;
{
  &quot;updated&quot;: true,
  &quot;paid&quot;: false
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl&#39;s &lt;a href=&quot;https://metacpan.org/pod/JSON&quot;&gt;JSON&lt;/a&gt; module solves this by using special references so it knows that there is a boolean value. Instead of Perl&#39;s squiggly true or false values, it uses references to either &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;0&lt;/code&gt;, which it then turns into special internal objects:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use JSON;

my $true  = \1;
my $false = \0;

my $data = { &quot;updated&quot; =&gt; $true, &quot;paid&quot; =&gt; $false };
my $json = encode_json($data);

say $json;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the output is what JSON expects:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;{&quot;updated&quot;:true,&quot;paid&quot;:false}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Going the other way, from JSON to a Perl structure, dumping the values shows how &lt;a href=&quot;https://metacpan.org/pod/JSON&quot;&gt;JSON&lt;/a&gt; uses special objects:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use JSON;

my $json = qq({ &quot;updated&quot;: true, &quot;paid&quot;: false });
my $data = decode_json($json);

use Data::Dumper;
say Dumper($data);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that &lt;a href=&quot;https://metacpan.org/pod/JSON&quot;&gt;JSON&lt;/a&gt; uses a special class, &lt;code&gt;JSON::PP::Boolean&lt;/code&gt;, where the object value is a reference to Perl&#39;s false or non-false values:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
	&#39;updated&#39; =&gt; bless( do{\(my $o = 1)}, &#39;JSON::PP::Boolean&#39; ),
	&#39;paid&#39; =&gt; bless( do{\(my $o = 0)}, &#39;JSON::PP::Boolean&#39; )
	};
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is one of the cures for people who want types. As long as the object can handle the behavior, you don&#39;t care what it actually is.
&lt;/p&gt;


&lt;h2&gt;true and false&lt;/h2&gt;


&lt;p&gt;
Through the new &lt;code&gt;builtin&lt;/code&gt;, Perl v5.36 adds the new functions &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;. These are &quot;distinguished&quot; boolean values because they are explicitly for boolean operations.  You can use these anywhere you&#39;d use any of Perl&#39;s false or non-false values. For example, in a &lt;code&gt;while&lt;/code&gt; conditional:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use builtin qw(true false);
use experimental qw(builtin);

while( true ) {
	state $count = 0;
	say $count;
	last if $count++ &gt; 3;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Under the hood, the &quot;distinguished&quot; boolean values have an extra &lt;code&gt;BOOL&lt;/code&gt; flag. In this program, the first two boolean values are the new special values and the last one is just a plain &lt;code&gt;&quot;1&quot;&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use builtin qw(true false);
use experimental qw(builtin);

use Devel::Peek;

my $from_function = true;
Dump($from_function);

my $from_eq = &#39;a&#39; eq &#39;b&#39;;
Dump($from_eq);

my $from_one = &quot;1&quot;;
Dump($from_one);
&lt;/code&gt;&lt;/pre&gt;

&lt;a href=&quot;https://metacpan.org/pod/Devel::Peek&quot;&gt;Devel::Peek&lt;/a&gt; shows that the first two values have a &lt;code&gt;BOOL&lt;/code&gt; flag. Compare those to the last dump, which does not have the boolean value:

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PVNV(0x7fcd72809850) at 0x7fcd74008b00
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,IsCOW,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1
  PV = 0x106f29ac3 &quot;1&quot; [BOOL PL_Yes]
  CUR = 1
  LEN = 0
SV = PVNV(0x7fcd72809990) at 0x7fcd74008a88
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,IsCOW,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x106f29ac5 &quot;&quot; [BOOL PL_No]
  CUR = 0
  LEN = 0
SV = PV(0x7fcbeb00b050) at 0x7fcbeb86e408
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0x600003bd9930 &quot;1&quot;\0
  CUR = 1
  LEN = 10
  COW_REFCNT = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Testing for a boolean&lt;/h2&gt;


&lt;p&gt;
The previous section showed that Perl now tracks a difference between distinguished booleans and the &quot;normal&quot; truthy values. Along with the new &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; values, &lt;a href=&quot;https://metacpan.org/pod/builtin&quot;&gt;builtin&lt;/a&gt; also provides &lt;code&gt;is_bool&lt;/code&gt; so you can tell the difference between these new values and &quot;normal&quot; values:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use builtin qw(true false is_bool);
use experimental qw(builtin);

my $from_function = true;
say is_bool($from_function) ? &#39;boolean&#39; : &#39;not boolean&#39;;

my $from_eq = &#39;a&#39; eq &#39;b&#39;;
say is_bool($from_eq) ? &#39;boolean&#39; : &#39;not boolean&#39;;

my $from_one = &quot;1&quot;;
say is_bool($from_one) ? &#39;boolean&#39; : &#39;not boolean&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;is_bool&lt;/code&gt; returns true for the first two, but false for the last one:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;boolean
boolean
not boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Maintaining boolean status&lt;/h3&gt;


&lt;p&gt;
If Perl can track boolean status, how would a value lose it? Or maybe, gain it? Comparisons return boolean values. Negation (and double negation) return distinguished booleans. Mathematical and string operations lose the &lt;code&gt;BOOL&lt;/code&gt; flag. Here&#39;s a program to demonstrate various situations:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use builtin qw(true false is_bool);
use experimental qw(builtin);

sub check ( $v ) { is_bool( $v ) ? &#39;boolean&#39; : &#39;not boolean&#39; }

my $format = &quot;%13s is %s\n&quot;;
printf $format, &quot;1&quot;,             check(    1 );
printf $format, &quot;0&quot;,             check(    0 );
printf $format, &quot;!1&quot;,            check(  ! 1 );
printf $format, &quot;!0&quot;,            check(  ! 0 );
printf $format, &quot;!!1&quot;,           check( !! 1 );
printf $format, &quot;!!0&quot;,           check( !! 0 );
printf $format, &quot;1 == 1&quot;,        check( 1 == 1 );
printf $format, &quot;&#39;a&#39; eq &#39;b&#39;&quot;,    check( &#39;a&#39; eq &#39;b&#39; );
printf $format, &quot;true&quot;,          check( true );
printf $format, &quot;false&quot;,         check( false );
printf $format, &quot;0 + true&quot;,      check( 0 + true );
printf $format, &quot;0 + false&quot;,     check( 0 + false );
printf $format, &quot;&#39;&#39; . true&quot;,     check( &#39;&#39; . true );
printf $format, &quot;&#39;&#39; . false&quot;,    check( &#39;&#39; . false );
printf $format, &quot;true == true&quot;,  check( true == true );
printf $format, &quot;true eq false&quot;, check( true eq false );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows what has the &lt;code&gt;BOOL&lt;/code&gt; flag and what doesn&#39;t:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;			1 is not boolean
			0 is not boolean
		   !1 is boolean
		   !0 is boolean
		  !!1 is boolean
		  !!0 is boolean
	   1 == 1 is boolean
   &#39;a&#39; eq &#39;b&#39; is boolean
		 true is boolean
		false is boolean
	 0 + true is not boolean
	0 + false is not boolean
	&#39;&#39; . true is not boolean
   &#39;&#39; . false is not boolean
 true == true is boolean
true eq false is boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/Perl/PPCs/blob/main/ppcs/ppc0008-sv-boolean-type.md&quot;&gt;PPC 8: Stable SV Boolean Type&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;From the Perl documentation&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://metacpan.org/pod/builtin&quot;&gt;builtin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/Perl/PPCs/pull/3&quot;&gt;PPC describing SV boolean semantics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Slurp a file from the command line with -g</title>
		<link>https://www.effectiveperlprogramming.com/2024/08/slurp-a-file-from-the-command-line-with-g/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/08/slurp-a-file-from-the-command-line-with-g/</guid>
		<pubDate>Thu, 15 Aug 2024 00:00:44 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
Perl v5.36 adds the &lt;code&gt;-g&lt;/code&gt; switch as a shortcut for &lt;code&gt;-0777&lt;/code&gt;, which undefines the input record separator so you can read an entire file  as a single string. This is often called &quot;slurping&quot;, and is useful when you need to process text that spans several lines.
&lt;/p&gt;


&lt;h2&gt;The input record separator&lt;/h2&gt;


&lt;p&gt;
The input record separator is the character (or characters) that Perl&#39;s line-input operator uses to determine when a line has ended. By default, that&#39;s a newline (U+000A), but you can use any string you like by setting &lt;code&gt;$/&lt;/code&gt; (&lt;code&gt;$INPUT_RECORD_SEPARATOR&lt;/code&gt;). Sometimes the form feed is a useful separator for multiline records:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$/ = &quot;\f&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
On the command line, the &lt;code&gt;-0&lt;/code&gt; switch is a quick way to set the value for &lt;code&gt;$/&lt;/code&gt;. Without a value, it uses the null byte, which is sometimes useful as a separator:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0 -e 1
BEGIN { $/ = &quot;\000&quot;; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
A number in octal or hexadecimal sets &lt;code&gt;$/&lt;/code&gt; to some other single character:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0014 -e 1
BEGIN { $/ = &quot;\f&quot;; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0xC -e 1
BEGIN { $/ = &quot;\f&quot;; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Any number above 0377 octal (more than 255 decimal) sets &lt;code&gt;$/&lt;/code&gt; to undef:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0377 -e 1
BEGIN { $/ = &quot;\377&quot;; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0400 -e 1
BEGIN { $/ = undef; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Conventionally, though, the Perl documentation has used &lt;code&gt;777&lt;/code&gt; as the value to get undef probably since it&#39;s easier to remember:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0777 -e 1
BEGIN { $/ = undef; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The new &lt;code&gt;-g&lt;/code&gt; is a short synonym for &lt;code&gt;-0777&lt;/code&gt;, so it does the same thing:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -e 1
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -g -e 1
BEGIN { $/ = undef; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Far more than you ever wanted to know&lt;/h2&gt;


&lt;p&gt;
The &lt;code&gt;-0&lt;/code&gt; switch has some other interesting behavior, and has a few other interesting features. Since I&#39;m already writing about this feature, I might as well keep going.
&lt;/p&gt;


&lt;h3&gt;Single-character line ending&lt;/h3&gt;


&lt;p&gt;
You can use an octal or hexadecimal number after the &lt;code&gt;-0&lt;/code&gt; to choose the single character that you want to use as the line ending. I&#39;ve often used the form feed (U+000C) to separate multi-line records. The particular character doesn&#39;t matter as long as it doesn&#39;t appear in the data (so the null byte might be useful too):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -le &quot;print qq(one\ntwo\nthree\n\fA\nB\nC\n\f9\n10\n11\n)&quot; &gt; formfeed.txt
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you read &lt;span class=&quot;filename&quot;&gt;formfeed.txt&lt;/span&gt; by lines with no change to the input record separator, you see the three records separated by &quot;blank&quot; lines, which are really the form feed:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -ne &#39;print&#39; formfeed.txt
one
two
three

A
B
C

9
10
11
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can see that easier when you replace the invisible characters with their ordinal values, which you do in octal here:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -pe &#39;s/(\P{Print})/sprintf(q(%03o),ord($1)) . &quot;\n&quot;/eg&#39; formfeed.txt
one012
two012
three012
014
A012
B012
C012
014
9012
10012
11012
012
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you use the octal value of the form feed for the number after the &lt;code&gt;-0&lt;/code&gt; switch and output lines surrounded by angle brackets, you get three lines (with the newlines and line-ending form feed intact):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -014 -ne &#39;print qq(&amp;lt;$_&gt;)&#39; formfeed.txt
&amp;lt;one
two
three

&gt;&amp;lt;A
B
C

&gt;&amp;lt;9
10
11

&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You could have also specified this with three digits, &lt;code&gt;-0014&lt;/code&gt;, or as hexadecimal with a leading &lt;code&gt;x&lt;/code&gt;, like &lt;code&gt;-0xC&lt;/code&gt;. The hexadecimal version is valuable when you need to specify a character past the largest single octet value you can get out of three octal digits, which is &lt;code&gt;0377&lt;/code&gt;.
&lt;/p&gt;



&lt;p&gt;
There&#39;s a catch though. If you want to set the input record separator to a wide character, you need to ensure that you read the input correctly. For the ☃ (U+2603 SNOWMAN) to be the separator, which takes up three octets in UTF-8, you need to read the input as UTF-8 too. The &lt;code&gt;-C&lt;/code&gt; is one way to do that:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -0x2603 -C -ne &#39;print qq(&amp;lt;$_&gt;)&#39; snowmen.txt &gt;&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You aren&#39;t able to specify multiple characters as a line separator since &lt;code&gt;perl&lt;/code&gt; thinks the extra characters are a file for input:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -0x0100x2603 -e
No Perl script found in input
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Slurping an entire file&lt;/h2&gt;


&lt;p&gt;
If you specify an octal value 400 or higher, which is more than 8 bits, Perl sets the input record separator to undef. With no defined value for &lt;code&gt;$/&lt;/code&gt;, Perl slurps the entire input. But, this is different than setting the empty string (a defined value), which I write about in the next section.
&lt;/p&gt;



&lt;p&gt;
You&#39;ve probably seen &lt;code&gt;-0777&lt;/code&gt;, perhaps the most common use of &lt;code&gt;-0&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -0777 -ne &#39;print qq(&amp;lt;$_&gt;)&#39; dog.txt
&amp;lt;Newfoundland
Golden Retreiver
Boxer
&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That &lt;span class=&quot;filename&quot;&gt;dog.txt&lt;/span&gt; is actually read through the &lt;code&gt;ARGV&lt;/code&gt; filehandle, which does some trickery to make it look like all the input is coming from one source. However, the line input operator can&#39;t read across the command line files; &lt;code&gt;perl&lt;/code&gt; figures out when one file is empty, closes it, then opens the next file. So, each file appears to be its own line:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -0777 -ne &#39;print qq(&amp;lt;$_&gt;)&#39;⏎dog.txt cat.txt lizard.txt
&amp;lt;Newfoundland
Golden Retreiver
Boxer
&gt;&amp;lt;Tabby
Marmalade
Tiger
&gt;&amp;lt;Monitor
Iguana
Godzilla
&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you wanted all the files to be one line, route them through standard input before they get to &lt;code&gt;perl&lt;/code&gt;. This only looks like a useless use of &lt;code&gt;cat&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% cat dog.txt cat.txt lizard.txt |⏎perl -0 -ne &#39;print qq(&amp;lt;$_&gt;)&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Paragraph mode&lt;/h2&gt;

&lt;p&gt;
&quot;Paragraph mode&quot; is a special case. The &lt;code&gt;-00&lt;/code&gt; sets the input record separator to the empty string. That&#39;s different than the undefined value even though both are false:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MO=Deparse -00 -e 1
BEGIN { $/ = &quot;&quot;; $\ = undef; }
&#39;???&#39;;
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When the input record separator is the empty string, &lt;code&gt;perl&lt;/code&gt; treats it as if it is multiple consecutive newlines. This has the same effect as if the input record separator were the pattern &lt;code&gt;\n+&lt;/code&gt;. Not only that, but it collapses the multiple newlines to exactly two newlines:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -00 -ne &#39;print qq(&amp;lt;$_&gt;)&#39; paras.txt
&amp;lt;First line first para
Second line first para
Third line first para

&gt;&amp;lt;After first blank line
Second line after first blank line
Third line after first blank line

&gt;&amp;lt;After 2nd blank line
2nd line after 2nd blank line
3rd line after 2nd blank line
&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Summary&lt;/h2&gt;


&lt;p&gt;
Here&#39;s a quick summary of the various incantations of the &lt;code&gt;-0&lt;/code&gt; switch:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;

&lt;tr&gt;&lt;th&gt;Switch&lt;/th&gt;&lt;th&gt;Input Record Separator&lt;/th&gt;&lt;th&gt;Note&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;\000&lt;/code&gt;&lt;/td&gt;&lt;td&gt;null byte&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-00&lt;/code&gt;&lt;/td&gt;&lt;td&gt;empty string, but &quot;\n+&quot;&lt;/td&gt;&lt;td&gt;paragraph mode&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0014&lt;/code&gt;&lt;/td&gt;&lt;td&gt;8-bit character, in octal&lt;/td&gt;&lt;td&gt;form feed&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0xC&lt;/code&gt;&lt;/td&gt;&lt;td&gt;8-bit character, in hex&lt;/td&gt;&lt;td&gt;form feed&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0400&lt;/code&gt;&lt;/td&gt;&lt;td&gt;undef, above 8-bit&lt;/td&gt;&lt;td&gt;slurp&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0777&lt;/code&gt;&lt;/td&gt;&lt;td&gt;undef, idiomatic&lt;/td&gt;&lt;td&gt;slurp&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-g&lt;/code&gt;&lt;/td&gt;&lt;td&gt;undef&lt;/td&gt;&lt;td&gt;slurp, new in v5.36&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0x1FF&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;\777&lt;/code&gt; character, include &lt;code&gt;-C&lt;/code&gt;&lt;/td&gt;&lt;td&gt;actual &lt;code&gt;\777&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0x2603&lt;/code&gt;&lt;/td&gt;&lt;td&gt;wide character, include &lt;code&gt;-C&lt;/code&gt;&lt;/td&gt;&lt;td&gt;snowman&lt;/td&gt;&lt;/tr&gt;

&lt;/table&gt;

&lt;h2&gt;From the Perl documentation&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://perldoc.perl.org/perlrun&quot;&gt;perlrun&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Automatically turn on warnings</title>
		<link>https://www.effectiveperlprogramming.com/2024/07/automatically-turn-on-warnings/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/07/automatically-turn-on-warnings/</guid>
		<pubDate>Mon, 22 Jul 2024 19:10:29 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;


&lt;hr/&gt;



&lt;p&gt;
Perl v5.36 automatically turns on &lt;code&gt;warnings&lt;/code&gt; when you specify the minimum Perl version with &lt;code&gt;use&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;  # use warnings for free
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since this form has been turning on strictures since v5.12 (&lt;a href=&quot;/2010/08/implicitly-turn-on-strictures-with-perl-5-12/&quot;&gt;Implicitly turn on strictures with Perl 5.12&lt;/a&gt;), you no longer have to specify warnings or strict at the top of your program.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
If you don&#39;t want warnings but still need to enforce a minimum version, use &lt;code&gt;require&lt;/code&gt; (&lt;a href=&quot;/2010/07/watch-out-for-side-effects-with-use-version/&quot;&gt;Watch out for side effects with &lt;code&gt;use VERSION&lt;/code&gt;&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;require v5.36;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, with &lt;code&gt;require&lt;/code&gt;, you miss out on all the other features that &lt;code&gt;use&lt;/code&gt; would turn on automatically. Instead, you could turn off warnings explicitly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
no warnings;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But beware of the modules that re-enable warnings for you. Although this is unrelated to what v5.36 is doing, I&#39;m often caught out because I thought I turned off warnings (and I did), but I still get the warnings:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;no warnings;
use Mojo::Base;  # warnings back on!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Part of &lt;a href=&quot;https://metacpan.org/pod/Mojo::Base&quot;&gt;Mojo::Base&lt;/a&gt;&#39;s job is to enable all the things that you probably want, including warnings. Since I tend to specify pragmas first followed by the modules, I end up with this ordering problem.
&lt;/p&gt;


&lt;h2&gt;Breaking -X&lt;/h2&gt;


&lt;p&gt;
The &lt;code&gt;-X&lt;/code&gt; command-line switch should suppress all warnings, which makes it a good choice for running &lt;code&gt;perl&lt;/code&gt; in production. In that case, you should be warning free, and if you aren&#39;t, you don&#39;t want to fill up your logs with messages you aren&#39;t monitoring.
&lt;/p&gt;



&lt;p&gt;
However, if you implicitly enable warnings as part of the feature bundle, you still get warnings (&lt;a href=&quot;https://github.com/Perl/perl5/issues/21427&quot;&gt;GitHub #21427&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.36.1 -Mwarnings -X -E &#39;say 1 + &quot;a&quot;&#39;
1
&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.36.1 -Mv5.36 -X -E &#39;say 1 + &quot;a&quot;&#39;
Argument &quot;a&quot; isn&#39;t numeric in addition (+) at -e line 1.
1
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Updating the feature bundle&lt;/h2&gt;


&lt;p&gt;
This release of Perl is the first update to the feature bundle since
v5.28. See what each feature bundle enables with &lt;code&gt;feature::features_enabled()&lt;/code&gt;. Here&#39;s what you get for free with &lt;code&gt;use v5.28&lt;/code&gt; (and up to v5.34):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mv5.28 -e &#39;use feature qw(:5.28);⏎BEGIN { say join qq(\n), feature::features_enabled() }&#39;
bareword_filehandles
bitwise
current_sub
evalbytes
fc
indirect
multidimensional
postderef_qq
say
state
switch
unicode_eval
unicode_strings
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s the list for v5.36:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mv5.36 -e &#39;use feature qw(:5.36);⏎BEGIN { say join q(\n), feature::features_enabled() }&#39;
bareword_filehandles
bitwise
current_sub
evalbytes
fc
isa
postderef_qq
say
signatures
state
unicode_eval
unicode_strings
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice the differences: &lt;code&gt;indirect&lt;/code&gt; is missing, &lt;code&gt;isa&lt;/code&gt; is now enabled, &lt;code&gt;multidimensional&lt;/code&gt; is missing, and &lt;code&gt;switch&lt;/code&gt; is missing.
&lt;/p&gt;



&lt;p&gt;
You might like a &lt;a href=&quot;https://gist.github.com/briandfoy/0aebbe60165c5cc62a43613092275f7e&quot;&gt;simple program I made&lt;/a&gt; to show in a spreadsheet all of the versions of Perl and which features their version enables.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Iterate over multiple elements at the same time</title>
		<link>https://www.effectiveperlprogramming.com/2024/06/iterate-over-multiple-elements-at-the-same-time/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/06/iterate-over-multiple-elements-at-the-same-time/</guid>
		<pubDate>Sat, 15 Jun 2024 18:56:16 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-36</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This feature was promoted to a stable version in &lt;a href=&quot;/2024/05/perl-v5-40-new-features/&quot;&gt;v5.40&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.36 adds experimental support that allows a &lt;code&gt;foreach&lt;/code&gt; (or &lt;code&gt;for&lt;/code&gt;) to iterate over multiple values at the same time by specifying multiple control variables. This is incredibly cool:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use experimental qw(for_list);

my @animals = qw( Buster Mimi Ginger Nikki );
foreach my( $s, $t ) ( @animals ) {
	say &quot;$s ^^^ $t&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows two iterations of the loop, each of which grabbed two values from the list:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster ^^^ Mimi
Ginger ^^^ Nikki
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Add another parameter; the list now doesn&#39;t divide evenly between the parameters, so any parameter that can&#39;t match with a list item gets &lt;code&gt;undef&lt;/code&gt;, just like normal list assignment:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use experimental qw(for_list);

foreach my( $s, $t, $u ) ( @animals ) {
	say &quot;$s ^^^ $t ^^^ $u&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since &lt;a href=&quot;/2024/07/automatically-turn-on-warnings/&quot;&gt;&lt;code&gt;use v5.36&lt;/code&gt; also turns on warnings&lt;/a&gt;, you get those &quot;uninitialized&quot; warnings for free when you use those &lt;code&gt;undef&lt;/code&gt; values:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster ^^^ Mimi ^^^ Ginger
Nikki ^^^  ^^^
Use of uninitialized value ...
Use of uninitialized value ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Another interesting use combines the new &lt;code&gt;builtin::indexed&lt;/code&gt; feature that gets you the index and value at the same time:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;
use experimental qw(for_list builtin);
use builtin qw(indexed);

my @animals = qw( Buster Mimi Ginger Nikki );
foreach my( $i, $value ) ( indexed(@animals) ) {
	say &quot;$i: $value&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s a bit nicer than going through the indices to access the value in an additional statement:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;foreach my $i ( 0 .. $#animals ) {
	my $value = $animals[$i];
	say &quot;$i: $value&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;No placeholders (yet)&lt;/h2&gt;


&lt;p&gt;
So far, this new syntax doesn&#39;t have a way to skip values. In a normal list assignment, you discard a value coming from the right hand list with a literal &lt;code&gt;undef&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $s, undef, $t ) = @animals
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Try that in the &lt;code&gt;for&lt;/code&gt; list and you get a syntax error:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;foreach my( $s, undef, $u ) ( @animals ) {  # ERROR!
	say &quot;$s ^^^ $u&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Hash keys and values&lt;/h2&gt;


&lt;p&gt;
I&#39;m tempted to use this for hashes, although &lt;code&gt;each&lt;/code&gt; inside a &lt;code&gt;while&lt;/code&gt; is still probably better since it doesn&#39;t have to build the entire input list in one go:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use experimental qw(for_list);

my %animals = (
	cats =&gt; [ qw( Buster Mimi Ginger ) ],
	dogs =&gt; [ qw( Nikki ) ],
	);

foreach my( $k, $v ) ( %animals ) {
	say &quot;$k ^^^ @$v&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since those hash values are array refs, it would be helpful if this feature could use the &lt;code&gt;refaliasing&lt;/code&gt; and &lt;code&gt;declared_refs&lt;/code&gt; features (&lt;a href=&quot;/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/&quot;&gt;Mix assignment and reference aliasing with declared_refs&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use experimental qw(for_list);
use experimental qw(refaliasing declared_refs);

my %animals = (
	cats =&gt; [ qw( Buster Mimi Ginger ) ],
	dogs =&gt; [ qw( Nikki ) ],
	);

foreach my( $k, \@v ) ( %animals ) {
	say &quot;$k ^^^ @v&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Sadly, the parser doesn&#39;t expect the reference operator inside that &lt;code&gt;for&lt;/code&gt; list:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;syntax error ... near &quot;, \&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Doing multiple things in a go&lt;/h2&gt;


&lt;p&gt;
Prior to builtin multiple iteration, the best way to do the same thing was probably the &lt;a href=&quot;https://metacpan.org/pod/List::MoreUtils&quot;&gt;List::MoreUtils&lt;/a&gt; (not part of core) module. The &lt;code&gt;natatime&lt;/code&gt; function, which I wished was named &lt;code&gt;n_at_a_time&lt;/code&gt;, grabs the number of elements that you specify and returns them as a list. Since it returns a list instead of an array reference, it&#39;s easier to use it with a &lt;code&gt;while&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use List::MoreUtils qw(natatime);

my @x = (&#39;a&#39; .. &#39;g&#39;);
my $iterator = natatime 3, @x;

while( my @vals = $iterator-&gt;() ) {
	print &quot;@vals\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Another approach uses &lt;code&gt;splice&lt;/code&gt;. The easiest thing might be to do it destructively since that requires no index fiddling:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @x = &#39;a&#39; .. &#39;g&#39;;
my @temp = @x;

while( my @vals = splice @temp, 0, 3, () ) {
	print &quot;@vals\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s an example from the &lt;a href=&quot;https://perldoc.perl.org/functions/splice&quot;&gt;splice&lt;/a&gt; documentation that does the same thing:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub nary_print {
  my $n = shift;
  while (my @next_n = splice @_, 0, $n) {
	say join q{ -- }, @next_n;
  }
}

nary_print(3, qw(a b c d e f g h));
# prints:
#   a -- b -- c
#   d -- e -- f
#   g -- h
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Playing with the array indices can get this done, but it comes with a lot of baggage. First, an array slice doesn&#39;t return an empty list, so you can&#39;t use that as a condition in the &lt;code&gt;while&lt;/code&gt; as in the previous examples. Since it fills in the missing elements with &lt;code&gt;undef&lt;/code&gt;, outputting the values possibly comes with warnings. Even if you want to accept those annoyances, you still have to manage the end of array condition (&lt;code&gt;$#X&lt;/code&gt;) yourself:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @x = &#39;a&#39; .. &#39;g&#39;;

my $start = 0;
my $n     = 3;

while( $start &amp;lt;= $#x ) {
	no warnings qw(uninitialized);
	my @vals = @x[$start .. $start + $n - 1];
	print &quot;@vals\n&quot;;
	$start += $n;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
So yeah, having a multiple iterator feature built into Perl is a huge win.
&lt;/p&gt;


&lt;h2&gt;Summary&lt;/h2&gt;


&lt;p&gt;
The experimental &lt;code&gt;for_list&lt;/code&gt; feature lets you take multiple elements of the list in each iteration. This doesn&#39;t yet handle many of the list assignment features that would make this as useful as people will want it to be.
&lt;/p&gt;


&lt;h2&gt;From the Perl documentation&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://perldoc.perl.org/perlsyn&quot;&gt;perlsyn&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description>
	</item>
	<item>
		<title>Perl v5.40 New Features</title>
		<link>https://www.effectiveperlprogramming.com/2024/05/perl-v5-40-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/05/perl-v5-40-new-features/</guid>
		<pubDate>Wed, 22 May 2024 20:08:34 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-40</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; These items are in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;code&gt;builtin&lt;/code&gt; adds &lt;code&gt;nan&lt;/code&gt; and &lt;code&gt;inf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-M&lt;/code&gt; command-line switch can now be followed by whitespace&lt;/li&gt;
&lt;li&gt;importing from an unloaded package name now warns&lt;/li&gt;
&lt;li&gt;lexical subroutines get some fixes&lt;/li&gt;
&lt;li&gt;The logical-xor operator &lt;code&gt;^^&lt;/code&gt; joins &lt;code&gt;&amp;&amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;finally&lt;/code&gt; blocks&lt;/li&gt;
&lt;li&gt;improved uninitialized value warnings&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Insignificant whitespace in brace constructs</title>
		<link>https://www.effectiveperlprogramming.com/2024/03/insignificant-whitespace-in-brace-constructs/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2024/03/insignificant-whitespace-in-brace-constructs/</guid>
		<pubDate>Mon, 18 Mar 2024 07:31:21 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-34</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;




&lt;hr/&gt;


&lt;p&gt;
Perl&#39;s coterie of brace constructs becomes a bit more lenient in v5.34. These things appear in double-quotish constructs, such as &lt;code&gt;\N{CHARNAME}&lt;/code&gt; to specify a character by name. And, patterns count as a double-quoted construct (unless you use &lt;code&gt;&#39;&lt;/code&gt; as the delimiter), so these new rules apply to brace constructs such as &lt;code&gt;\k{}&lt;/code&gt; (for named backreferences) and the general quantifier, &lt;code&gt;{n,m}&lt;/code&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;Specifying characters&lt;/h2&gt;


&lt;p&gt;
These constructs apply to double-quotish interpretation to specify a character by its codepoint or name:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;
&lt;th&gt;Construct&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Item&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\N{CHARNAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Character name&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2011/12/loose-match-unicode-character-names/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;&lt;code&gt;\o{177}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Octal code point&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2010/10/specify-any-character-by-its-octal-ordinal-value/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;&lt;code&gt;\x{ABCD}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Hex code point&lt;/td&gt;&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/table&gt;


&lt;p&gt;
There are already loose names for &lt;code&gt;\N{}&lt;/code&gt; that ignore whitespace (&lt;a href=&quot;/2011/12/loose-match-unicode-character-names/&quot;&gt;Loose match Unicode character names&lt;/a&gt;), but this feature is a bit different. It ignores horizontal whitespace around a value (but not inside a value):
&lt;/p&gt;



&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Spade suit: \N{ BLACK SPADE SUIT }
	Octal:      \o{ 23140 }
	Hex:        \x{ 2660 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This outputs the character we expect:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.34.0 whitespace.pl
Spade suit: ♠
Octal:      ♠
Hex:        ♠
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you add space within the value, you don&#39;t get the character you want (the &lt;code&gt;\N{}&lt;/code&gt; will actually fail):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 40 }
	Hex:      \x{ 26 60 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This discards the cruft once it encounters non-digit characters (just like Perl&#39;s string-to-number conversions). This is effectively:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 }
	Hex:      \x{ 26 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s even worse. You can add extra nonsense after the code number and v5.34 will ignore it. Although these have illegal digits (along with the internal space), they still work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);
use warnings;

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 abc }
	Hex:      \x{ 26 xyz }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With trailing tabs or spaces, &lt;a href=&quot;https://perldoc.perl.org/warnings&quot;&gt;warnings&lt;/a&gt; says that it ignores the cruft and uses what it received so far:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Non-octal character &#39; &#39; terminates \o early.  Resolved as &quot;\o{231}&quot; at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With leading tabs or spaces, earlier Perls give up right away and use the null character. The warning from v5.32 is this:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Non-octal character &#39; &#39; terminates \o early.  Resolved as &quot;\o{000}&quot; at...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Finally, the whitespace can&#39;t be vertical space or other double-quote escapes (it&#39;s just literal tabs or spaces). These don&#39;t work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;\o{\t231}
\o{
	231 }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In regular expressions, this fails before Perl interprets the pattern, where the &lt;code&gt;/x&lt;/code&gt; would be able to handle the vertical whitespace. This would match a null byte because the string-to-number parsing stops at the first newline, returning &lt;code&gt;\000&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;m/\o{
	231
	}/x;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;In regular expressions&lt;/h2&gt;


&lt;p&gt;
And these constructs apply to regular expression features, and you don&#39;t need the &lt;code&gt;/x&lt;/code&gt; flag to get this new, insignificant whitespace:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;&lt;th&gt;Construct&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Item&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\b{TYPE}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Word boundary&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\g{N}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Numbered backreference&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\g{NAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Named backreference&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\k{NAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Named backreference&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\p{PROPNAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Unicode property name&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\P{PROPNAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Unicode property name&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;\x{ABCD}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Hex code point&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;{n,m}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;general quantifier&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2023/11/up-to-n-matches/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;


&lt;p&gt;
The rules for these are similar to those from the previous section. Perl ignores the tabs or spaces at the beginning
or the end, but not in the middle (aside from around the &lt;code&gt;,&lt;/code&gt; in &lt;code&gt;{n,m}&lt;/code&gt;). For example, these all work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use open qw(:std :utf8);
use warnings;

$_ = &#39;aa&#39;;

my @patterns = (
	qr/(.)\g{ -1 }/,
	qr/(?&amp;lt;first&gt;.)\g{ first }/,
	qr/(?&amp;lt;first&gt;.)\k{ first }/,
	qr/\b{ sb }(.)/,
	qr/(\o{ 141 })\g{ -1 }/,
	qr/(\p{Letter})\g{ -1 }/,
	qr/(.)\g{ -1 }/,
	qr/(\x{ 61 })\g{ -1 }/,
	);

foreach my $pattern ( @patterns ) {
	say /$pattern/
	};
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Insignificant leading or trailing whitespace in brace constructs</title>
		<link>https://www.effectiveperlprogramming.com/2023/12/insignificant-leading-or-trailing-whitespace-in-brace-constructs/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2023/12/insignificant-leading-or-trailing-whitespace-in-brace-constructs/</guid>
		<pubDate>Mon, 04 Dec 2023 15:02:17 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-34</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
Perl&#39;s coterie of brace constructs becomes a bit more lenient in v5.34. These things appear in double-quotish constructs, such as &lt;code&gt;\N{CHARNAME}&lt;/code&gt; to specify a character by name. And, patterns count as a double-quoted construct (unless you use &lt;code&gt;&#39;&lt;/code&gt; as the delimiter), so these new rules apply to brace constructs such as &lt;code&gt;\k{}&lt;/code&gt; (for named backreferences) and the general quantifier, &lt;code&gt;{n,m}&lt;/code&gt;.
&lt;/p&gt;


&lt;h2&gt;Specifying characters&lt;/h2&gt;


&lt;p&gt;
These constructs apply to double-quotish interpretation to specify a character by its codepoint or name:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;
&lt;th&gt;Construct&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Item&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\N{CHARNAME}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Character name&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2011/12/loose-match-unicode-character-names/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;&lt;code&gt;\o{177}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Octal code point&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;/2010/10/specify-any-character-by-its-octal-ordinal-value/&quot;&gt;item&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;&lt;code&gt;\x{ABCD}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Hex code point&lt;/td&gt;&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/table&gt;


&lt;p&gt;
There are already loose names for &lt;code&gt;\N{}&lt;/code&gt; that ignore whitespace (&lt;a href=&quot;/2011/12/loose-match-unicode-character-names/&quot;&gt;Loose match Unicode character names&lt;/a&gt;), but this feature is a bit different. It ignores horizontal whitespace around a value (but not inside a value):
&lt;/p&gt;



&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Spade suit: \N{ BLACK SPADE SUIT }
	Octal:      \o{ 23140 }
	Hex:        \x{ 2660 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This outputs the character we expect:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.34.0 whitespace.pl
Spade suit: ♠
Octal:      ♠
Hex:        ♠
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you add space within the value, you don&#39;t get the character you want (the &lt;code&gt;\N{}&lt;/code&gt; will actually fail):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 40 }
	Hex:      \x{ 26 60 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This discards the cruft once it encounters non-digit characters (just like Perl&#39;s string-to-number conversions). This is effectively:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 }
	Hex:      \x{ 26 }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s even worse. You can add extra nonsense after the code number and v5.34 will ignore it. Although these have illegal digits (along with the internal space), they still work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.34;
use open qw(:std :utf8);
use warnings;

say &amp;lt;&amp;lt;~&quot;HERE&quot;;
	Octal:    \o{ 231 abc }
	Hex:      \x{ 26 xyz }
	HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With trailing tabs or spaces, &lt;a href=&quot;https://perldoc.perl.org/warnings&quot;&gt;warnings&lt;/a&gt; says that it ignores the cruft and uses what it received so far:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Non-octal character &#39; &#39; terminates \o early.  Resolved as &quot;\o{231}&quot; at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With leading tabs or spaces, earlier Perls give up right away and use the null character. The warning from v5.32 is this:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Non-octal character &#39; &#39; terminates \o early.  Resolved as &quot;\o{000}&quot; at...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Finally, the whitespace can&#39;t be vertical space or other double-quote escapes (it&#39;s just literal tabs or spaces). These don&#39;t work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;\o{\t231}
\o{
	231 }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In regular expressions, this fails before Perl interprets the pattern, where the &lt;code&gt;/x&lt;/code&gt; would be able to handle the vertical whitespace. This would match a null byte because the string-to-number parsing stops at the first newline, returning &lt;code&gt;\000&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;m/\o{
	231
	}/x;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;In regular expressions&lt;/h2&gt;


&lt;p&gt;
And these constructs apply to regular expression features, and you don&#39;t need the &lt;code&gt;/x&lt;/code&gt; flag to get this new, insignificant whitespace:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;&lt;tr&gt;&lt;th&gt;Construct     &lt;/th&gt;&lt;th&gt; Description           &lt;/th&gt;&lt;th&gt; Chapter
&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\b{TYPE}&lt;/code&gt;      &lt;/td&gt;&lt;td&gt; Word boundary         &lt;/td&gt;&lt;td&gt; &lt;a href=&quot;/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/&quot;&gt;Item&lt;/a&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\g{N}&lt;/code&gt;         &lt;/td&gt;&lt;td&gt; Numbered backreference&lt;/td&gt;&lt;td&gt;  Item 31 (book)
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\g{NAME}&lt;/code&gt;      &lt;/td&gt;&lt;td&gt; Named backreference   &lt;/td&gt;&lt;td&gt;  Item 31 (book)
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\k{NAME}&lt;/code&gt;      &lt;/td&gt;&lt;td&gt; Named backreference   &lt;/td&gt;&lt;td&gt; Item 31 (book)
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\p{PROPNAME}&lt;/code&gt;  &lt;/td&gt;&lt;td&gt; Unicode property name &lt;/td&gt;&lt;td&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\P{PROPNAME}&lt;/code&gt;  &lt;/td&gt;&lt;td&gt; Unicode property name &lt;/td&gt;&lt;td&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;\x{ABCD}&lt;/code&gt;      &lt;/td&gt;&lt;td&gt; Hex code point        &lt;/td&gt;&lt;td&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;{n,m}&lt;/code&gt;        &lt;/td&gt;&lt;td&gt; general quantifier    &lt;/td&gt;&lt;td&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;/table&gt;


&lt;p&gt;
The rules for these are similar to those from the previous section. Perl ignores the tabs or spaces at the beginning
or the end, but not in the middle (aside from around the &lt;code&gt;,&lt;/code&gt; in &lt;code&gt;{n,m}&lt;/code&gt;). For example, these all work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use open qw(:std :utf8);
use warnings;

$_ = &#39;aa&#39;;

my @patterns = (
	qr/(.)\g{ -1 }/,
	qr/(?&amp;lt;first&gt;.)\g{ first }/,
	qr/(?&amp;lt;first&gt;.)\k{ first }/,
	qr/\b{ sb }(.)/,
	qr/(\o{ 141 })\g{ -1 }/,
	qr/(\p{Letter})\g{ -1 }/,
	qr/(.)\g{ -1 }/,
	qr/(\x{ 61 })\g{ -1 }/,
	);

foreach my $pattern ( @patterns ) {
	say /$pattern/
	};
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>&quot;Up to N&quot; matches</title>
		<link>https://www.effectiveperlprogramming.com/2023/11/up-to-n-matches/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2023/11/up-to-n-matches/</guid>
		<pubDate>Tue, 28 Nov 2023 07:50:51 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-34</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;p&gt;
Perl&#39;s general regex quantifier, &lt;code&gt;{n,m}&lt;/code&gt;, takes a minimum and maximum number of matches. If you leave out the maximum number, like &lt;code&gt;{n,}&lt;/code&gt;, you have to match the preceding thing at least &lt;i&gt;n&lt;/i&gt; times but as many times as it can match: the maximum is unbounded.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Before v5.34, the converse &lt;code&gt;{,n}&lt;/code&gt; was not legal syntax. Now it is. You can match zero to &lt;i&gt;n&lt;/i&gt; of the preceding thing. It&#39;s still optional (zero matched), but if it&#39;s there, this will consume up to &lt;i&gt;n&lt;/i&gt; of them.
&lt;/p&gt;



&lt;p&gt;
Consider these examples. First, quantify &lt;i&gt;a&lt;/i&gt; to match 0 to 5 times. This matches every string because every string has at least zero &lt;i&gt;a&lt;/i&gt;s (a trick question we have early on in &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;/a{,5}/
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is more useful when it follows or is anchored by something else that must match:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;/ba{,5}/    # b optionally followed by up to 5 a
/\Aa{,5}/   # optional zero to 5 a at beginning of string
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here are a few runs (using the &lt;code&gt;/p&lt;/code&gt; flag for the safe match variables, although this really hasn&#39;t been a problem with v5.18). The first line matches because there is a &lt;i&gt;b&lt;/i&gt; with some &lt;i&gt;a&lt;/i&gt;s after it. The next line matches for the same reason if you count zero as &quot;some&quot;. The third line matches the first instance of &lt;code&gt;ba&lt;/code&gt; like all other Perl patterns; it finds the leftmost match, not the longest. Finally, consider the last line. It matches no &lt;i&gt;a&lt;/i&gt;s because matching a &lt;i&gt;b&lt;/i&gt; with zero &lt;i&gt;a&lt;/i&gt;s works too. That might surprise you:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.34.0 -ne &#39;/ba{,5}/p &amp;&amp; print qq(${^PREMATCH} &amp;lt;${^MATCH}&gt; ${^POSTMATCH})&#39;
baaaaaaaaaacd
 &amp;lt;baaaaa&gt; aaaaacd
bcd
 &amp;lt;b&gt; cd
babaaa
 &amp;lt;ba&gt; baaa
bbaaa
 &amp;lt;b&gt; baaa
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, consider what happens with other versions of Perl. Perl v5.22 is the version that deprecated an unescaped left brace. Although the regex parses, it doesn&#39;t handle this syntax. Instead, it sees &lt;code&gt;{,5}&lt;/code&gt; as literal characters:
&lt;/p&gt;



&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.22 -ne &#39;/ba{,5}/p &amp;&amp; print qq(${^PREMATCH} &amp;lt;${^MATCH}&gt; ${^POSTMATCH})&#39;
Unescaped left brace in regex is deprecated, passed through
in regex; marked by &amp;lt;-- HERE in m/ba{ &amp;lt;-- HERE ,5}/ at ...
ba
baaaaa
ba{,5}
 &amp;lt;ba{,5}&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.26 (then not v5.28, then with v5.30 again), the unescaped left brace is fatal (&lt;a href=&quot;/2017/04/you-must-escape-the-left-brace-in-a-regex/&quot;&gt;You must escape the left brace in a regex&lt;/a&gt;):
&lt;/p&gt;



&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.26 -ne &#39; /ba{,5}/p &amp;&amp; print qq(${^PREMATCH} &amp;lt;${^MATCH}&gt; ${^POSTMATCH})&#39;

Unescaped left brace in regex is illegal here in regex;
marked by &amp;lt;-- HERE in m/ba{ &amp;lt;-- HERE ,5}/ at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Finally, here&#39;s a summary of the quantifiers up to v5.34:
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;&lt;th&gt;Quantifier&lt;/th&gt;&lt;th&gt;General Quantifier&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt; &lt;code&gt;*&lt;/code&gt;           &lt;/td&gt;&lt;td&gt; &lt;code&gt;{ 0, } &lt;/code&gt;             &lt;/td&gt;&lt;td&gt; zero or more&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;  &lt;code&gt;+&lt;/code&gt;          &lt;/td&gt;&lt;td&gt; &lt;code&gt;{ 1, }  &lt;/code&gt;            &lt;/td&gt;&lt;td&gt; one or more &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;  &lt;code&gt;?&lt;/code&gt;          &lt;/td&gt;&lt;td&gt; &lt;code&gt;{ 0, 1 } &lt;/code&gt;           &lt;/td&gt;&lt;td&gt; zero or one  &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;            &lt;/td&gt;&lt;td&gt;&lt;code&gt;{ n, m }  &lt;/code&gt;          &lt;/td&gt;&lt;td&gt; between n and m matches &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;           &lt;/td&gt;&lt;td&gt; &lt;code&gt;{ n, }&lt;/code&gt;              &lt;/td&gt;&lt;td&gt; at least n, but unlimited-ish &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;          &lt;/td&gt;&lt;td&gt; &lt;code&gt;{ , m } &lt;/code&gt;         &lt;/td&gt;&lt;td&gt; up to m (new!) &lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;


&lt;p&gt;
Although we say that &lt;code&gt;{n,}&lt;/code&gt; is unlimited, &lt;a href=&quot;/2018/12/perl-v5-30-lets-you-match-more-with-the-general-quantifier/&quot;&gt;pedantically it&#39;s not&lt;/a&gt;. It&#39;s &quot;unlimited-ish&quot;. Before v5.30, the maximum number is actually 32,766. That likely means &quot;infinite&quot; to you (and if it doesn&#39;t, let me know what you are doing). With v5.30, &lt;a href=&quot;/2018/12/perl-v5-30-lets-you-match-more-with-the-general-quantifier/&quot;&gt;the maximum increased to 65,535&lt;/a&gt; (and if that&#39;s still not enough, I really want to know what you are doing).
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.38 New Features</title>
		<link>https://www.effectiveperlprogramming.com/2023/07/perl-v5-38-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2023/07/perl-v5-38-new-features/</guid>
		<pubDate>Sat, 22 Jul 2023 19:58:49 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-38</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; These items are in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Perl 4 package separators are deprecated&lt;/li&gt;
&lt;li&gt;user-defined bareword file handles are disabled with &lt;code&gt;use v5.38&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the new &lt;code&gt;class&lt;/code&gt; feature&lt;/li&gt;
&lt;li&gt;compilation stops on the first syntax error&lt;/li&gt;
&lt;li&gt;deprecations now have subcategories&lt;/li&gt;
&lt;li&gt;the new &lt;code&gt;${^LAST_SUCCESSFUL_PATTERN}&lt;/code&gt; variable&lt;/li&gt;
&lt;li&gt;modules no longer have to return a true value&lt;/li&gt;
&lt;li&gt;signatures can specify a default value if an argument is undefined or false&lt;/li&gt;
&lt;li&gt;the switch feature is deprecated and scheduled for removal&lt;/li&gt;
&lt;li&gt;Unicode 15&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Specify octal numbers with the 0o prefix</title>
		<link>https://www.effectiveperlprogramming.com/2021/03/specify-octal-numbers-with-the-0o-prefix/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2021/03/specify-octal-numbers-with-the-0o-prefix/</guid>
		<pubDate>Wed, 24 Mar 2021 12:48:26 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-34</category>
		<description>&lt;p&gt;
Perl v5.34 allows you to specify octal literals with the &lt;code&gt;0o&lt;/code&gt; prefix, as in &lt;code&gt;0o123_456&lt;/code&gt;. This is consistent with the existing constructs to specify hexadecimal literal &lt;code&gt;0xddddd&lt;/code&gt; and binary literal &lt;code&gt;0bddddd&lt;/code&gt;. The builtin &lt;code&gt;oct()&lt;/code&gt; function accepts any of these forms.
&lt;/p&gt;



&lt;p&gt;
Previously, you specified octal with just a leading zero:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;chmod 0644, $file;
mkdir 0755, $file;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now you can do that with an extra character that specifies the base:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;chmod 0o644, $file;
mkdir 0o755, $file;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This makes it consistent with &lt;code&gt;0b&lt;/code&gt; for binary and &lt;code&gt;0x&lt;/code&gt; for hexadecimal. See &quot;Scalar value constructors&quot; in &lt;a href=&quot;https://perldoc.perl.org/perldata#Scalar-value-constructors&quot;&gt;perldata&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
And, remember that v5.14 added the &lt;code&gt;\o{NNN}&lt;/code&gt; notation to &lt;a href=&quot;/2010/10/specify-any-character-by-its-octal-ordinal-value/&quot;&gt;specify characters by their octal number&lt;/a&gt;. We&#39;re still waiting for octal floating point values (we got the &lt;a href=&quot;/2015/06/perl-v5-22-adds-hexadecimal-floating-point-literals/&quot;&gt;hex version&lt;/a&gt; in v5.22), but don&#39;t hold your breath.
&lt;/p&gt;



&lt;p&gt;
Perhaps we&#39;ll get &lt;code&gt;0d&lt;/code&gt; sometime so that all the bases have a prefix. 
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.34 new features</title>
		<link>https://www.effectiveperlprogramming.com/2021/03/perl-v5-34-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2021/03/perl-v5-34-new-features/</guid>
		<pubDate>Wed, 24 Mar 2021 11:38:13 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-34</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2021/03/specify-octal-numbers-with-the-0o-prefix/&quot;&gt;Specify octal numbers with the &lt;code&gt;0o&lt;/code&gt; prefix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Experimental &lt;code&gt;try-catch&lt;/code&gt; syntax&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2023/11/up-to-n-matches/&quot;&gt;The no-minimum &lt;code&gt;{,m}&lt;/code&gt; regex quantifier&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2024/03/insignificant-whitespace-in-brace-constructs/&quot;&gt;Add insignificant space in brace constructs, such as &lt;code&gt;\x{...}&lt;/code&gt; and &lt;code&gt;{n,m}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
And, I&#39;ll update the &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;Perl New Features&lt;/a&gt; for v5.34 too.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Match Unicode property values with a wildcard</title>
		<link>https://www.effectiveperlprogramming.com/2020/07/match-unicode-property-values-with-a-wildcard/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/07/match-unicode-property-values-with-a-wildcard/</guid>
		<pubDate>Sat, 11 Jul 2020 13:27:23 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
Not all characters with a numerical value are &quot;digits&quot;, in the Perl sense. You saw in &lt;a href=&quot;/2010/07/match-unicode-characters-by-property-value/&quot;&gt;Match Unicode characters by property value&lt;/a&gt;, in which you selected characters by a Unicode property value. That showed up again in &lt;a href=&quot;/2015/02/perl-v5-18-adds-character-class-set-operations/&quot;&gt;Perl v5.18 adds character class set operations&lt;/a&gt;. Perl v5.30 adds a slightly easier way to get at multiple numerical values at the same time. Now you can match Unicode property values with &lt;a href=&quot;https://www.unicode.org/reports/tr18/#Wildcard_Properties&quot;&gt;wildcards (Unicode TR 18)&lt;/a&gt;, which are sorta like Perl patterns. Don&#39;t get too excited, though, because these can be expensive.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Here&#39;s what &lt;a href=&quot;/2010/07/match-unicode-characters-by-property-value/&quot;&gt;you saw before&lt;/a&gt; to match a character that has a particular value for a particular Unicode property:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.12;
use open qw(:std :utf8);

use Unicode::UCD;
say &quot;Unicode &quot;, Unicode::UCD::UnicodeVersion();

foreach ( 1 .. 0x10fffd ) {
	next unless chr =~ m/ \p{Numeric_Value: $ARGV[0]} /x;
	printf &quot;%s (U+%04X)\n&quot;, chr, $_;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
And then, &lt;a href=&quot;/2015/02/perl-v5-18-adds-character-class-set-operations/&quot;&gt;you saw&lt;/a&gt; that you can match one of several numerical values by putting the property values in a character class:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

use open qw(:std :utf8);

use Unicode::UCD;
say &quot;Unicode &quot;, Unicode::UCD::UnicodeVersion();

foreach ( 1 .. 0x10fffd ) {
	next unless chr =~ m/ [\p{nv=1}\p{nv=3}\p{nv=7}] /x;
	printf &quot;%s (U+%04X)\n&quot;, chr, $_;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s not all that great. You have to repeat the &lt;code&gt;\p{nv=}&lt;/code&gt; for each value. Well, you had to, and now you don&#39;t. Perl v5.30 allows the property value to be a &lt;a href=&quot;https://perldoc.perl.org/perlunicode#Wildcards-in-Property-Values&quot;&gt;wildcard&lt;/a&gt;. This looks almost like a Perl pattern:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $regex = qr| \p{nv=/[137]/} |x;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s not really the match operator there and there are many restrictions on what you can do in the pattern. For example, you can&#39;t use a closing brace in your pattern because Perl thinks that closes &lt;code&gt;\p{nv=&lt;/code&gt;. Also, you can use an alternate delimiter so you don&#39;t need to change the delimiter for the overall pattern:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $regex = qr/ \p{nv=:[137]:} /x;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There is a limited subset of Perl patterns, summarized in &lt;a href=&quot;https://perldoc.perl.org/perlunicode#Wildcards-in-Property-Values&quot;&gt;perlunicode&lt;/a&gt;:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;You can&#39;t have match modifiers outside the pattern. No &lt;code&gt;/a b c/ix&lt;/code&gt;, but you can have &lt;code&gt;/(?ix) a b c/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Alternate delimiters must be punctuation, but not &lt;code&gt;{}&lt;/code&gt; because the braces interfere with &lt;code&gt;\p{}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No zero-or-more quantifiers.&lt;/li&gt;
&lt;li&gt;No &lt;code&gt;\p{}&lt;/code&gt; inside a &lt;code&gt;\p{}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You automatically get insignificant whitespace around the wildcard: &lt;code&gt;\p{nv= :[0-5]:}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
All of that is nice, but realize that you are actually matching that pattern against all the Unicode properties. That can be an expensive operation. The savings in typing might not be worth it.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Match Unicode character names with a pattern</title>
		<link>https://www.effectiveperlprogramming.com/2020/06/match-unicode-character-names-with-a-pattern/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/06/match-unicode-character-names-with-a-pattern/</guid>
		<pubDate>Fri, 26 Jun 2020 16:33:22 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
Perl has some of the best Unicode support out there, and it keeps getting better. Perl v5.32 supports Unicode 13, and you can now apply patterns to character names. You probably don&#39;t want to do that though.
&lt;/p&gt;



&lt;p&gt;
First, the &lt;a href=&quot;https://unicode.org/ucd/&quot;&gt;Unicode Character Database&lt;/a&gt; catalogs each character, giving it a code number, a name, and many other properties.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt; &lt;th&gt;Char&lt;/th&gt; &lt;th&gt;Code number&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;/tr&gt;

&lt;tr&gt; &lt;td&gt;à&lt;/td&gt; &lt;td&gt;U+00E0&lt;/td&gt; &lt;td&gt;ʟᴀᴛɪɴ sᴍᴀʟʟ ʟᴇᴛᴛᴇʀ ᴀ ᴡɪᴛʜ ɢʀᴀᴠᴇ&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;á&lt;/td&gt; &lt;td&gt;U+00E1&lt;/td&gt; &lt;td&gt;ʟᴀᴛɪɴ sᴍᴀʟʟ ʟᴇᴛᴛᴇʀ ᴀ ᴡɪᴛʜ ᴀᴄᴜᴛᴇ&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;🐪&lt;/td&gt; &lt;td&gt;U+1F42A&lt;/td&gt; &lt;td&gt;ᴅʀᴏᴍᴇᴅᴀʀʏ ᴄᴀᴍᴇʟ&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;🐫&lt;/td&gt; &lt;td&gt;U+1F42B&lt;/td&gt; &lt;td&gt;ʙᴀᴄᴛʀɪᴀɴ ᴄᴀᴍᴇʟ&lt;/td&gt; &lt;/tr&gt;

&lt;/table&gt;


&lt;p&gt;
Start with what Perl can already do. Match characters by their complete name in uppercase (strict Unicode matching). Put the character name in &lt;code&gt;\N{}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.10;

$_ = &quot;\x{1F42A}&quot;; # Dromedary
say &quot;1: &quot;, /\N{DROMEDARY CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;

$_ = &quot;\x{1F42B}&quot;; # Bactrian
say &quot;2: &quot;, /\N{BACTRIAN CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Both of these match because they use the exact names:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: Matched
2: Matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/charnames&quot;&gt;charnames&lt;/a&gt; module, which comes with Perl, allows &quot;loose&quot; matching. If you&#39;re willing to give up to a 3x performance hit for Perl to match against all the names to determine a match, you can use mixed case and insignificant underscores and hyphens:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.10;

use charnames qw(:loose);

$_ = &quot;\x{1F42A}&quot;; # Dromedary
say &quot;1: &quot;, /\N{DROMEDARY CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;2: &quot;, /\N{DROMEDARY_CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;3: &quot;, /\N{dromedary camel}/ ? &#39;Matched&#39; : &#39;Missed&#39;;

$_ = &quot;\x{1F42B}&quot;; # Bactrian
say &quot;4: &quot;, /\N{BACTRIAN CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;5: &quot;, /\N{BACTRIAN-CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;6: &quot;, /\N{bactrian camel}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
All of these match even though they aren&#39;t the exact names:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: Matched
2: Matched
3: Matched
4: Matched
5: Matched
6: Matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can&#39;t interpolate inside &lt;code&gt;\N{}&lt;/code&gt;. This is the literal name &lt;code&gt;$type CAMEL&lt;/code&gt;, and there&#39;s no such character even under loose matching:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.10;

$_ = &quot;\x{1F42A}&quot;; # Dromedary
my $type = &#39;DROMEDARY&#39;;
say &quot;1: &quot;, /\N{$type CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It fails at compile time:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Unknown charname &#39;$type CAMEL&#39; at ...
Execution of ... aborted due to compilation errors.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Instead, you can make your own aliases to names and avoid the loose matching:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.10;

use charnames &#39;:alias&#39; =&gt; {
	ONE_HUMP_CAMEL =&gt; &#39;DROMEDARY CAMEL&#39;,
	TWO_HUMP_CAMEL =&gt; &#39;BACTRIAN CAMEL&#39;,
	};

$_ = &quot;\x{1F42A}&quot;; # Dromedary
my $type = &#39;DROMEDARY&#39;;
say &quot;1: &quot;, /\N{ONE_HUMP_CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;What&#39;s new in v5.32&lt;/h2&gt;


&lt;p&gt;
Perl v5.32 allows you to specify a name with the new &lt;code&gt;\p{Name=}&lt;/code&gt; syntax. It uses loose Unicode matching and allows interpolation:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.32;

$_ = &quot;\x{1F42A}&quot;; # Dromedary
my $type = &#39;DROMEDARY&#39;;
say &quot;1: &quot;, /\p{Name=DROMEDARY CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;2: &quot;, /\p{Name=dromedary camel}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;3: &quot;, /\p{Name=$type CAMEL}/     ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;4: &quot;, /\p{Name=$type camel}/     ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;5: &quot;, /\p{Name=$type c_a_M-e-L}/ ? &#39;Matched&#39; : &#39;Missed&#39;;

# Vary &quot;Name&quot; too
say &quot;6: &quot;, /\p{name=DROMEDARY CAMEL}/ ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;7: &quot;, /\p{Na=DROMEDARY CAMEL}/   ? &#39;Matched&#39; : &#39;Missed&#39;;
say &quot;8: &quot;, /\p{na=DROMEDARY CAMEL}/   ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It gets better though. You can have subpatterns for the name. It&#39;s an already-enabled experimental feature but you can disable its warning:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.32;
no warnings qw(experimental::uniprop_wildcards);

$_ = &quot;\x{1F42A}&quot;; # Dromedary

say &quot;1: &quot;, m&amp;lt;\p{Name=/(DROMEDARY|BACTRIAN) CAMEL/}&gt; ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s a pattern that finds the Latin Small Letters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.32;
no warnings qw(experimental::uniprop_wildcards);

my @letters = qw(A b C d E);

foreach my $letter ( @letters ) {
	say &quot;$letter: &quot;,
		$letter =~ m&amp;lt;\p{Name=/LATIN SMALL LETTER [A-Z]/}&gt;
		?
		&#39;Matched&#39; : &#39;Missed&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
And here&#39;s a program that tests if a character&#39;s name includes &lt;code&gt;WITH&lt;/code&gt;, indicating there&#39;s something else with the base character:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.32;
use open qw(:std :utf8);

no warnings qw(experimental::uniprop_wildcards);

use charnames qw();

my @letters = qw(a à á â ã ä å);

foreach my $letter ( @letters ) {
	my $name = charnames::viacode( ord $letter );
	say &quot;$letter ($name):&quot;,
		$letter =~ m&amp;lt;\p{Name=/\bWITH\b/}&gt;
		?
		&#39;Matched&#39; : &#39;Missed&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This matches the accented characters from that list:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;a (LATIN SMALL LETTER A):Missed
à (LATIN SMALL LETTER A WITH GRAVE):Matched
á (LATIN SMALL LETTER A WITH ACUTE):Matched
â (LATIN SMALL LETTER A WITH CIRCUMFLEX):Matched
ã (LATIN SMALL LETTER A WITH TILDE):Matched
ä (LATIN SMALL LETTER A WITH DIAERESIS):Matched
å (LATIN SMALL LETTER A WITH RING ABOVE):Matched
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Not quite the match operator&lt;/h3&gt;


&lt;p&gt;
That syntax in the &lt;code&gt;\p{}&lt;/code&gt; looks like the match operator:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;\p{Name=/\bWITH\b/i}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s not. You can&#39;t use the leading &lt;code&gt;m&lt;/code&gt; or set flags. These are compilation errors:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;\p{Name=m/\bWITH\b/}
\p{Name=/\bWITH\b/i}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, you can set flags inside the pattern:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;\p{Name=/(?ix)\b w i T h \b /}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The quantifiers are a bit weird. You can&#39;t use the zero-or-more star, and the braces for the generalized quantifier interfere with the &lt;code&gt;\p{}&lt;/code&gt; syntax. Non-backtracking modifiers aren&#39;t allowed, but the non-greedy &lt;code&gt;?&lt;/code&gt; is:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;\p{Name=/\S+ WITH\b/}      # Yes
\p{Name=/\S* WITH\b/}      # Error
\p{Name=/\S{1,} WITH\b/}   # Error
\p{Name=/\b\S++ITH\b/}     # Error
\p{Name=/\b\S+?ITH\b/}     # Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can loosely match Unicode character names, but an alias might be better.&lt;/li&gt;
&lt;li&gt;You can match the Name property of a character with a limited set of pattern features.&lt;/li&gt;
&lt;li&gt;Applying patterns to every Unicode character name can be very slow.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Turn off indirect object notation</title>
		<link>https://www.effectiveperlprogramming.com/2020/06/turn-off-indirect-object-notation/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/06/turn-off-indirect-object-notation/</guid>
		<pubDate>Wed, 17 Jun 2020 14:50:51 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
Perl v5.32 adds a way to turn off a Perl feature that you shouldn&#39;t use anyway. You can still use this feature, but now there&#39;s a way to take it away from you.
&lt;/p&gt;

&lt;!--more--&gt;

Indirect object notation is a way of putting the cart before the horse. The method name shows up in front of the invocant, whether a class name or an instance:

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = new Horse;
cart $horse;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
C++ programmers (maybe Java) may look at that and think &quot;Yeah, so,&quot; and that&#39;s fine. But Perl having two ways to do it is one too many. It feels weird to say that, but the code that makes the &lt;code&gt;perl&lt;/code&gt; binary doesn&#39;t have to use the same philosophy it extends to user code. If the Perl core didn&#39;t have to support this second way, its parsing could be simpler. And, cleaner internals make it easier to support new ideas. This sort of clean up in the past several years helps people help &lt;code&gt;perl&lt;/code&gt;.
&lt;/p&gt;



&lt;p&gt;
Consider this problem that I encounter every week. I&#39;ve forgotten to enable the &lt;code&gt;say&lt;/code&gt; feature (wouldn&#39;t it be nice if we could have nice things for free?), so I get a weird error:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Mojo::URL;
my $url = Mojo::URL-&gt;new( &#39;https://www.example.com&#39; );
say $url;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Because Perl supports indirect object notation, the runtime error thinks that &lt;code&gt;say&lt;/code&gt; must be a method for the &lt;code&gt;$url&lt;/code&gt; object:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Can&#39;t locate object method &quot;say&quot; via package &quot;Mojo::URL&quot;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Imagine a package that had an &lt;code&gt;AUTOLOAD&lt;/code&gt; that would try to handle that &lt;code&gt;say&lt;/code&gt;. That&#39;s some weird voodoo.
&lt;/p&gt;



&lt;p&gt;
I don&#39;t think the indirect object notation was ever the dominant form, but it also wasn&#39;t rare. Some popular modules used it in their docs. But, the direct object notation is modern practice. &lt;a href=&quot;https://amzn.to/302c9uT&quot;&gt;Perl Best Practices&lt;/a&gt; recommends against it in section 15.12 and the &lt;a href=&quot;https://metacpan.org/pod/Perl::Critic&quot;&gt;Perl::Critic&lt;/a&gt; policy &lt;a href=&quot;https://metacpan.org/pod/Perl::Critic::Policy::Objects::ProhibitIndirectSyntax&quot;&gt;Objects::ProhibitIndirectSyntax&lt;/a&gt; prohibit it. That&#39;s 15 years ago!
&lt;/p&gt;



&lt;p&gt;
You should use the direct object notation, which you&#39;re already likely to see most of the time:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $horse = Horse-&gt;new;
$horse-&gt;cart;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This doesn&#39;t help me avoid that mistake that I showed earlier. I wasn&#39;t trying to use the indirect object and it still shows up. This is where the knobs and dials come in.
&lt;/p&gt;



&lt;p&gt;
Perl v5.32 now has a way to turn off the indirect object syntax. Actually, it has a way to turn it on, but it&#39;s turned on by default in v5.32, and is already available in all previous Perl 5 versions. This still works and outputs &quot;Neigh!&quot;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;

package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = new Horse;
cart $horse;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Use the &lt;code&gt;feature&lt;/code&gt; pragma to turn off &lt;code&gt;indirect&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;
no feature qw(indirect);

package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = new Horse;
cart $horse;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.36, you get this for free:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.36;  # no feature qw(indirect) for free
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The error shows that &lt;code&gt;perl&lt;/code&gt; doesn&#39;t understand the code and gets confused by the method name:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Bareword found where operator expected at ..., near &quot;new Horse&quot;
	(Do you need to predeclare new?)
Scalar found where operator expected at ..., near &quot;cart $horse&quot;
	(Do you need to predeclare cart?)
syntax error at..., near &quot;new Horse&quot;
Global symbol &quot;$horse&quot; requires explicit package name (did you forget to declare &quot;my $horse&quot;?) at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Change that to the direct object notation and it works again:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;
no feature qw(indirect);

package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = Horse-&gt;new;
$horse-&gt;cart;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s a way that you can do this before v5.32 too. The CPAN module &lt;a href=&quot;https://metacpan.org/pod/indirect&quot;&gt;indirect&lt;/a&gt; will warn about indirect object notation (but the method calls still work):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;
no indirect;

package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = new Horse;
cart $horse;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The warning is better than the parser confusion for v5.32, so you might want to start with this module before turning off all uses (or use Perl::Critic too):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Neigh!
Indirect call of method &quot;new&quot; on object &quot;Horse&quot; at ... line 10.
Indirect call of method &quot;cart&quot; on object &quot;$horse&quot; at ... line 11.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Make that an error by importing &lt;code&gt;fatal&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;
no indirect &#39;fatal&#39;;

package Horse {
	sub new  { bless {}, $_[0] }
	sub cart { print &quot;Neigh!&quot; }
	}

my $horse = new Horse;
cart $horse;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the program dies on the &lt;code&gt;new&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Indirect call of method &quot;new&quot; on object &quot;Horse&quot; at ... line 9.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Indirect filehandles&lt;/h2&gt;


&lt;p&gt;
Some of you may know that filehandles are objects but we generally treat them as functions. This is the reason there&#39;s no comma between the filehandle and the first argument:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;print STDOUT &quot;Hello world&quot;;
print { $filehandle } &quot;Hello world&quot;;
$filehandle-&gt;print( &quot;Hello world&quot; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
So far, &lt;code&gt;no feature qw(indirect)&lt;/code&gt; does not disturb these.
&lt;/p&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;
Here&#39;s the thing for you to think about: there&#39;s a way for you to turn off this feature, but it&#39;s enabled by default. You don&#39;t have to stop using indirect object notation. But now Perl has that knob and dial and eventually might disable it by default.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Chain comparisons to avoid excessive typing</title>
		<link>https://www.effectiveperlprogramming.com/2020/03/chain-comparisons-to-avoid-excessive-typing/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/03/chain-comparisons-to-avoid-excessive-typing/</guid>
		<pubDate>Fri, 27 Mar 2020 06:26:05 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
Checking that a value is between two others involves two comparisons, and so far in Perl that&#39;s meant that you&#39;ve had to type one of the values more than once. That gets simpler in v5.32 with chained comparisons. This would make Perl one of the &lt;a href=&quot;https://softwareengineering.stackexchange.com/q/316969/103630&quot;&gt;few languages that support the feature&lt;/a&gt;. So far it&#39;s implemented in v5.31.10 and until v5.32 is actually released, it isn&#39;t a real feature.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Raku (formerly Perl 6) has had this feature for a long time, as has &lt;a href=&quot;https://docs.python.org/3/reference/expressions.html#comparisons&quot;&gt;Python&lt;/a&gt; (but apparently &lt;a href=&quot;https://stackoverflow.com/q/4089284/2766176&quot;&gt;not JavaScript&lt;/a&gt;):
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://web.archive.org/web/20200810114957/https://www.learningraku.com/2017/05/21/dont-use-in-programming/&quot;&gt;Don&#39;t use &amp;lt; in programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://web.archive.org/web/20200930024210/https://www.learningraku.com/2020/03/17/reducing-comparison-operators/&quot;&gt;Reducing comparison operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
So far you have to write two comparisons and join them with a logical operator. If you&#39;ve never complained about this, you probably haven&#39;t been programming long enough:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( $lower &amp;lt; $n and $n &amp;lt; $upper ) { ... }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Mathematically, you want to write that as &lt;i&gt;lower &amp;lt; n &amp;lt; upper&lt;/i&gt;. It&#39;s an idea, not an implementation. A high-level language should allow us to use ideas with the compiler translating it into action. And, that&#39;s now what you can do. Write the chained comparison:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.10;
if( 1 &amp;lt; $ARGV[0] &amp;lt; 10 ) {
	say &quot;$ARGV[0] is between 1 and 10&quot;;
	}
else {
	say &quot;Out of range&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The compiler recognizes the idea and turns it into the right lower level steps:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.10;
if( 1 &amp;lt; $ARGV[0] and $ARGV[0] &amp;lt; 10 ) {
	say &quot;$ARGV[0] is between 1 and 10&quot;;
	}
else {
	say &quot;Out of range&quot;;
	}

&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Evaluating once&lt;/h2&gt;


&lt;p&gt;
In the previous code, it looks like you have to evaluate the middle operand twice since it&#39;s part of two comparisons. In a dynamic language like Perl, you can&#39;t rely on something keeping the same value. However, Perl caches the first evaluation and uses that value in the second comparison. Consider this program, where you use a function call in the middle. This function returns a possibly (probably) different value every time, but also outputs a line every time you call it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.10;

sub unstable {
	my $value = int rand 10;
	say &quot;Called unstable, returning $value&quot;;
	return $value;
	}

if( 5 &amp;lt; unstable() &amp;lt; 9 ) {
	say &quot;Found a value between 5 and 9&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; calls the subroutine exactly once:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.31.10 ~/Desktop/test.pl
Called unstable, returning 4

$ perl5.31.10 ~/Desktop/test.pl
Called unstable, returning 1

$ perl5.31.10 ~/Desktop/test.pl
Called unstable, returning 5

$ perl5.31.10 ~/Desktop/test.pl
Called unstable, returning 6
Found a value between 5 and 9
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This works with a method call too:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.10;

package UnstableScalar {
	sub new { bless {}, $_[0] }
	sub val {
		my $value = int rand 10;
		say &quot;Calling val with $value&quot;;
		return $value;
		}
	}

my $unstable = UnstableScalar-&gt;new;

if( 5 &amp;lt; $unstable-&gt;val &amp;lt; 9 ) {
	say &quot;Found a value between 5 and 9&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that you don&#39;t call the method twice:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.31.10 ~/Desktop/test.pl
Calling val with 0

$ perl5.31.10 ~/Desktop/test.pl
Calling val with 6
Found a value between 5 and 9

$ perl5.31.10 ~/Desktop/test.pl
Calling val with 5
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Broken for tied scalars&lt;/h3&gt;


&lt;p&gt;
As I write this article, tied scalars are evaluated more than once.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.10;

package UnstableScalar {
	use parent qw(Tie::Scalar);
	sub TIESCALAR { bless {}, $_[0] }
	sub FETCH {
		my $value = int rand 10;
		say &quot;Fetching scalar with $value&quot;;
		return $value;
		}
	}

tie my $unstable, &#39;UnstableScalar&#39;;

if( 5 &amp;lt; $unstable &amp;lt; 9 ) {
	say &quot;Found a value between 5 and 9&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is basically the same thing but with extra magic going on. If the left side evaluates to false, the second evaluation doesn&#39;t happen:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.31.10 ~/Desktop/test.pl
Fetching scalar with 1

$ perl5.31.10 ~/Desktop/test.pl
Fetching scalar with 8
Fetching scalar with 4
Found a value between 5 and 9

$ perl5.31.10 ~/Desktop/test.pl
Fetching scalar with 6
Fetching scalar with 9
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You already know that a method call works, so you can get the object underneath the &lt;code&gt;tie&lt;/code&gt; then call a method on that. This works as you would expect:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;	if( 5 &amp;lt; tied($unstable)-&gt;FETCH &amp;lt; 9 ) {
		say &quot;Found a value between 5 and 9&quot;;
		}
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Use a variable-width lookbehind if it won&#39;t match more than 255 characters</title>
		<link>https://www.effectiveperlprogramming.com/2020/03/use-a-variable-width-lookbehind-if-it-wont-match-more-than-255-characters/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/03/use-a-variable-width-lookbehind-if-it-wont-match-more-than-255-characters/</guid>
		<pubDate>Thu, 26 Mar 2020 09:27:34 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
In &lt;a href=&quot;/2012/10/ignore-part-of-a-substitutions-match/&quot;&gt;Ignore part of a substitution’s match&lt;/a&gt;, I showed you the match resetting &lt;code&gt;\K&lt;/code&gt;—it&#39;s basically a variable-width positive lookbehind assertion. It&#39;s a special feature to work around Perl&#39;s lack of variable-width lookbehinds. However, v5.30 adds an experimental feature to allow a limited version of a variable-width lookbehind.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
In that post, I used the example of matching variable whitespace at the beginning of a line but not replacing it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &quot;    buster&quot;;      # lowercase!
s/\A\s+\K\S+\z/\u$&amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Before v5.30, a positive lookbehind fails to compile:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &quot;    buster&quot;;      # lowercase!
s/(?&amp;lt;=\A\s+)\S+\z/\u$&amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Running this on v5.28 fails:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.28.0 vlb.pl
Variable length lookbehind not implemented
in regex m/(?&amp;lt;=\A\s+)\S+\z/ at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Running this on v5.30 gives a different error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.30.0 vlb.pl
Lookbehind longer than 255 not implemented
in regex m/(?&amp;lt;=\A\s+)\S+\z/ at ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s the limitation. The regex engine needs to know in advance that the length of the subpattern won&#39;t be longer than 255. The &lt;code&gt;+&lt;/code&gt; has an indeterminate length. Instead of the &lt;code&gt;+&lt;/code&gt;, use the generalized quantifier so you can specify a maximum number—255 levels of indent should be enough for anyone:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.30;
$_ = &quot;    buster&quot;;      # lowercase!
s/(?&amp;lt;=\A\s{1,255})\S+\z/\u$&amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This works because static analysis can tell that the pattern cannot match more than 255 characters. You do get a warning:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Line |    Buster|
Variable length lookbehind is experimental in regex;
marked by &amp;lt;-- HERE in m/(?&amp;lt;=\A\s{1,255})\S+\z &amp;lt;-- HERE / at ...
&lt;/code&gt;&lt;/pre&gt;

&lt;a href=&quot;/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/&quot;&gt;Turn off that experimental warning&lt;/a&gt; like you would other experimental warnings:

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.30;
no warnings qw(experimental::vlb);
$_ = &quot;    buster&quot;;      # lowercase!
s/(?&amp;lt;=\A\s{1,255})\S+\z/\u$&amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s one more thing to consider though. Some characters turn into multiple characters with case folding, as you read in &lt;a href=&quot;/2012/02/fold-cases-properly/&quot;&gt;Fold cases properly&lt;/a&gt; with ß (U+00DF LATIN SMALL LETTER SHARP S) that turns into ss. If you use &lt;code&gt;/i&lt;/code&gt; for case insensitivity, Perl knows that this happens and counts the final number of characters in the 255 limit.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.30;
no warnings qw(experimental::vlb);
$_ = &quot;    buster&quot;;      # lowercase!
s/(?&amp;lt;=\A\s{1,253}ß)\S+\z/\u$&amp;/i;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
All of this works for either positive or negative lookbehinds.
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Before v5.30, you could not have variable-width lookbehinds&lt;/li&gt;
&lt;li&gt;v5.30 adds limited support for variable-width lookbehinds&lt;/li&gt;
&lt;li&gt;The lookbehind subpattern must not be able to match more than 255 characters&lt;/li&gt;
&lt;li&gt;If you can&#39;t determine the length of the sub pattern match, you can still use &lt;code&gt;\K&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl v5.32 new features</title>
		<link>https://www.effectiveperlprogramming.com/2020/01/perl-v5-32-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/01/perl-v5-32-new-features/</guid>
		<pubDate>Thu, 23 Jan 2020 09:41:50 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
Perl v5.32 is out and it has some interesting new features. The previous major releases focussed more on finally removing deprecations and shoring up odd cases, and you still find a few of those in this release. Full details, as always, are in the &lt;i&gt;perldelta&lt;/i&gt;.
&lt;/p&gt;



&lt;p&gt;
&lt;span class=&quot;person&quot;&gt;Sawyer X&lt;/span&gt; just &lt;a href=&quot;https://www.perl.com/article/announcing-perl-7/&quot;&gt;announced Perl 7&lt;/a&gt; as a major version jump that relabels what is now v5.32. If your code is ready for v5.32, you should be mostly ready for Perl 7.
&lt;/p&gt;



&lt;ul&gt;
 	&lt;li&gt;&lt;a href=&quot;/2020/01/use-the-infix-class-instance-operator/&quot;&gt;The new &lt;code&gt;isa&lt;/code&gt; infix operator (&quot;class instance&quot;)&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;The &lt;code&gt;streamzip&lt;/code&gt; program comes with IO::Compress::Base, so it comes with perl&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2019/02/use-alpha-assertions-for-more-understandable-regexes/&quot;&gt;Regex alpha assertions are stable&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2019/03/match-only-the-same-unicode-script/&quot;&gt;Script runs are no longer experimental&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;/2020/06/match-unicode-character-names-with-a-pattern/&quot;&gt;Match Unicode names with a pattern&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;Mixed mode access to undef uses a temporary file&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;https://www.learning-perl.com/2019/01/surprising-output-for-the-descending-range-01-1/&quot;&gt;&lt;code&gt;&quot;0&quot; .. &quot;-1&quot;&lt;/code&gt; is fixed&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;Modifiable contexts in constants now throw an exception&lt;/li&gt;
 	&lt;li&gt;User-defined properties override Unicode ones of the same name&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;/2020/03/chain-comparisons-to-avoid-excessive-typing/&quot;&gt;Use chained comparison operators&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;/2020/06/turn-off-indirect-object-notation/&quot;&gt;Disable indirect object notation&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use the infix class instance operator</title>
		<link>https://www.effectiveperlprogramming.com/2020/01/use-the-infix-class-instance-operator/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2020/01/use-the-infix-class-instance-operator/</guid>
		<pubDate>Thu, 23 Jan 2020 09:33:25 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;



&lt;hr/&gt;


&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This feature is stable in &lt;a href=&quot;/2024/07/perl-v5-36-new-features/&quot;&gt;v5.36&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.32 adds &lt;a href=&quot;https://github.com/leonerd&quot;&gt;&lt;span class=&quot;person&quot;&gt;Paul Evans&lt;/span&gt;&#39;s&lt;/a&gt; infix &lt;code&gt;isa&lt;/code&gt; operator—the &quot;class instance operator&quot; as an experimental feature. It still has some issues to work out which prevent its use at the moment, but it looks promising. It subverts how the &lt;code&gt;UNIVERSAL::isa&lt;/code&gt; does its job and breaks that in the process. As an experimental feature, that&#39;s fine, but you shouldn&#39;t use this until that&#39;s worked out.
&lt;/p&gt;



&lt;p&gt;
There&#39;s no word on versions for &lt;code&gt;can&lt;/code&gt; or &lt;code&gt;does&lt;/code&gt;.
&lt;/p&gt;




&lt;p&gt;
One of the delightful things to note about this addition is that it is one of the features whose development took place almost entirely through &lt;a href=&quot;https://github.com/Perl/perl5/issues/17200&quot;&gt;a GitHub issue&lt;/a&gt; and &lt;a href=&quot;https://github.com/Perl/perl5/pull/17212&quot;&gt;pull request&lt;/a&gt;. GitHub is now the primary repository for the Perl code, and has been since October 2019. This is a feature that I&#39;ll want to use right away in new production code.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The &lt;code&gt;UNIVERSAL&lt;/code&gt; class, from which all other classes ultimately inherit, provides the basic &lt;code&gt;isa&lt;/code&gt; method. Pronounce that IS-A, like &quot;a cat is a mammal.&quot;  I think the best explanation is in the now-removed Perl Barnyard OO Tutorial (&lt;a href=&quot;https://github.com/Perl/perl5/blob/dbe1c45dfc646f81b41cf322f7c93f0ddca03bfd/pod/perlboot.pod&quot;&gt;perlboot&lt;/a&gt;). It&#39;s also how we explain object-orientation in &lt;a href=&quot;https://www.intermediateperl.com&quot;&gt;&lt;span class=&quot;book-title&quot;&gt;Intermediate Perl&lt;/span&gt;&lt;/a&gt;. We showed &lt;code&gt;isa&lt;/code&gt; in &lt;span class=&quot;book-title&quot;&gt;Effective Perl Programming&lt;/span&gt; too.
&lt;/p&gt;



&lt;p&gt;
One way to use it involves &lt;code&gt;eval&lt;/code&gt; so that a non-object can still return false instead of a method error. If it&#39;s not an object it doesn&#39;t inherit:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( eval { $var-&amp;gt;isa( $some_package ) } ) {
	... do stuff ...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Sometimes you see this in function form to avoid the error if &lt;code&gt;$var&lt;/code&gt; is not an object, but this subverts any overridden &lt;code&gt;isa&lt;/code&gt; methods. Sadly, I think I use this somewhere in &lt;span class=&quot;book-title&quot;&gt;Effective Perl Programming&lt;/span&gt; too:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( UNIVERSAL::isa( $var, $some_package ) ) {
	... do stuff ...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This new infix feature removes some of the syntax and obviates the &lt;code&gt;eval&lt;/code&gt; by returning false if &lt;code&gt;$var&lt;/code&gt; is not an object:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.7;
use warnings;
use feature qw(isa);
no warnings qw(experimental::isa);

if( $var isa $some_package ) {
	... do stuff ...
	}

&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The lefthand side can be anything. If it&#39;s not an object, it&#39;s false. If it&#39;s an object that inherits from the class name on the righthand side, it&#39;s true:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.31.7;
use warnings;
use feature qw(isa);
no warnings qw(experimental::isa);

package Parent { sub new { bless {}, $_[0] } }
package Child  { our @ISA = qw(Parent) }

my $obj = Child-&amp;gt;new;

my @tests = (
	[ &#39;string&#39;,   &#39;Some::Class&#39;,  &#39;Plain string&#39;            ],
	[ 0xDEADBEEF, &quot;Some::Class&quot;,  &#39;Plain number&#39;            ],
	[ undef,      &quot;Some::Class&quot;,  &#39;undef&#39;                   ],
	[ $obj,       &quot;Parent&quot;,       &#39;obj of Parent&#39;           ],
	[ $obj,       &quot;Child&quot;,        &#39;obj of Child&#39;            ],
	[ $obj,       &quot;UNIVERSAL&quot;,    &#39;obj of UNIVERSAL&#39;        ],
	[ $obj,       &quot;Other::Class&quot;, &#39;not obj of Other::Class&#39; ],
	[ [],         &quot;ARRAY&quot;,        &#39;array ref&#39;               ],
	[ {},         &quot;HASH&quot;,         &#39;hash ref&#39;                ],
	);

foreach my $test ( @tests ) {
	my( $candidate, $class, $label ) = $test-&amp;gt;@*;

	say $label if $candidate isa $class;
	}

say &quot;Loaded class, bareword, Parent&quot; if $obj isa Parent;
say &quot;Loaded class, bareword, Child&quot;  if $obj isa Child;

# Normally, a bareword class needs to be loaded. This ignores
# that.
say &quot;Non-loaded class, bareword&quot; if $obj isa Other::Class;

say &quot;Done!&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This is the entire output, warnings (none!) and all. The program goes all the way to the end without die-ing, even though I use a class name that I haven&#39;t defined:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;obj of Parent
obj of Child
obj of UNIVERSAL
Loaded class, bareword, Parent
Loaded class, bareword, Child
Done!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You can see more examples in the test file, &lt;a href=&quot;https://github.com/Perl/perl5/blob/blead/t/op/isa.t&quot;&gt;t/op/isa.t&lt;/a&gt;.
&lt;/p&gt;


&lt;h2&gt;The Problem&lt;/h2&gt;


&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This is fixed in v5.34, but still broken in v5.32.1.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
If you use the new infix &lt;code&gt;isa&lt;/code&gt;, it breaks the normal use of &lt;code&gt;UNIVERSAL::isa&lt;/code&gt;, even outside the scope that enabled it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.32;
use warnings;

package Thingy { }

my $x = bless {}, &#39;Thingy&#39;;

eval {
	use experimental &#39;isa&#39;;
	say &quot;Inside infix: &quot;, $x isa &#39;Thingy&#39;;
	say &quot;Inside -&gt;isa: &quot;, $x-&gt;isa(&#39;Thingy&#39;);
	};
say &quot;AT 1: $@&quot;;

say &quot;Outside -&gt;isa: &quot;, eval { $x-&gt;isa(&#39;Thingy&#39;) };
say &quot;AT 2: $@&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that the method call inside the same scope as the feature fails. It also shows that the method call outside the scope fails.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Inside infix: 1
AT 1: Can&#39;t locate object method &quot;isa&quot; via package &quot;Thingy&quot; at /Users/brian/Desktop/isa.pl line 11.

Outside -&gt;isa:
AT 2: Can&#39;t locate object method &quot;isa&quot; via package &quot;Thingy&quot; at /Users/brian/Desktop/isa.pl line 15.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This appears to happen only within the same file (compilation unit), so a different module that uses it is safe. If you don&#39;t mix both uses in the same file, you might be okay. However, anyone who works on that file needs to know not to mix them, which means they need to know which one you&#39;ve chosen to use. That&#39;s too high of a maintenance burden in most cases.
&lt;/p&gt;


&lt;h2&gt;Things to Remember&lt;/h2&gt;
&lt;ul&gt;
 	&lt;li&gt;&lt;code&gt;UNIVERSAL::isa&lt;/code&gt; subverts overridden &lt;code&gt;isa&lt;/code&gt; methods.&lt;/li&gt;
 	&lt;li&gt;Calling the &lt;code&gt;isa&lt;/code&gt; blows up if the invocant is not an object.&lt;/li&gt;
 	&lt;li&gt;The experimental infix &lt;code&gt;isa&lt;/code&gt; solves these problems.&lt;/li&gt;
	&lt;li&gt;You can&#39;t mix both in the same compilation unit.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl 5.30 fixes single quoted qr&#39;&#39; with \N{}</title>
		<link>https://www.effectiveperlprogramming.com/2019/09/single-quoted-qr-slash-n/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2019/09/single-quoted-qr-slash-n/</guid>
		<pubDate>Sun, 29 Sep 2019 05:03:59 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
The &lt;code&gt;qr//&lt;/code&gt; operator allows you to compile a regular expression without applying it to anything. You get the pattern without the match, and you can reuse the pattern as often as you like. Before v5.30, it had an inconsistency with &lt;code&gt;\N{}&lt;/code&gt; sequences, but that&#39;s fixed now.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
I covered part of this in &lt;a href=&quot;/2012/01/understand-the-order-of-operations-in-double-quoted-contexts/&quot;&gt;Understand the order of operations in double quoted contexts&lt;/a&gt;. The &lt;code&gt;qr//&lt;/code&gt; is a double-quoted context, but you can make it a single-quoted context with the single tick as a delimiter:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $string = &#39;\ABuster\s+&#39;;
say &quot;String: $string&quot;;

# will interpolate
my $double = qr/$string/;
say &quot;Double: $double&quot;;

# will not interpolate
my $single = qr&#39;$string&#39;;
say &quot;Single: $single&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that &lt;code&gt;qr//&lt;/code&gt; interpolates &lt;code&gt;$string&lt;/code&gt;, but &lt;code&gt;qr&#39;&#39;&lt;/code&gt; does not:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;String: \ABuster\s+
Double: (?^:\ABuster\s+)
Single: (?^:$string)
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s another program. This time there&#39;s no interpolation. Each pattern has &lt;code&gt;\N{CAT FACE}&lt;/code&gt; directly in the operator:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $double = qr/\N{CAT FACE}/;
say &quot;Double: $double&quot;;

my $single = qr&#39;\N{CAT FACE}&#39;;
say &quot;Single: $single&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Before v5.30, this was a compile-time error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.28.0 test.pl
\N{NAME} must be resolved by the lexer in regex
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Starting with v5.30.0, it works out. Notice that interpolating it directly translates to its code number, while the single-quoted version hasn&#39;t done that step yet:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.30.0 test.pl
Double: (?^u:\N{U+1F431})
Single: (?^u:\N{CAT FACE})
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>No more false postfix lexical declarations in v5.30</title>
		<link>https://www.effectiveperlprogramming.com/2019/07/no-more-false-postfix-lexical-declarations-in-v5-30/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2019/07/no-more-false-postfix-lexical-declarations-in-v5-30/</guid>
		<pubDate>Tue, 16 Jul 2019 21:00:36 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
Before Perl v5.10 introduced &lt;a href=&quot;https://perldoc.perl.org/functions/state&quot;&gt;state&lt;/a&gt; variables, people did various things to create persistent lexical variables for a subroutine. With v5.30, one of those constructs is now a fatal error.
&lt;/p&gt;



&lt;p&gt;
Often you want a persistent variable to be scoped and private to a subroutine. But, once you leave that scope, normal lexical variables disappear because their reference count drops to zero. So, no persistence.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You could make another scope and declare your variable in that. Your subroutine can see that variable since they are both in the same scope. The subroutine is defined and references the variable, so the variable doesn&#39;t disappear until the subroutine does:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;{
my $persistent;

sub do_something {
	print $persistent++, &quot;\n&quot;;
	}
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s slightly ugly because you use an additional scope and you declare the variable outside of the subroutine. It makes more sense when you want to share a private variable between subroutines (discussed in &lt;a href=&quot;https://www.intermediateperl.com/&quot;&gt;Intermediate Perl&lt;/a&gt; and demonstrated in &lt;a href=&quot;https://metacpan.org/pod/File::Find::Closures&quot;&gt;File::Find::Closures&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;{
my $persistent;

sub increment {
	print ++$persistent, &quot;\n&quot;;
	}

sub decrement {
	print --$persistent, &quot;\n&quot;;
	}
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, there&#39;s a highly discouraged, ancient trick that does the same thing. If you&#39;ve never seen it, that&#39;s a good thing.
&lt;/p&gt;



&lt;p&gt;
Inside your subroutine, declare the lexical variable and attach a false postfix conditional to this:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/Users/brian/bin/perls/perl5.8.9
use strict;
use warnings;

do_something() for 1 .. 5;

sub do_something {
	my $x if 0;
	print $x++, &quot;\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Run this. The variable keeps its value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0
1
2
3
4
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This should have never worked, but it did and people started using it. Instead of breaking the unintended use of a bug, it stuck around. For a long time, Perl tolerated weird and arcane constructs both in the name of backward compatibility and &quot;we have better things to do&quot;.
&lt;/p&gt;



&lt;p&gt;
Change that to a true postfix conditional and you don&#39;t get a persistent variable (because it was a bug that you ever did!):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub do_something {
	my $x if 1;
	print $x++, &quot;\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now it&#39;s a new variable each time so it never remembers the value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0
0
0
0
0
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This was useful for a long time because Perl didn&#39;t have a way to declare persistent, private variables.
&lt;/p&gt;



&lt;p&gt;
But, Perl v5.10 introduced &lt;a href=&quot;https://perldoc.perl.org/functions/state&quot;&gt;state&lt;/a&gt; to do the same thing; it&#39;s executed once then never again:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub do_something {
	state $x;
	print $x++, &quot;\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;The history&lt;/h2&gt;


&lt;p&gt;
Perl v5.10 (released December 2007!) introduced a warning to deprecate this (although you need warnings turned on to see it):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.8.9  -e &#39;my $x if 0&#39;
$ perl5.10.1 -w -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice that it&#39;s only the false conditional. A true conditional gives no warning:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.10.1 -e &#39;my $x if 1&#39;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This deprecation continues to be the case for several years until v5.26 said &quot;no, really this time.&quot; The &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;perl policy&lt;/a&gt; is that you get two minor versions warning before it actually happens (so, after v5.26 and v5.28, v5.30 can remove it):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.12.0 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.14.2 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.16.3 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.18.2 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.20.1 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.22.1 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.24.1 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional at -e line 1.
$ perl5.26.3 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional. This will be a fatal error in Perl 5.30 at -e line 1.
$ perl5.28.1 -e &#39;my $x if 0&#39;
Deprecated use of my() in false conditional. This will be a fatal error in Perl 5.30 at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl v5.30 at last fatalizes the questionable statement:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.30.0 -e &#39;my $x if 0&#39;
This use of my() in false conditional is no longer allowed at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, this isn&#39;t a solution to the entire problem. If you initialize the variable at the same time, you don&#39;t get the error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.30.0 -e &#39;my $x = 0 if 0&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Diagnosis&lt;/h2&gt;


&lt;p&gt;
Do you have this problem? &lt;a href=&quot;https://metacpan.org/pod/Perl::Critic&quot;&gt;Perl::Critic&lt;/a&gt; can tell you:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perlcritic declare-postfix.pl
Variable declared in conditional statement at line 8, column 2.  Declare variables outside of the condition.  (Severity: 5)
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Match only the same Unicode script</title>
		<link>https://www.effectiveperlprogramming.com/2019/03/match-only-the-same-unicode-script/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2019/03/match-only-the-same-unicode-script/</guid>
		<pubDate>Thu, 14 Mar 2019 20:05:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-32</category>
		<description>&lt;p&gt;
Earlier this year, this website was the target of some sort of attack in which a bot sent seemingly random data in its requests. The attack wasn&#39;t that big of a deal since I easily blocked it with Cloudflare, but it was interesting. The apparently random data was actually a mix of Latin, Hangul, and Cyrillic. &lt;a href=&quot;https://shkspr.mobi/blog/2018/11/domain-hacks-with-unusual-unicode-characters/&quot;&gt;Domain hacks with unusual Unicode characters&lt;/a&gt; shows some of these exploits. Curiously, v5.28 added some regex feature that deals with this sort of nonsense.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;Unicode Scripts&lt;/h2&gt;

&lt;p&gt;
Unicode has the notion of &lt;i&gt;&lt;a href=&quot;https://unicode.org/standard/supported.html&quot;&gt;scripts&lt;/a&gt;&lt;/i&gt;—sets of graphical symbols that represent a particular writing system. For example, the characters in &lt;i lang=&quot;el&quot;&gt;Γεια σου κόσμε&lt;/i&gt; are all in the Greek script, and the characters in &lt;i lang=&quot;chr&quot;&gt;ᎣᏏᏲ ᎡᎶᎯ&lt;/i&gt; are all in the Cherokee script. All of those characters are also alphabetic, as well as identifier characters. The &lt;code&gt;\w&lt;/code&gt; matches all of them:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use open qw(:std :utf8);

# https://helloworldcollection.de/#Human
$_ = q(ᎣᏏᏲᎡᎶᎯΓειασουκόσμε);

if( /(\w+)/ ) {
	say &quot;Found: $1&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The capture matches the entire string because every character is a word character:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Found: ᎣᏏᏲᎡᎶᎯΓειασουκόσμε
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Want to find out which scripts a string has? &lt;a href=&quot;https://metacpan.org/pod/Unicode::UCD&quot;&gt;Unicode::UCD (Unicode Character Database) can do that:&lt;/a&gt;
&lt;/p&gt;






&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use open qw(:std :utf8);

use Unicode::UCD qw(charscript);

my $string = &#39;ᎣᏏᏲᎡᎶᎯΓειασουκόσμε&#39;;
my @chars = split //, $string;

foreach my $char ( @chars ) {
	say &quot;$char: &quot;, charscript( ord $char );
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
There&#39;s your mix of Cherokee and Greek:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Ꭳ: Cherokee
Ꮟ: Cherokee
Ᏺ: Cherokee
Ꭱ: Cherokee
Ꮆ: Cherokee
Ꭿ: Cherokee
Γ: Greek
ε: Greek
ι: Greek
α: Greek
σ: Greek
ο: Greek
υ: Greek
κ: Greek
ό: Greek
σ: Greek
μ: Greek
ε: Greek
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Numbers&lt;/h3&gt;

&lt;p&gt;
Here&#39;s another example, with three sets of digits from different scripts:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use open qw(:std :utf8);

$_ = q(۵۲۸528੫੨੮);

if( /(\d+)/ ) {
	say &quot;Found: $1&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This again finds the entire string because every one of the characters is a digit:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Found: ۵۲۸528੫੨੮
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Character class tangent&lt;/h3&gt;

&lt;p&gt;
You may have learned Perl when the character classes were simpler, but now &lt;a href=&quot;/2011/01/know-your-character-classes/&quot;&gt;they have different, more expansive semantics&lt;/a&gt;. The &lt;a href=&quot;/2014/12/enforce-ascii-semantics-when-you-only-want-ascii/&quot;&gt;&lt;code&gt;/a&lt;/code&gt; flag makes the character class shortcuts match with ASCII semantics&lt;/a&gt;, or you could use &lt;code&gt;[0-9]&lt;/code&gt; instead. You can make specific character classes for the numbers in any script that you like, but what if you want to match any run of digits as long as they are in the same script without knowing that script ahead of time?
&lt;/p&gt;


&lt;h2&gt;Script runs&lt;/h2&gt;

&lt;p&gt;
This is where the new &lt;i&gt;script_run&lt;/i&gt; feature comes in. It&#39;s an &lt;a href=&quot;/2019/02/use-alpha-assertions-for-more-understandable-regexes/&quot;&gt;alpha assertion&lt;/a&gt; (also new in v5.28):
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::script_run);

$_ = q(۵۲۸528੫੨੮);

if( /(*script_run:(\d+))/ ) {
	say &quot;Found: $1&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This only finds the digits that belong to the first script it encounters (Arabic in this case):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Found: ۵۲۸
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Now you find all the runs of digits:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::script_run);

$_ = q(۵۲۸528੫੨੮);

my @script_runs = /(*script_run:(\d+))/g;
$&quot; = &quot;\n&quot;;
say &quot;@script_runs&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Now the continuous line of digits is broken into groups in which all the digits are the same script:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;۵۲۸
528
੫੨੮
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;The Common script&lt;/h3&gt;

&lt;p&gt;
There&#39;s a special script named &quot;Common&quot; that can be part of any script run. Characters such as the full stop (U+002E) can be part of any script run, as well as thousands of other characters:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

use utf8;
use open qw(:std :utf8);

my $count = 0;
foreach my $code_number ( 0 .. 0x10FFFD ) {
	next unless chr( $code_number ) =~ m/\p{Common}/;
	say chr( $code_number );
	$count++;
	}
say &quot;There are $count Common Characters&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Any of those characters can be part of the match in a script run without interrupting it:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::script_run);

$_ = &#39;The number is ५.२८&#39;;

say /(*script_run:([\d.]+))/ ? &quot;Found $1&quot; : &quot;No match&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The full stop shows up with the digits from the Devanagari script:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Found ५.२८
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Look-alike spoofing&lt;/h2&gt;

&lt;p&gt;
So, where&#39;s the problem? Can you tell the difference between &lt;i&gt;example.com&lt;/i&gt; and &lt;i&gt;ехамрӏе.com&lt;/i&gt;? Assuming that something didn&#39;t mess with my original source, the first is ASCII, and the second is a mix of ASCII and similar looking Cyrillic letters. Both &lt;i&gt;.com&lt;/i&gt;s are in ASCII, but the domains are different. Indeed, the attack against this site was sending this sort of data. If I wanted to match a domain name:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::script_run);

my $expected = &#39;example.com&#39;;  # All Latin
my $spoofer  = &#39;ехамрӏе.com&#39;;  # some Cyrillic

my @domains = ( $expected, $spoofer );

my @naive_match = map { /\A[\w.]+\z/ } @domains;
say &#39;Matched &#39; . @naive_match . &#39; domains&#39;;

my @run_match = map { /\A(*script_run:[\w.]+)\z/ } @domains;
say &#39;Matched &#39; . @run_match . &#39; domains&#39;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The naïve match passes through both domains, while the script run match doesn&#39;t. Note that you need the anchors because the script run would find the run of the first script it encounters and still match just as it did for the numeric example:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Matched 2 domains
Matched 1 domains
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Use atomic matching for complex non-backtracking</title>
		<link>https://www.effectiveperlprogramming.com/2019/03/use-atomic-matching-for-complex-non-backtracking/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2019/03/use-atomic-matching-for-complex-non-backtracking/</guid>
		<pubDate>Tue, 12 Mar 2019 00:18:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-10</category>
		<description>&lt;p&gt;
You can sometimes improve the performance of your regular expression by preventing parts of it from backtracking when you know that might be useful. &lt;span class=&quot;item&quot;&gt;Item 38. Avoid unnecessary backtracking&lt;/span&gt; had many techniques for this, although it did not mention atomic matching (a feature added in v5.005).
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
An &lt;i&gt;atomic match&lt;/i&gt; treats a subpattern as a single unit. That entire unit matches or it doesn&#39;t and it doesn&#39;t allow the regex engine to backtrack into it.
&lt;/p&gt;



&lt;p&gt;
Consider a pattern that matches some text, some whitespace, and some more text, where I&#39;m using the &lt;code&gt;/x&lt;/code&gt; to spread out the pattern with insignificant whitespace (&lt;span class=&quot;item&quot;&gt;Item 37. Make regular expressions readable.&lt;/span&gt;):
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $pattern = qr/ \A a \s+ b /x;

$_ = &quot;a\t\t\t\tc&quot;;
say /$pattern/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Just looking at that, you can tell that the match will fail because there are no &lt;code&gt;b&lt;/code&gt; characters in it. It takes a while for the regex engine to figure out that.
&lt;/p&gt;

&lt;ul&gt;
 	&lt;li&gt;The pattern starts at the beginning of the string and matches the &lt;i&gt;a&lt;/i&gt;&lt;/li&gt;
 	&lt;li&gt;After the &lt;i&gt;a&lt;/i&gt;, the &lt;code&gt;\s+&lt;/code&gt; matches all of the tab characters.&lt;/li&gt;
 	&lt;li&gt;Next the pattern needs to match a &lt;i&gt;b&lt;/i&gt;. It doesn&#39;t match that though.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
When the &lt;code&gt;b&lt;/code&gt; part fails, the engine backs up to the previous quantifier. In this example, it matched four characters, so the engine reduces that to matching three characters then tries again. That fails because the tab character is not a &lt;i&gt;b&lt;/i&gt;. The quantifier gives up another character, but the penultimate tab is also not a &lt;i&gt;b&lt;/i&gt;. This continues until the quantifier cannot give up anything more since it has to match at least one tab.
&lt;/p&gt;



&lt;p&gt;
The &lt;code&gt;rxrx&lt;/code&gt; program from &lt;a href=&quot;https://metacpan.org/pod/Regexp::Debugger&quot;&gt;Regexp::Debugger&lt;/a&gt; helps you visualize this, with the caveat that it modifies your regex so it can probe it, so take that into account when &lt;code&gt;rxrx&lt;/code&gt; reports these steps. It reports that it takes 33 steps before the regex engine gives up.
&lt;/p&gt;


&lt;video class=&quot;d-block mx-auto&quot; controls=&quot;&quot; width=&quot;600&quot; title=&quot;rxrx stepping through the unhindered match, which fails in 33 steps&quot;&gt;&lt;source src=&quot;/videos/atomic/unhindered.mp4&quot; type=&quot;video/mp4&quot;&gt;&lt;/video&gt;


&lt;p&gt;
One way to get around this is to add the non-backtracking modifier to the &lt;code&gt;+&lt;/code&gt; quantifier, making it the &lt;code&gt;++&lt;/code&gt;:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $pattern = qr/ \A a \s++ b /x;

$_ = &quot;a\t\t\t\tc&quot;;
say /$pattern/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This shaves off a few steps because the &lt;code&gt;++&lt;/code&gt; quantifier doesn&#39;t backtrack. It gives up everything it matched and starts again from the beginning of the pattern. The pattern moves over a character and tries the entire pattern again (a move that an anchor would solve, but that&#39;s not the point here). This time it fails in 25 steps. Imagine all of these with much later patterns and many more tabs. The backtracking can be pathological.
&lt;/p&gt;


&lt;video class=&quot;d-block mx-auto&quot; controls=&quot;&quot; width=&quot;600&quot; title=&quot;rxrx stepping through the non-backtracking match, which fails in 25 steps&quot;&gt;&lt;source src=&quot;/videos/atomic/non-backtracking.mp4&quot; type=&quot;video/mp4&quot;&gt;&lt;/video&gt;


&lt;p&gt;
Another way is the atomic match group, &lt;code&gt;(?&amp;gt; ... )&lt;/code&gt;. This also prevents the regex engine from backtracking, although this time it fails in 30 steps:
&lt;/p&gt;


&lt;video class=&quot;d-block mx-auto&quot; controls=&quot;&quot; width=&quot;600&quot; title=&quot;rxrx stepping through the atomic group match, which fails in 30 steps&quot;&gt;&lt;source src=&quot;/videos/atomic/atomic-simple.mp4&quot; type=&quot;video/mp4&quot;&gt;&lt;/video&gt;


&lt;p&gt;
But this is too simple for the atomic match because it&#39;s grouping a single thing with a quantifier. It&#39;s better when you want to group something that&#39;s complex itself. Here&#39;s an only slightly less simple pattern that has two zero-or-more quantifiers, which fails very quickly:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $pattern = qr/ \A a (?&amp;gt; b* c* ) d /x;

$_ = &quot;abbbbccccz&quot;;
say /$pattern/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The backtracking inside a sequence of zero-or-more constructs can quickly get out of hand. Imagine a target string with thousands of &lt;i&gt;b&lt;/i&gt;s followed by thousands of &lt;i&gt;c&lt;/i&gt;s, but with no &lt;i&gt;d&lt;/i&gt; in the string:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $pattern = qr/ \A a b* c* d /x;

my $b = &#39;b&#39; x 1_000;
my $c = &#39;c&#39; x 1_000;

$_ = &quot;a${b}${c}z&quot;;
say /$pattern/ ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The regex engine will backtrack repeatedly through those 2,000 characters.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Use alpha assertions for more understandable regexes</title>
		<link>https://www.effectiveperlprogramming.com/2019/02/use-alpha-assertions-for-more-understandable-regexes/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2019/02/use-alpha-assertions-for-more-understandable-regexes/</guid>
		<pubDate>Wed, 27 Feb 2019 04:00:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This feature stabilizes in Perl v5.32.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.28 adds more-readable, alternate spelled-out forms for some of its regular expression extended patterns. Then, to make those slightly less readable, there are very short initialisms for those. Although these might seem superfluous now, the ability to define new syntax without relying on the limited number of ASCII symbols will pay off later.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
These are experimental features but you don&#39;t need to enable them although you can &lt;a href=&quot;/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/&quot;&gt;turn off their warnings&lt;/a&gt;:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;
no warnings qw(experimental::alpha_assertions);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You now have additional ways to specify the same things. I won&#39;t explain these here, but I do cover lookarounds in &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt;.
&lt;/p&gt;

&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Symbolic&lt;/th&gt;
&lt;th&gt;Alpha Assertion&lt;/th&gt;
&lt;th&gt;Shortcut&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Positive lookahead&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(?=...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*positive_lookahead:...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*pla:...)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negative lookahead&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(?!...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*negative_lookahead:...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*nla:...)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Positive lookbehind&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(?&amp;lt;=...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*positive_lookbehind:...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*plb:...)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negative lookbehind&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(?&amp;lt;!...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*negative_lookbehind:...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*nlb:...)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Atomic&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(?&amp;gt;...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(*atomic:...)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Without the alpha assertion, you&#39;d write a positive lookahead assertion. Here&#39;s an example that finds &quot;camel&quot; and &quot;fox&quot; in a string without caring which order they appear:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_          = &#39;The quick brown fox jumps over the lazy camel&#39;;
my $pattern = qr/(?=.*camel).*fox/;

say &quot;Matched!&quot; if /$pattern/;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
With the alpha assertion, the pattern exchanges the symbols for letters:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;
no warnings qw(experimental::alpha_assertions);

$_          = &#39;The quick brown fox jumps over the lazy camel&#39;;
my $pattern = qr/(*positive_lookahead:.*camel).*fox/;
# OR: my $pattern = qr/(*pla:.*camel).*fox/;

say &quot;Matched!&quot; if /$pattern/;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
These make more sense with extended patterns so it&#39;s easier to see the parts of the pattern (and to give room to the longer syntax):
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;
no warnings qw(experimental::alpha_assertions);

$_          = &#39;The quick brown fox jumps over the lazy camel&#39;;
my $pattern = qr/
	(*positive_lookahead:.*camel)
	.*fox
	/x;

say &quot;Matched!&quot; if /$pattern/;
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Perl v5.30 new features</title>
		<link>https://www.effectiveperlprogramming.com/2018/12/perl-v5-30-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/12/perl-v5-30-new-features/</guid>
		<pubDate>Tue, 04 Dec 2018 23:19:33 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
Perl v5.29 is the development series leading up to the maintenance release v5.30 sometime in the middle of 2019. As it&#39;s released—roughly monthly—you can get a peek at what&#39;s coming up. You can track the progress by reading the &lt;i&gt;perldelta&lt;/i&gt; documentation that comes with each &lt;a href=&quot;https://metacpan.org/pod/distribution/perl/pod/perldelta.pod&quot;&gt;Perl release&lt;/a&gt; (although you&#39;ll need to select the development version you want to inspect).
&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2019/07/no-more-false-postfix-lexical-declarations-in-v5-30/&quot;&gt;You no longer can declare lexical with false postfix conditionals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2018/12/perl-v5-30-lets-you-match-more-with-the-general-quantifier&quot;&gt;The generalized quantifier can match more&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Delimiters must be graphemes&lt;/li&gt;
&lt;li&gt;File::Glob::glob() will disappear&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/04/you-must-escape-the-left-brace-in-a-regex/&quot;&gt;Some uses of an unescaped left brace &quot;{&quot; in a regex will be illegal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Upgrades to Unicode 12&lt;/li&gt;
&lt;li&gt;Previously deprecated sysread()/syswrite() on :utf8 handles now fatal&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2019/09/single-quoted-qr-slash-n/&quot;&gt;&lt;code&gt;qr&#39;\N{name}&#39;&lt;/code&gt; is now supported&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2020/03/use-a-variable-width-lookbehind-if-it-wont-match-more-than-255-characters/&quot;&gt;There&#39;s limited, experimental support for variable-width lookbehinds&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2020/07/match-unicode-property-values-with-a-wildcard/&quot;&gt;Match Unicode property values with a wildcard&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl v5.30 lets you match more with the general quantifier</title>
		<link>https://www.effectiveperlprogramming.com/2018/12/perl-v5-30-lets-you-match-more-with-the-general-quantifier/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/12/perl-v5-30-lets-you-match-more-with-the-general-quantifier/</guid>
		<pubDate>Mon, 03 Dec 2018 06:15:17 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-30</category>
		<description>&lt;p&gt;
Does the &lt;code&gt;{N,}&lt;/code&gt; really match infinite repetitions in a Perl regular expression? No, it never has. You&#39;ve been limited to 32,766 repetitions. Perl v5.30 is about to double that for you. And, if you are one of the people who needed more, I&#39;d like to hear your story.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
That 32,766 is one less than the maximum 15-bit number (16 bits with one reserved for sign):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;print 0b0111_1111_1111_1111&#39;
32767
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Reading the &lt;i&gt;perldelta&lt;/i&gt; for v5.29.4 (the development track leading to v5.30), I saw that you&#39;re about to get double that.
&lt;/p&gt;




&lt;p&gt;
Do you need that sign bit? What if you put a negative number in there? As a literal, &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; warns you:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;/.{0,1}/&#39;
$ perl -e &#39;/.{0,-1}/&#39;
Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,-1}/ at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Read that warning! Since this isn&#39;t a valid quantifier, the regex engine thinks that the &lt;code&gt;{&lt;/code&gt; is a literal left brace. That&#39;s allowed now but has been deprecated. Perl v5.26 tried to do this but we found out that &lt;span class=&quot;binary&quot;&gt;autoconf&lt;/span&gt; was using it. This time for sure!
&lt;/p&gt;



&lt;p&gt;
Curiously, this passes a syntax check:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -c -e &#39;/.{0,-1}/&#39;
Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,-1}/ at -e line 1.
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What if it&#39;s in a variable? With a positive value it&#39;s no problem. With a negative value you get the warning again:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;$n = 1; /.{0,$n}/&#39;
$ perl -e &#39;$n = -1; /.{0,$n}/&#39;
Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,-1}/ at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Try it without a value specified in the literal code. The &lt;code&gt;--&lt;/code&gt; is there to end the argument list so the shell doesn&#39;t think that &lt;code&gt;-1&lt;/code&gt; is a command-line option:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;$n = shift; /.{0,$n}/&#39; 1
$ perl -e &#39;$n = shift; /.{0,$n}/&#39; -- -1
Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,-1}/ at -e line 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Trying to match too much&lt;/h2&gt;


&lt;p&gt;
The current situation is a bit different if you go past the upper bound. You still get a runtime error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;$n = shift; /.{0,$n}/&#39; 32766
$ perl -e &#39;$n = shift; /.{0,$n}/&#39; 32767
Quantifier in {,} bigger than 32766 in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,32767}/ at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
&lt;i&gt;It&#39;s important to note here that I&#39;m running this on macOS 10.14 using Apple&#39;s compiler tools. Your system or compiler might choose different values.&lt;/i&gt;
&lt;/p&gt;



&lt;p&gt;
The v5.29.5 docs say:
&lt;/p&gt;


&lt;blockquote&gt;&lt;b&gt;The maximum number of times a pattern can match has been doubled to 65535&lt;/b&gt;


&lt;p&gt;
This means if you specify &lt;code&gt;qr/a+/&lt;/code&gt; that there can be anywhere from 1 through 65535 &lt;code&gt;&quot;a&quot;&lt;/code&gt;&#39;s in a row, instead of 32267 as previously.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
You can force &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; to tell you what the limit is by doubling the number each time. The number you get here is much smaller than the one I discovered:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -e &#39;$_ **= $_ , / {1,$_} / for 2 .. 42;&#39;
Quantifier in {,} bigger than 32766 in regex; marked by &amp;lt;-- HERE in m/ { &amp;lt;-- HERE 46656} / at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Let&#39;s look at that next.
&lt;/p&gt;


&lt;h2&gt;The + quantifier&lt;/h2&gt;


&lt;p&gt;
The &lt;code&gt;+&lt;/code&gt; can be represented as the general quantifier &lt;code&gt;{1,}&lt;/code&gt;. That&#39;s how we present that in &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt;. Here&#39;s an extract of &lt;a href=&quot;https://perldoc.perl.org/perlre&quot;&gt;perlre&lt;/a&gt;:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
&lt;p&gt;
The &quot;*&quot; quantifier is equivalent to {0,} , the &quot;+&quot; quantifier to {1,} , and the &quot;?&quot; quantifier to {0,1} . n and m are limited to non-negative integral values less than a preset limit defined when perl is built. This is usually 32766 on the most common platforms.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
Does that have the same limit? Are they actually the same?
&lt;/p&gt;



&lt;p&gt;
Let&#39;s try that in a program. First, I&#39;ll make a small string that only has the letter &quot;a&quot; then try to match that it only has the letter &quot;a&quot;. The &lt;code&gt;\A&lt;/code&gt; matches only at the absolute beginning of the string and the &lt;code&gt;\z&lt;/code&gt; matches only at the absolute end of the string (the &lt;code&gt;\Z&lt;/code&gt; allows a trailing newline):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

my $length = $ARGV[0] // 10;
my $string = &#39;a&#39; x $length;
say $string =~ /\A a+ \z/x ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This regex should match anything that only has &quot;a&quot; between the absolute beginning and end of the string. The quantifier must match everything up to the end of the string. I run this and it matches, as I expected:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.28.0 long_a.pl
Matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The dangerous part here is that I&#39;ve confirmed that my regex works. Now that I&#39;ve convinced myself that my program works, I&#39;ll take that length from the command line. The &lt;code&gt;+&lt;/code&gt; can match much more than the 32,766 or 65,535:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl long_a.pl 10
Matched
$ perl long_a.pl 32766
Matched
$ perl long_a.pl 32767
Matched
$ perl5.28.0 long_a.pl 2147483647
String length is 2147483647
Matched
$ perl5.28.0 long_a.pl 2147483648
String length is 2147483648
Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Curious! It&#39;s not equivalent to &lt;code&gt;{1,}&lt;/code&gt;. I match much, much more that way. Again, if this is an important feature for you, I want to hear about it. Who&#39;s matching that many repetitions?
&lt;/p&gt;



&lt;p&gt;
Does that 2147483647 number look special? It&#39;s half of the 32-bit maximum:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -le &#39;print 0xFFFFFFFF / 2&#39;
2147483647.5
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Trying it with v5.29.5&lt;/h2&gt;


&lt;p&gt;
Now try the same thing with the latest development &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt;. With v5.29.5, the previous command lines can handle up to 65,535:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.29.5 -e &#39;$n = shift; /.{0,$n}/&#39; 32767
$ perl5.29.5 -e &#39;$n = shift; /.{0,$n}/&#39; 65355
$ perl5.29.5 -e &#39;$n = shift; /.{0,$n}/&#39; 65356
$ perl5.29.5 -e &#39;$n = shift; /.{0,$n}/&#39; 65535
Quantifier in {,} bigger than 65534 in regex; marked by &amp;lt;-- HERE in m/.{ &amp;lt;-- HERE 0,65535}/ at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What about the &lt;code&gt;+&lt;/code&gt;? The behavior stays the same:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.29.5 long_a.pl 10
Matched
$ perl5.29.5 long_a.pl 32766
Matched
$ perl5.29.5 long_a.pl 2147483647
Matched
$ perl5.29.5 long_a.pl 2147483648
Missed
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The general quantifier &lt;code&gt;{,}&lt;/code&gt; has a maximum number of repetitions, even though that is probably more than you ever need.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;+&lt;/code&gt; should be equivalent, but can match much more.&lt;/li&gt;
&lt;li&gt;v5.30 will increase the limit for &lt;code&gt;{,}&lt;/code&gt;. If you&#39;ve been working around that limitation, I want to hear about what you are doing.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use @{^CAPTURE} to get a list of all the capture buffers</title>
		<link>https://www.effectiveperlprogramming.com/2018/10/use-capture-to-get-a-list-of-all-the-capture-buffers/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/10/use-capture-to-get-a-list-of-all-the-capture-buffers/</guid>
		<pubDate>Wed, 03 Oct 2018 16:14:20 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Perl v5.26 adds three new special variables related to captures. The &lt;code&gt;@{^CAPTURE}&lt;/code&gt; is an array of all the capture buffers. &lt;code&gt;%{^CAPTURE}&lt;/code&gt; is an alias for &lt;code&gt;%+&lt;/code&gt; and stores the actually-matched named capture labels as its keys. &lt;code&gt;%{^CAPTURE_ALL}&lt;/code&gt; is an alias for &lt;code&gt;%-&lt;/code&gt; and stores all the named capture labels and their matched (or not) values.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;@{^CAPTURE}&lt;/h2&gt;


&lt;p&gt;
I started the first edition of &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt; with an example that used &lt;code&gt;@+&lt;/code&gt; and &lt;code&gt;@-&lt;/code&gt;. These variables hold the position offsets for the capture buffers. You can use the length of these arrays to figure out the number of capture buffers. You can also use those with &lt;code&gt;substr&lt;/code&gt; to extract the proper substrings. Here&#39;s a pattern made up of two other patterns that you might not know ahead of time:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &amp;lt;&amp;lt;~&#39;HERE&#39;;
Buster the cat
Nikki the dog
HERE

my $pattern = qr/(.+?) the (.+?)\R/;

if( /$pattern$pattern/ ) {
	my @captures;
	foreach my $i ( 1 .. $#+ ) {
		push @captures, substr( $_,
			$-[$i],
			$+[$i] - $-[$i]
			);
		}
	say &quot;&amp;lt;@captures&gt;&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice that the first index in that example is 1. The offsets in position 0 correspond to &lt;code&gt;$&amp;&lt;/code&gt;, the overall match. Here&#39;s the output:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;&amp;lt;Buster cat Nikki dog&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might do this with a complicated pattern where you don&#39;t know how many captures there are or you don&#39;t want to recount every time you change the pattern. But, ignore that for now because you don&#39;t need to do that anymore.
&lt;/p&gt;



&lt;p&gt;
In v5.26 you don&#39;t need to do all of that nonsense. You get an array of all the capture buffers in &lt;code&gt;@{^CAPTURE}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &amp;lt;&amp;lt;~&#39;HERE&#39;;
Buster the cat
Nikki the dog
HERE

my $pattern = qr/(.+?) the (.+?)\R/;

if( /$pattern$pattern/ ) {
	say &quot;&amp;lt;@{^CAPTURE}&gt;&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now note that the equivalent of &lt;code&gt;$&amp;&lt;/code&gt; isn&#39;t in &lt;code&gt;@{^CAPTURE}&lt;/code&gt;. The output is the same as before.
&lt;/p&gt;


&lt;h2&gt;%{^CAPTURE}, or, %+&lt;/h2&gt;


&lt;p&gt;
The &lt;code&gt;%+&lt;/code&gt; hash has the named capture labels as keys (and it&#39;s mostly the keys that are important here):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Dumper;

$_ = &lt;&lt;~&#39;HERE&#39;;
Buster the cat
Nikki the dog
HERE

my $pattern = qr/(?&amp;lt;name&gt;.+?) the (?&amp;lt;animal&gt;.+?)\R/;

if( /$pattern$pattern/ ) {
	say Dumper( \%+ );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows the first match for each of the named labels (you&#39;ll fix that in a minute):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
          &#39;name&#39; =&gt; &#39;Buster&#39;,
          &#39;animal&#39; =&gt; &#39;cat&#39;
        };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;%+&lt;/code&gt; hash only has keys for the named captures that actually matched. This pattern has a named capture label that won&#39;t match:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Dumper;

$_ = &lt;&lt;~&#39;HERE&#39;;
Buster the cat
Nikki the dog
HERE

my $pattern = qr/
	(?&amp;lt;cat&gt;.+?)  \s the \s cat \R |
	(?&amp;lt;dog&gt;.+?)  \s the \s dog \R |
	(?&amp;lt;bird&gt;.+?) \s the \s bird \R
	/x;

if( /$pattern$pattern/ ) {
	say Dumper( \%+ );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows only the labels that match (and still only the first value that matched):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
          &#39;cat&#39; =&gt; &#39;Buster&#39;,
          &#39;dog&#39; =&gt; &#39;Nikki&#39;
        };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.26, you can give that special variable a name:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( /$pattern$pattern/ ) {
	say Dumper( \%{^CAPTURE} );
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;%{^CAPTURE_ALL}, or, %-&lt;/h2&gt;


&lt;p&gt;
The &lt;code&gt;%-&lt;/code&gt; hash is more expansive than &lt;code&gt;%+&lt;/code&gt;. It has keys for every defined label:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Dumper;

$_ = &amp;lt;&amp;lt;~&#39;HERE&#39;;
Buster the cat
Nikki the dog
HERE

my $pattern = qr/
	(?&amp;lt;cat&gt;.+?)  \s the \s cat \R |
	(?&amp;lt;dog&gt;.+?)  \s the \s dog \R |
	(?&amp;lt;bird&gt;.+?) \s the \s bird \R
	/x;

if( /$pattern$pattern/ ) {
	say Dumper( \%- );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the output has entries for every named capture and an array reference for the value. That array has an entry for every place that label could have matched. Where it didn&#39;t match there&#39;s an &lt;code&gt;undef&lt;/code&gt; in the array:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
          &#39;dog&#39; =&gt; [
                     undef,
                     &#39;Nikki&#39;
                   ],
          &#39;cat&#39; =&gt; [
                     &#39;Buster&#39;,
                     undef
                   ],
          &#39;bird&#39; =&gt; [
                      undef,
                      undef
                    ]
        };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now, instead of &lt;code&gt;%-&lt;/code&gt; you can use &lt;code&gt;%{^CAPTURE_ALL}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( /$pattern$pattern/ ) {
	say Dumper( \%{^CAPTURE_ALL} );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That provides an easy way to tell them apart. There&#39;s one variable for the named capture labels and there&#39;s one for everything.
&lt;/p&gt;


&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Perl v5.26 adds one new special variable and two special variable aliases.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@{^CAPTURE}&lt;/code&gt; is an array of all the capture buffers.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;%{^CAPTURE}&lt;/code&gt; is an alias for &lt;code&gt;%+&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;%{^CAPTURE_ALL}&lt;/code&gt; is an alias for &lt;code&gt;%-&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Undef a scalar to release its memory</title>
		<link>https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/</guid>
		<pubDate>Wed, 19 Sep 2018 05:32:01 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>item</category>
		<description>&lt;p&gt;
When you store a large string in a scalar, &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; allocates the memory to store that string and associate it with the scalar. It uses the same memory even if you assign a much shorter value to the same scalar. Use the functional form of &lt;code&gt;undef&lt;/code&gt; to let &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; reuse that memory for something else. This is important when you want to reuse the variable or the lifetime of the variable is very long.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
&lt;a href=&quot;https://jberger.github.io&quot;&gt;&lt;span class=&quot;person&quot;&gt;Joel Berger&lt;/span&gt;&lt;/a&gt; told me about this trick at the &lt;a href=&quot;https://oslo.pm/npw2018/&quot;&gt;Nordic Perl Workshop&lt;/a&gt; when we were talking about non-blocking reads from a stream where a line might be much longer than the underlying IO buffer size. You&#39;d need to store the partial line in a buffer until the rest of it arrived. The problem is that one large line could force the interpreter to use a huge chunk of memory that it wouldn&#39;t reuse for anything else. Do this enough in a long-running program and you have a leak problem. 
&lt;/p&gt;



&lt;p&gt;
&lt;span class=&quot;person&quot; data-fullname=&quot;Joel Berger&quot;&gt;Joel&lt;/span&gt; pointed me to &lt;a href=&quot;https://github.com/mojolicious/mojo/issues/1256&quot;&gt;Mojo issue 1256&lt;/a&gt; that reported such a problem with &lt;a href=&quot;https://metacpan.org/pod/Mojo::IOLoop::Stream&quot;&gt;Mojo::IOLoop::Stream&lt;/a&gt;. A buffer variable would hold incoming data before it went on its way. That variable stuck around instead of going out of scope so it could store data between reads. If one iteration put a much larger string than normal the scalar wouldn&#39;t give it up. &lt;a href=&quot;https://github.com/leonerd&quot;&gt;&lt;span class=&quot;person&quot;&gt;Paul Evans&lt;/span&gt;&lt;/a&gt; showed the way to fix it.
&lt;/p&gt;



&lt;p&gt;
Consider some string that stores some large string. The interpreter allocates and handles all of the memory to hold that string and point the scalar at it. &lt;a href=&quot;https://metacpan.org/pod/Devel::Peek&quot;&gt;Devel::Peek&lt;/a&gt;&#39;s &lt;code&gt;Dump&lt;/code&gt; can show you the low-level details of a variable:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows the scalar data structure (the SV) and the pointer value (PV) that it points to. The &lt;code&gt;CUR&lt;/code&gt; shows the number of octets used and &lt;code&gt;LEN&lt;/code&gt; shows the number allocated. This isn&#39;t surprising:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7fcbba003e70) at 0x7fcbba01bc80
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0x10834b000 &quot;BusterBusterBusterBu&quot;...\0
  CUR = 6000000
  LEN = 6000002
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
After you use that large string you might replace it with a smaller string. You don&#39;t need all that memory and you might expect that Perl will free some of it since the long string is gone:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
$buffer = &#39;Buster&#39;;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s not the case. You had a long string there and you might have a long string again so the interpreter keeps that pointer just in case. The &lt;code&gt;LEN&lt;/code&gt; is the same although only six octets are used. This ties up a bunch of memory:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7fb957002e70) at 0x7fb957817880
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0x102936000 &quot;Buster&quot;\0
  CUR = 6
  LEN = 6000002
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
An empty string is as good a string as any other. You might think that would release the memory:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
$buffer = &#39;&#39;;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the length is 0 but all the memory is still there. The &lt;code&gt;LEN&lt;/code&gt; hasn&#39;t changed:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7fc28e003e70) at 0x7fc28e01bc80
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0x10ee99000 &quot;&quot;\0
  CUR = 0
  LEN = 6000002
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Assigning &lt;code&gt;undef&lt;/code&gt; does a slightly different thing:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
$buffer = undef;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The pointer still holds the old string but the &lt;code&gt;POK&lt;/code&gt; and &lt;code&gt;pPOK&lt;/code&gt; flags have disappeared. That signals that the SV can&#39;t trust the value in the pointer even though it hasn&#39;t changed it.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7fed79003e70) at 0x7fed79806e80
  REFCNT = 1
  FLAGS = ()
  PV = 0x108347000 &quot;BusterBusterBusterBu&quot;...\0
  CUR = 6000000
  LEN = 6000002
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Assign another value to &lt;code&gt;$buffer&lt;/code&gt; after you assigned &lt;code&gt;undef&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
$buffer = undef;
$buffer = &#39;Ginger&#39;;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Setting the new value reuses the same memory and restores the flags that signal that the value is okay to use:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7f8fa4803e70) at 0x7f8fa481bc80
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0x101ec1000 &quot;Ginger&quot;\0
  CUR = 6
  LEN = 6000002
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To release that memory, use &lt;code&gt;undef&lt;/code&gt; in its functional form rather than assigning it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;
$Devel::Peek::pv_limit = 20; # truncate the string in output

my $buffer = &#39;Buster&#39; x 1e6;
Dump $buffer;

undef $buffer;
Dump $buffer;

$buffer = &#39;Ginger&#39;;
Dump $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
After the &lt;code&gt;undef&lt;/code&gt; there&#39;s no PV. When you assign a new value, the interpreter can use different memory for it. Your process&#39;s memory footprint probably will not get smaller but the interpreter can use that memory for something else. 
&lt;/p&gt;



&lt;p&gt;
In the last part of the output you see that the &lt;code&gt;LEN&lt;/code&gt; is a more appropriate value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x7ff0ae803e70) at 0x7ff0af007280
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0x10c87d000 &quot;BusterBusterBusterBu&quot;...\0
  CUR = 6000000
  LEN = 6000002
SV = PV(0x7ff0ae803e70) at 0x7ff0af007280
  REFCNT = 1
  FLAGS = ()
  PV = 0
SV = PV(0x7ff0ae803e70) at 0x7ff0af007280
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0x7ff0ae60b0f0 &quot;Ginger&quot;\0
  CUR = 6
  LEN = 10
  COW_REFCNT = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Perl manages the memory for you and will keep big chunks of memory around just in case.&lt;/li&gt;
&lt;li&gt;Assigning a new value, including the empty string or &lt;code&gt;undef&lt;/code&gt; doesn&#39;t necessarily release the memory that scalar uses.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;undef&lt;/code&gt; in the functional form releases the pointer from the scalar and you&#39;ll get a new PV when you assign another value.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Mix assignment and reference aliasing with declared_refs</title>
		<link>https://www.effectiveperlprogramming.com/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/</guid>
		<pubDate>Tue, 11 Sep 2018 21:23:05 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Perl v5.26 adds the experimental &lt;code&gt;declared_refs&lt;/code&gt; feature that expands on the experimental &lt;a href=&quot;/2015/08/create-named-variable-aliases-with-ref-aliasing/&quot;&gt;&lt;code&gt;refaliasing&lt;/code&gt; feature from v5.22&lt;/a&gt;. As with all experimental features, this may change or disappear according to &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;perlpolicy&lt;/a&gt;. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The &lt;code&gt;refaliasing&lt;/code&gt; allows you to alias a named variable to a reference (even if that reference comes from a named variable). You could write that as either of these forms:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use experimental qw(refaliasing);

\my @new = \@old;

my @new;
\@new = \@old;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In the first, you declare and assign in the same statement. The reference operator (&lt;code&gt;\&lt;/code&gt;) comes before the &lt;code&gt;my&lt;/code&gt;. In the second, you declare the new variable in one statement then alias in another.
&lt;/p&gt;



&lt;p&gt;
The new &lt;code&gt;declared_refs&lt;/code&gt; feature lets you move the reference operator to after the declarator (the &lt;code&gt;my&lt;/code&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
use experimental qw(refaliasing declared_refs);

my \@new = \@old;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This means that list assignments with a mix of ref aliasing and conventional assignments are now possible. This mix is not possible with just the v5.22 feature: 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use experimental qw(refaliasing declared_refs);

my @cats = qw( Buster Ginger );
my %pets = (
	&#39;Buster&#39; =&gt; &#39;cat&#39;,
	&#39;Ginger&#39; =&gt; &#39;cat&#39;,
	&#39;Addy&#39;   =&gt; &#39;dog&#39;,
	&#39;Nikki&#39;  =&gt; &#39;dog&#39;,
	);

my( $n, $m, \@y, \%z ) = ( &#39;Addy&#39;, &#39;Nikki&#39;, \@cats, \%pets );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This still doesn&#39;t get you to ref aliasing in &lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt;subroutine signatures&lt;/a&gt; but it&#39;s a step closer to what you need for that.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Use Unicode 10 in Perl v5.28</title>
		<link>https://www.effectiveperlprogramming.com/2018/08/use-unicode-10-in-perl-v5-28/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/08/use-unicode-10-in-perl-v5-28/</guid>
		<pubDate>Mon, 27 Aug 2018 21:06:16 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;p&gt;
Perl v5.28 updates to Unicode 10. There are 8,518 new characters, 7,473 of which are in the CJK extension. There are 56 new emojis. And, the Bitcoin symbol, ₿. It adds a &lt;span class=&quot;scientific-name&quot;&gt;T. rex&lt;/span&gt;, 🦖, but we&#39;re still waiting for &lt;a href=&quot;https://github.com/kraih/perl-raptor&quot;&gt;a raptor&lt;/a&gt;. To Perl they are just characters like any other so you don&#39;t need anything new to deal with them.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You can find the count of the new characters as you saw in &lt;a href=&quot;/2018/08/find-the-new-emojis-in-perls-unicode-support/&quot;&gt;Find the new emojis in Perl’s Unicode support&lt;/a&gt; but with an updated Unicode version number:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;
use utf8;
use strict;
use warnings;

my $count = 0;
foreach ( 0 .. 0x10FFFF ) {
	my $char = chr;
	next if $char =~ /\p{Present_In: 9.0}/;
	next if $char =~ /\p{Unassigned}/;
	$count++;
	}
say &quot;There are $count new chars&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s the new count of characters:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;There are 8518 new chars
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The addition of the Bitcoin symbol enhances your Bitcoin accounting software:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;printf &quot;I lost 100 ₿, which is %.2f dollars\n&quot;, rand(time);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The exciting additions are the emojis (I guess). The Mojo program from the previous post needs a slight update for the URL and selectors:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use charnames qw();

use Mojo::UserAgent;
my $ua = Mojo::UserAgent-&gt;new;

my $url = &#39;https://blog.emojipedia.org/whats-new-in-unicode-10/&#39;;
my $tx = $ua-&gt;get( $url );

die &quot;That didn&#39;t work!\n&quot; unless $tx-&gt;success;

say $tx-&gt;result
	-&gt;dom
	-&gt;find( &#39;ul:not( [class] ) li a&#39; )
	-&gt;map( &#39;text&#39; )
	-&gt;map( sub {
		my $c = substr $_, 0, 1;
		ord($c) &gt; 0x1F000
			?
		[ $c, ord($c), charnames::viacode( ord($c) ) ]
			:
			()
		})
	-&gt;sort( sub { $a-&gt;[1] &amp;lt;=&gt; $b-&gt;[1] } )
	-&gt;map( sub {
		sprintf &#39;%s (U+%05X) %s&#39;, $_-&gt;@*
		} )
	-&gt;join( &quot;\n&quot; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Unicode 9 got a spoon, 🥄, but Unicode 10 does it one better with a spoon  and bowl, 🥣. Too bad it has a cup with a straw, 🥤:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;🛷 (U+1F6F7) SLED
🛸 (U+1F6F8) FLYING SAUCER
🤟 (U+1F91F) I LOVE YOU HAND SIGN
🤨 (U+1F928) FACE WITH ONE EYEBROW RAISED
🤩 (U+1F929) GRINNING FACE WITH STAR EYES
🤪 (U+1F92A) GRINNING FACE WITH ONE LARGE AND ONE SMALL EYE
🤫 (U+1F92B) FACE WITH FINGER COVERING CLOSED LIPS
🤬 (U+1F92C) SERIOUS FACE WITH SYMBOLS COVERING MOUTH
🤭 (U+1F92D) SMILING FACE WITH SMILING EYES AND HAND COVERING MOUTH
🤮 (U+1F92E) FACE WITH OPEN MOUTH VOMITING
🤯 (U+1F92F) SHOCKED FACE WITH EXPLODING HEAD
🤱 (U+1F931) BREAST-FEEDING
🤲 (U+1F932) PALMS UP TOGETHER
🥌 (U+1F94C) CURLING STONE
🥟 (U+1F95F) DUMPLING
🥠 (U+1F960) FORTUNE COOKIE
🥡 (U+1F961) TAKEOUT BOX
🥢 (U+1F962) CHOPSTICKS
🥣 (U+1F963) BOWL WITH SPOON
🥤 (U+1F964) CUP WITH STRAW
🥥 (U+1F965) COCONUT
🥦 (U+1F966) BROCCOLI
🥧 (U+1F967) PIE
🥨 (U+1F968) PRETZEL
🥩 (U+1F969) CUT OF MEAT
🥪 (U+1F96A) SANDWICH
🥫 (U+1F96B) CANNED FOOD
🦒 (U+1F992) GIRAFFE FACE
🦓 (U+1F993) ZEBRA FACE
🦔 (U+1F994) HEDGEHOG
🦕 (U+1F995) SAUROPOD
🦖 (U+1F996) T-REX
🦗 (U+1F997) CRICKET
🧐 (U+1F9D0) FACE WITH MONOCLE
🧑 (U+1F9D1) ADULT
🧒 (U+1F9D2) CHILD
🧓 (U+1F9D3) OLDER ADULT
🧔 (U+1F9D4) BEARDED PERSON
🧕 (U+1F9D5) PERSON WITH HEADSCARF
🧖 (U+1F9D6) PERSON IN STEAMY ROOM
🧗 (U+1F9D7) PERSON CLIMBING
🧘 (U+1F9D8) PERSON IN LOTUS POSITION
🧙 (U+1F9D9) MAGE
🧚 (U+1F9DA) FAIRY
🧛 (U+1F9DB) VAMPIRE
🧜 (U+1F9DC) MERPERSON
🧝 (U+1F9DD) ELF
🧞 (U+1F9DE) GENIE
🧟 (U+1F9DF) ZOMBIE
🧠 (U+1F9E0) BRAIN
🧡 (U+1F9E1) ORANGE HEART
🧢 (U+1F9E2) BILLED CAP
🧣 (U+1F9E3) SCARF
🧤 (U+1F9E4) GLOVES
🧥 (U+1F9E5) COAT
🧦 (U+1F9E6) SOCKS
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Find the new emojis in Perl&#39;s Unicode support</title>
		<link>https://www.effectiveperlprogramming.com/2018/08/find-the-new-emojis-in-perls-unicode-support/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/08/find-the-new-emojis-in-perls-unicode-support/</guid>
		<pubDate>Sat, 25 Aug 2018 20:09:45 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
I shut down the Learning Perl 6 site, so I removed the link to it. You can still see &lt;a href=&quot;https://web.archive.org/web/20191022135450/https://www.learningperl6.com/&quot;&gt;the last archive.org copy&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Perl v5.26 updates itself to Unicode 9. That&#39;s not normally exciting news but people have been pretty enthusiastic about the 72 new emojis that come. As far as Perl cares, they are just valid code points like all of the other ones.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
I went to Emojipedia to see what I could do. I&#39;d already used the 🦋 in Learning Perl 6, although 🦖 would be more appropriate here. I was curious where these new emojis are in the UCD.
&lt;/p&gt;



&lt;p&gt;
First, how many new characters show up in Unicode 9? I could look this up but it&#39;s easier and more fun to do it in Perl:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $count = 0;
foreach ( 0 .. 0x10FFFF ) {
	my $char = chr;
	next if $char =~ /\p{Present_In: 8.0}/;
	next if $char =~ /\p{Unassigned}/;
	$count++;
	}
say &quot;There are $count new chars&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This skips anything that was present in the previous version (8.0) and then any code points that are not assigned. This gives exactly 7,500:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;There are 7500 new chars
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What are those other 7,428 characters? You could figure that out just as quickly. But who cares? They aren&#39;t emojis.
&lt;/p&gt;


&lt;a href=&quot;https://blog.emojipedia.org/new-unicode-9-emojis/&quot;&gt;The 72 new emojis are listed on Emojipedia&lt;/a&gt;, so you can use &lt;a href=&quot;https://metacpan.org/pod/Mojo::UserAgent&quot;&gt;Mojo::UserAgent&lt;/a&gt; to grab those. Since these are certainly wide characters you have to do some extra setup (see the &quot;Unicode Primer&quot; chapter in &lt;a href=&quot;https://www.learning-perl.com/&quot;&gt;Learning Perl&lt;/a&gt; or the various &lt;i&gt;perluni*&lt;/i&gt; docs):

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
use utf8;
use strict;
use warnings;
use open qw(:std :utf8);
use charnames qw();

use Mojo::UserAgent;
my $ua = Mojo::UserAgent-&gt;new;

my $url = &#39;https://blog.emojipedia.org/new-unicode-9-emojis/&#39;;
my $tx = $ua-&gt;get( $url );

die &quot;That didn&#39;t work!\n&quot; unless $tx-&gt;success;

say $tx-&gt;result
	-&gt;dom
	-&gt;find( &#39;ul:not( [class] ) li a&#39; )
	-&gt;map( &#39;text&#39; )
	-&gt;map( sub {
		my $c = substr $_, 0, 1;
		[ $c, ord($c), charnames::viacode( ord($c) ) ]
		})
	-&gt;sort( sub { $a-&gt;[1] &amp;lt;=&gt; $b-&gt;[1] } )
	-&gt;map( sub {
		sprintf &#39;%s (U+%05X) %s&#39;, $_-&gt;@*
		} )
	-&gt;join( &quot;\n&quot; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
From this I see the list of new emoji is not contiguous:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;🕺 (U+1F57A) MAN DANCING
🖤 (U+1F5A4) BLACK HEART
🛑 (U+1F6D1) OCTAGONAL SIGN
🛒 (U+1F6D2) SHOPPING TROLLEY
🛴 (U+1F6F4) SCOOTER
🛵 (U+1F6F5) MOTOR SCOOTER
🛶 (U+1F6F6) CANOE
🤙 (U+1F919) CALL ME HAND
🤚 (U+1F91A) RAISED BACK OF HAND
🤛 (U+1F91B) LEFT-FACING FIST
🤜 (U+1F91C) RIGHT-FACING FIST
🤝 (U+1F91D) HANDSHAKE
🤞 (U+1F91E) HAND WITH INDEX AND MIDDLE FINGERS CROSSED
🤠 (U+1F920) FACE WITH COWBOY HAT
🤡 (U+1F921) CLOWN FACE
🤢 (U+1F922) NAUSEATED FACE
🤣 (U+1F923) ROLLING ON THE FLOOR LAUGHING
🤤 (U+1F924) DROOLING FACE
🤥 (U+1F925) LYING FACE
🤦 (U+1F926) FACE PALM
🤧 (U+1F927) SNEEZING FACE
🤰 (U+1F930) PREGNANT WOMAN
🤳 (U+1F933) SELFIE
🤴 (U+1F934) PRINCE
🤵 (U+1F935) MAN IN TUXEDO
🤶 (U+1F936) MOTHER CHRISTMAS
🤷 (U+1F937) SHRUG
🤸 (U+1F938) PERSON DOING CARTWHEEL
🤹 (U+1F939) JUGGLING
🤺 (U+1F93A) FENCER
🤼 (U+1F93C) WRESTLERS
🤽 (U+1F93D) WATER POLO
🤾 (U+1F93E) HANDBALL
🥀 (U+1F940) WILTED FLOWER
🥁 (U+1F941) DRUM WITH DRUMSTICKS
🥂 (U+1F942) CLINKING GLASSES
🥃 (U+1F943) TUMBLER GLASS
🥄 (U+1F944) SPOON
🥅 (U+1F945) GOAL NET
🥇 (U+1F947) FIRST PLACE MEDAL
🥈 (U+1F948) SECOND PLACE MEDAL
🥉 (U+1F949) THIRD PLACE MEDAL
🥊 (U+1F94A) BOXING GLOVE
🥋 (U+1F94B) MARTIAL ARTS UNIFORM
🥐 (U+1F950) CROISSANT
🥑 (U+1F951) AVOCADO
🥒 (U+1F952) CUCUMBER
🥓 (U+1F953) BACON
🥔 (U+1F954) POTATO
🥕 (U+1F955) CARROT
🥖 (U+1F956) BAGUETTE BREAD
🥗 (U+1F957) GREEN SALAD
🥘 (U+1F958) SHALLOW PAN OF FOOD
🥙 (U+1F959) STUFFED FLATBREAD
🥚 (U+1F95A) EGG
🥛 (U+1F95B) GLASS OF MILK
🥜 (U+1F95C) PEANUTS
🥝 (U+1F95D) KIWIFRUIT
🥞 (U+1F95E) PANCAKES
🦅 (U+1F985) EAGLE
🦆 (U+1F986) DUCK
🦇 (U+1F987) BAT
🦈 (U+1F988) SHARK
🦉 (U+1F989) OWL
🦊 (U+1F98A) FOX FACE
🦋 (U+1F98B) BUTTERFLY
🦌 (U+1F98C) DEER
🦍 (U+1F98D) GORILLA
🦎 (U+1F98E) LIZARD
🦏 (U+1F98F) RHINOCEROS
🦐 (U+1F990) SHRIMP
🦑 (U+1F991) SQUID
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;v5.26 updates to Unicode 9 with 7,500 new characters.&lt;/li&gt;
&lt;li&gt;You can check the Unicode version of characters with the &lt;code&gt;Present_In&lt;/code&gt;, &lt;code&gt;In&lt;/code&gt;, or &lt;code&gt;Age&lt;/code&gt; properties.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ord&lt;/code&gt; gets you the code number and &lt;code&gt;charnames::viacode&lt;/code&gt; can use the code number to get you the code name.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Avoid perl housekeeping for hot loop optimization</title>
		<link>https://www.effectiveperlprogramming.com/2018/08/avoid-perl-housekeeping-for-hot-loop-optimization/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/08/avoid-perl-housekeeping-for-hot-loop-optimization/</guid>
		<pubDate>Sat, 25 Aug 2018 00:24:20 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>performance</category>
		<description>&lt;p&gt;
&lt;span class=&quot;person&quot;&gt;David Golden&lt;/span&gt; gave a talk at The Perl Conference 2017 where he showed &lt;a href=&quot;https://www.youtube.com/watch?v=_PJIVVGAZqA&quot;&gt;Real World Optimization&lt;/a&gt; for the MongoDB Perl driver. He spoke about many big performance gains and you can watch the talk for that, but at the end he talked about various micro-optimizations.
&lt;/p&gt;



&lt;p&gt;
Small gains in &quot;hot loops&quot; (code that executes many, many times) can add up to significant savings. &lt;span class=&quot;person&quot; data-fullname=&quot;David Golden&quot;&gt;David&lt;/span&gt; was able to cut off 20% of the runtime with some of these micro-optimizations. All of these are his ideas but they are the very thing the Effective Perl programmer is curious about.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
This is also in line with the history of programming. If you want more speed, at some point you have to think about how things actually happen. Perl, being a dynamic language, does quite a bit of work for you. These optimizations are about delaying or removing that work for hot loops, not everywhere in your program. This is the sort of stuff you think about if you have done all of the other, big optimizations and you still need to squeak out a small sliver of performance.
&lt;/p&gt;



&lt;p&gt;
For what it&#39;s worth, next in my YouTube playlist was &lt;span class=&quot;person&quot;&gt;Frew Schmidt&lt;/span&gt;&#39;s &lt;a href=&quot;https://www.youtube.com/watch?v=WHKQ2JfTaIM&quot;&gt;Scaling, Reliability, and Performance at ZipRecruiter&lt;/a&gt; from the same conference. His advice was that if something takes a lot of time, you shouldn&#39;t optimize it. Instead, you should just stop doing that thing. You&#39;ll have to watch that talk to see what he means by that.
&lt;/p&gt;


&lt;h2&gt;Avoid Statements&lt;/h2&gt;


&lt;p&gt;
After &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; executes a statement it can take some time for itself to do things. In a &quot;hot loop&quot; where you want your code to run as fast as possible (and let Perl do its housekeeping later), you can get rid of the statement boundaries. Here are three statement boundaries and three places where Perl can wander off:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $x = 5;
my $y = 6;
my $z = 7;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Instead of that, combine that into one statement:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $x, $y, $z ) = (5,6,7);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Although this example is simple, you could rearrange it in other ways that aren&#39;t special to assignments. Put &quot;statements&quot; in parentheses to compartmentalize them and separate those with the comma operator:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;(my $x = 5), (my $y = 6), (my $z = 7);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is just about delaying &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt;&#39;s potential housekeeping so this code that runs repeatedly runs faster. &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; will have other opportunities.
&lt;/p&gt;


&lt;h2&gt;Avoid Scopes&lt;/h2&gt;


&lt;p&gt;
Perl does a lot of work to set up a scope. Each scope gets its own lexical variables so it needs to set up that stuff. Various things can happen on scope entry and exit, so that needs to happen. Sometimes we don&#39;t need that.
&lt;/p&gt;



&lt;p&gt;
Here&#39;s a conditional. You want that &lt;code&gt;print&lt;/code&gt; to do its work under that condition. That block isn&#39;t there to limit scope of variables or do any entry or exit work. It&#39;s there because that&#39;s the Perl syntax for an &lt;code&gt;if&lt;/code&gt; (because &lt;span class=&quot;person&quot; data-fullname=&quot;Larry Wall&quot;&gt;Larry&lt;/span&gt; didn&#39;t want the single statement C &lt;code&gt;if&lt;/code&gt;).
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( $condition ) {
	print &quot;That was true\n&quot;;
	warn &quot;That was also true\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In that case, you can avoid the scope with the postfix form. Now there&#39;s no additional block and there&#39;s no scope to setup and tear down:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;( (print &quot;That was true\n&quot;), (warn &quot;That was also true\n&quot;) ) if $condition;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Avoid Variables&lt;/h2&gt;


&lt;p&gt;
I like intermediate variables and use them liberally, but I do that knowing that I&#39;m wasting a little bit of time. Often it&#39;s time that I don&#39;t care about because I want to grab the intermediate result for debugging (that&#39;s the time I care about):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $middle = do_this_thing();
# say &quot;middle was [$middle]&quot;;
some_other_call( $middle );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
For &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; to do that, it needs to setup and teardown that lexical variable. That&#39;s not insignificant. Instead, pass the result directly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;some_other_call( do_this_thing() );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If I had to debug this, I could rearrange it to see what&#39;s going on (using a branch in git, of course). When I was satisfied with that I could throw away the branch.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.28 can delete key-value slices</title>
		<link>https://www.effectiveperlprogramming.com/2018/08/perl-v5-28-can-delete-key-value-slices/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/08/perl-v5-28-can-delete-key-value-slices/</guid>
		<pubDate>Tue, 14 Aug 2018 19:56:11 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
&lt;a href=&quot;/2014/07/perl-5-20-introduces-keyvalue-slices/&quot;&gt;Perl v5.20 introduced key-value slices&lt;/a&gt; that worked on hashes and arrays. You could extract values by their keys or indices as well as assigning to those.
&lt;/p&gt;



&lt;p&gt;
The key-value slice delete is a way to extract the keys and values you want and delete them at the same time. You can destructively change the original hash while also moving everything you removed.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;A review of delete&lt;/h2&gt;


&lt;p&gt;
The &lt;a href=&quot;https://perldoc.perl.org/functions/delete&quot;&gt;delete&lt;/a&gt; removes the element from the hash or array and returns it.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Data::Dumper;

my %hash = qw(
	Cat    Buster
	Dog    Addy
	Bird   Foghorn
	Lizard Godzilla
	);

my $removed_value = delete $hash{&#39;Lizard&#39;};
say &quot;Removed $removed_value&quot;;
say Dumper( \%hash );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;a href=&quot;https://perldoc.perl.org/functions/delete&quot;&gt;delete&lt;/a&gt; removed the &lt;code&gt;Lizard&lt;/code&gt; key and returned its value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Removed Godzilla
$VAR1 = {
          &#39;Bird&#39; =&gt; &#39;Foghorn&#39;,
          &#39;Dog&#39; =&gt; &#39;Addy&#39;,
          &#39;Cat&#39; =&gt; &#39;Buster&#39;
        };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
A &lt;a href=&quot;https://perldoc.perl.org/functions/delete&quot;&gt;delete&lt;/a&gt; on a slice does the same thing. The &lt;code&gt;@&lt;/code&gt; before &lt;code&gt;hash&lt;/code&gt; signifies multiple values (not an array!) and you know it&#39;s a hash variable because the indexing uses &lt;code&gt;{}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Data::Dumper;

my %hash = qw(
	Cat    Buster
	Dog    Addy
	Bird   Foghorn
	Lizard Godzilla
	);

my @removed_values = delete @hash{ qw(Lizard Bird) };
say &quot;Removed @removed_values&quot;;
say Dumper( \%hash );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The delete returns the values from the keys it deleted:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Removed Godzilla Foghorn
$VAR1 = {
          &#39;Cat&#39; =&gt; &#39;Buster&#39;,
          &#39;Dog&#39; =&gt; &#39;Addy&#39;
        };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This works on arrays too even though the documentation says you shouldn&#39;t use them. The &quot;key&quot; doesn&#39;t necessarily disappear. Interior items become undef and items at the end disappear:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Data::Dumper;

my @array = qw( Buster Addy Foghorn Godzilla );

my @removed_values = delete @array[ 1, 3 ];
say &quot;Removed @removed_values&quot;;
say Dumper( \@array );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows what&#39;s left:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Removed Addy Godzilla
$VAR1 = [
          &#39;Buster&#39;,
          undef,
          &#39;Foghorn&#39;
        ];
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
One of the &quot;keys&quot; (the one that was at the end) is apparently gone but the interior &quot;key&quot; is still present but now has an &lt;code&gt;undef&lt;/code&gt; value. Maybe that works for you but I&#39;m guessing your maintenance programmer won&#39;t know that.
&lt;/p&gt;


&lt;h2&gt;The key-value slice&lt;/h2&gt;


&lt;p&gt;
&lt;a href=&quot;/2014/07/perl-5-20-introduces-keyvalue-slices/&quot;&gt;The key-value slice&lt;/a&gt; extracts the keys and the values at the same time. It returns a list (maybe of zero items if the key does not exist):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

my %hash = qw(
	Cat    Buster
	Dog    Addy
	Bird   Foghorn
	Lizard Godzilla
	);

say join &#39; &#39;, %hash{ qw(Bird Lizard) }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows the keys and their values:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Bird Foghorn Lizard Godzilla
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This works on an array and uses the indices as the keys and square braces to surround those keys. There&#39;s still a &lt;code&gt;%&lt;/code&gt; in front of the variable name to signify that it&#39;s a key-value slice but you use the square brackets for the indices (&lt;span class=&quot;item&quot;&gt;Item 4. Understand what sigils are telling you.&lt;/span&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

my @array = qw(
	Buster
	Addy
	Foghorn
	Godzilla
	);

my @indices = ( 1, 3 );
my @sliced = %array[@indices];
say &quot;@sliced&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the keys are the list indices. This can be useful when you don&#39;t know ahead of time what the indices are but still need to match them up with their values:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1 Addy 3 Godzilla
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is certainly more palatable than computing the intermediate results and merging the indices later:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @indices = ( 1, 3 );
my @values = @array[@indices];
my @sliced = zip( @indices, @values );
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Destructive processing with key-value slices&lt;/h2&gt;


&lt;p&gt;
Deleting a key-value slice removes the elements (sets them to &lt;code&gt;undef&lt;/code&gt; if an array) and removes those key-values at the same time.
&lt;/p&gt;



&lt;p&gt;
Suppose that &lt;code&gt;show_items&lt;/code&gt; does some complicated and interesting task. It needs to know which value it is processing and what that value belongs to. The key-value &lt;code&gt;delete&lt;/code&gt; provides the keys and values together and removes them from the things to process:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

my %hash_to_process = qw(
	Cat    Buster
	Dog    Addy
	Bird   Foghorn
	Lizard Godzilla
	);

my @items_to_process = qw(Bird Lizard);
process( delete %hash_to_process{ @items_to_process } );

sub process { say &quot;Processing @_&quot; }
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A classic &lt;code&gt;delete&lt;/code&gt; returns the value&lt;/li&gt;
&lt;li&gt;A key-value &lt;code&gt;delete&lt;/code&gt; returns both the key and the value&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;%&lt;/code&gt; signifies a key-value slice for either a hash or an array&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Initialize array and hash variables with state</title>
		<link>https://www.effectiveperlprogramming.com/2018/08/initialize-array-and-hash-variables-with-state/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/08/initialize-array-and-hash-variables-with-state/</guid>
		<pubDate>Wed, 08 Aug 2018 15:02:24 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;p&gt;
Perl v5.28 allows you to initialize array and hash variables that you declare with &lt;code&gt;state&lt;/code&gt;. This is a feature a long time coming and that I&#39;m quite happy has finally arrived.
&lt;/p&gt;



&lt;p&gt;
Since v5.10 and up to v5.26 you could only initialize a &lt;code&gt;state&lt;/code&gt; variable if it was a scalar. You could declare a hash or array variable but you couldn&#39;t give it an initial value at the same time. You could do this:
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
sub do_it {
	state $previous_argument = 0;
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But you can&#39;t give an array or hash initial values. You can&#39;t assign to them at all. &quot;Initializing&quot; them to an empty list won&#39;t work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
sub do_it {
	state @previous_arguments = ();  # ERROR
	push @previous_arguments, @_;
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The error gives some hope that the situation will change:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Initialization of state variables in list context currently forbidden
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
References are one way around this because they are scalar variables. Instead of an array or hash, use an array or a hash reference.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
sub do_it {
	state $previous_arguments = [];
	push @$previous_arguments, @_;
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since you can initialize the array reference you can give it values before the actual arguments of the first call:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
sub do_it {
	state $previous_arguments = [ qw(this that) ];
	push @$previous_arguments, @_;
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.28 you can skip the references and the dereferencing. This is slightly cleaner and amenable to people still grappling with the idea of references:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;
sub do_it {
	state @previous_arguments = qw( this that );
	push @previous_arguments, @_;
	...
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Fibonacci caching&lt;/h2&gt;


&lt;p&gt;
In &lt;a href=&quot;https://www.masteringperl.org&quot;&gt;Mastering Perl&lt;/a&gt; I show a caching Fibonacci generator. If you intend on using many of these numbers you don&#39;t want to recompute what you already know. You can trade a bit of memory for some speed by caching prior work. Here&#39;s the same thing using an array variable:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.28;

foreach my $n ( 0 .. 100 ) {
	say fibonacci( $n );
	}


sub fibonacci {
	state @Cached = qw( 1 1 );
	my( $n ) = @_;

	return $Cached[$n] if $n &amp;lt;= $#Cached;

	foreach my $i ( $#Cached + 1 .. $n ) {
		$Cached[$i] = $Cached[-1] + $Cached[-2];
		}

	return $Cached[$n];
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s that same subroutine prior to v5.28. It&#39;s a bit more ugly although perfectly serviceable. It uses an array reference so there are some added dereferencing arrows and an accretion of symbols to get the last index (&lt;code&gt;$#$Cached&lt;/code&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;

sub fibonacci {
	state $Cached = [ qw( 1 1 ) ];
	my( $n ) = @_;

	return $Cached-&gt;[$n] if $n &amp;lt;= $#$Cached;

	foreach my $i ( $#$Cached + 1 .. $n ) {
		$Cached-&gt;[$i] = $Cached-&gt;[-1] + $Cached-&gt;[-2];
		}

	return $Cached-&gt;[$n];
	}
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Perl v5.26 now recognizes version control conflict markers</title>
		<link>https://www.effectiveperlprogramming.com/2018/04/perl-v5-26-now-recognizes-version-control-conflict-markers/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/04/perl-v5-26-now-recognizes-version-control-conflict-markers/</guid>
		<pubDate>Tue, 17 Apr 2018 18:17:05 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Perl v5.26 can now detect and warn you about version control conflict markers in your code. In prior versions, the compiler would try to interpret those as code and would complain about a syntax error. Your program still fails to compile but you get a better error message. Maybe some future Perl will bifurcate the program, run both versions, and compare the results (don&#39;t hold your breath).
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
To demonstrate this, set up a new git repository, create a simple &quot;Hello World&quot; program, and commit it:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ mkdir test_repo; cd test_repo; git init
Initialized empty Git repository in /Users/buster/test_repo/.git/

$ cat &amp;gt; hello.pl
#!/usr/bin/perl
use v5.10;
say &quot;Hello World!&quot;;
^D

$ git add hello.pl

$ git commit -a -m &#39;initial program&#39;
[master (root-commit) 0665c2b] initial program
 1 file changed, 3 insertions(+)
 create mode 100755 hello.pl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Now that you have the file you need to create a conflict that Perl can detect. One way is to edit in a new branch and the master branch at the same time.
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ git checkout -b new_feature
Switched to a new branch &#39;new_feature&#39;

$ cat &amp;gt; hello.pl
#!/usr/bin/perl
use v5.10;
say &quot;Hello Perl 5.26!&quot;;

$ git commit -a -m &#39;Greet the new perl&#39;
[new_feature 9ed328a] Greet the new perl
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout master
Switched to branch &#39;master&#39;

$ cat &amp;gt; hello.pl
#!/usr/bin/perl
use v5.10;
say &quot;Hello new Perl!&quot;;

$ git commit -a -m &#39;Greet the new Perl&#39;
[master 15dd6ac] Greet the new Perl
 1 file changed, 1 insertion(+), 1 deletion(-)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Both of these programs change the message in the &lt;code&gt;say&lt;/code&gt; statement. Both commits have a similar message (that&#39;s not important). You can pull from a branch to try to merge it into your current branch. You may get the same thing by pulling from a remote where someone else was working on the same file as you. The merge gives you a warning:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ git pull . new_feature
From .
 * branch            new_feature -&amp;gt; FETCH_HEAD
Auto-merging hello.pl
CONFLICT (content): Merge conflict in hello.pl
Automatic merge failed; fix conflicts and then commit the result.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The file now has both versions of the file. A git conflict marker starts with &lt;code&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/code&gt;. That&#39;s followed by the version for the file as it exists in the current branch. That ends with &lt;code&gt;=======&lt;/code&gt;, after which is the content for the incoming version. Finally, the end of the conflict is denoted with &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; and the commit digest for the incoming commit marked as &lt;code&gt;HEAD&lt;/code&gt; (you know the current commit because, well, it&#39;s the &lt;a href=&quot;https://stackoverflow.com/questions/949314/how-to-retrieve-the-hash-for-the-current-commit-in-git&quot;&gt;current commit&lt;/a&gt;: &lt;code&gt;git show -s --format=%H&lt;/code&gt;):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ cat hello.pl
#!/usr/bin/perl
use v5.10;
&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; HEAD
say &quot;Hello new Perl!&quot;;
=======
say &quot;Hello Perl 5.26!&quot;;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; 9ed328a5468aeab03098a4ded7a6bbc6daf8ac61
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Versions prior to v5.26 give an odd error that doesn&#39;t seem to relate to anything going on in the program. Earlier Perls think you are trying to use a heredoc:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.24 hello.pl
Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is deprecated at hello.pl line 3.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Now the error message better detects what&#39;s going on:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.26 hello.pl
Version control conflict marker at hello.pl line 3, near &quot;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&quot;
Version control conflict marker at hello.pl line 5, near &quot;=======&quot;
Version control conflict marker at hello.pl line 7, near &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&quot;
Execution of hello.pl aborted due to compilation errors.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Running it under &lt;a href=&quot;https://metacpan.org/pod/diagnostics&quot;&gt;diagnostics&lt;/a&gt; gives extra information (although it&#39;s not always much more helpful):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -Mdiagnostics hello.pl
Version control conflict marker at hello.pl line 3, near &quot;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&quot;
Version control conflict marker at hello.pl line 5, near &quot;=======&quot;
Version control conflict marker at hello.pl line 7, near &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&quot;
Execution of hello.pl aborted due to compilation errors (#1)
    (F) The parser found a line starting with &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;,
    &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;, or =======.  These may be left by a
    version control system to mark conflicts after a failed merge operation.

Uncaught exception from user code:
	Version control conflict marker at hello.pl line 3, near &quot;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&quot;
	Version control conflict marker at hello.pl line 5, near &quot;=======&quot;
	Version control conflict marker at hello.pl line 7, near &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&quot;
	Execution of hello.pl aborted due to compilation errors.
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Unquoted empty heredoc terminators are now fatal</title>
		<link>https://www.effectiveperlprogramming.com/2018/01/unquoted-empty-heredoc-terminators-are-now-fatal/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2018/01/unquoted-empty-heredoc-terminators-are-now-fatal/</guid>
		<pubDate>Sun, 28 Jan 2018 06:29:59 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;p&gt;
Continuing its quest to clean up long deprecated features, v5.28 takes care of another feature deprecated since v5.0. You can no longer neglect to specify a heredoc separator. This was a warning in v5.26 and is now fatal. You probably weren&#39;t doing this anyway (I&#39;ve never seen it in the wild), but it&#39;s nice to know the edge cases are disappearing.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Usually, you give a heredoc some string that marks its end. The quotes around that terminator string denote the type of string it is. This one is a double-quoted string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $position = &quot;first&quot;;
print &amp;lt;&amp;lt;&quot;HERE&quot;;
This is the $position line
This is a different line
This is the last line
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With no quoting, it&#39;s still a double-quoted string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $position = &quot;first&quot;;
print &amp;lt;&amp;lt;HERE;
This is the $position line
This is a different line
This is the last line
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.26, you can &lt;a href=&quot;/2016/12/strip-leading-spaces-from-here-docs-with-v5-26/&quot;&gt;indent the heredoc&lt;/a&gt;. This gives the same output:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $position = &quot;first&quot;;
print &amp;lt;&amp;lt;~&quot;HERE&quot;;
	This is the $position line
	This is a different line
	This is the last line
	HERE
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
Single quotes around the delimiter string make for a single-quoted string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;print &amp;lt;&amp;lt;&#39;HERE&#39;;
This is the first line
This is a different line
This is the last line
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can even use the empty string (single- or double-quoted), which is probably a bad idea for future maintainers:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;print &amp;lt;&amp;lt;&#39;&#39;;
This is the first line
This is a different line
This is the last line

print &quot;New statement!&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Prior to v5.26, you could leave off the quotes and the delimiter string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;print &amp;lt;&amp;lt;;
This is the first line
This is a different line
This is the last line

print &quot;New statement!&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Starting with v5.26, that still works but gives a warning:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is deprecated. Its use will
be fatal in Perl 5.28
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now, with v5.28, it&#39;s fatal:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is forbidden
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>In-place editing gets safer in v5.28</title>
		<link>https://www.effectiveperlprogramming.com/2017/12/in-place-editing-gets-safer-in-v5-28/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/12/in-place-editing-gets-safer-in-v5-28/</guid>
		<pubDate>Sat, 30 Dec 2017 00:04:28 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;p&gt;
In-place editing is getting much safer in v5.28. Before that, in rare circumstances it could lose data. You may have never noticed the problem and even with all the times I&#39;ve explained it in a Perl class I haven&#39;t really thought about it. This was first reported as early as December 2002 and after we get v5.28 it won&#39;t be a problem anymore.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
In the age of Perl one-liners people would write complex text transformations on the command-line. I wasn&#39;t quite into that since I&#39;m not precise enough to type a complicated program left-to-right at a prompt but I&#39;ve seen people who are. The &lt;code&gt;-p&lt;/code&gt; switch wraps some default code around the string argument to &lt;code&gt;-e&lt;/code&gt;. You can see the difference by &lt;a href=&quot;/2010/05/use-bdeparse-to-see-what-perl-thinks-the-code-is/&quot;&gt;deparsing the code&lt;/a&gt;. First just a program from the command-line. This program simply prints the input line number when it&#39;s run (but you&#39;re not running it):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -MO=Deparse -e &#39;print qq($. )&#39;
print $.;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
When you add the &lt;code&gt;-p&lt;/code&gt; switch there&#39;s much more code. Now it reads input from the files on the command line and outputs the line numbers to standard output. There&#39;s an attached &lt;code&gt;continue&lt;/code&gt; block. That&#39;s always evaluated right before the conditional is checked again. In that block the &lt;code&gt;-p&lt;/code&gt; prints the current value of &lt;code&gt;$_&lt;/code&gt;:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -MO=Deparse -p -e &#39;print qq($. )&#39;
LINE: while (defined($_ = readline ARGV)) {
    print &quot;$. &quot;;
}
continue {
    die &quot;-p destination: $!\n&quot; unless print $_;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In-place editing is a nifty feature where the output ends up in the filename that it came from. The &lt;code&gt;-i&lt;/code&gt; switch turns on this feature by using the &lt;code&gt;$^I&lt;/code&gt; special variable. This redirects standard output into a filehandle connected to the same filename:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -MO=Deparse -p -i -e &#39;print qq($. )&#39; *.txt
BEGIN { $^I = &quot;&quot;; }
LINE: while (defined($_ = readline ARGV)) {
    print &quot;$. &quot;;
}
continue {
    die &quot;-p destination: $!\n&quot; unless print $_;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The &lt;code&gt;-i&lt;/code&gt; can take an argument, which will be used as the file extension for a backup file:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -MO=Deparse -p -i.old -e &#39;print qq($. )&#39; *.txt
BEGIN { $^I = &quot;.old&quot;; }
LINE: while (defined($_ = readline ARGV)) {
    print &quot;$. &quot;;
}
continue {
    die &quot;-p destination: $!\n&quot; unless print $_;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Before v5.28, the &lt;code&gt;-i&lt;/code&gt; did a few things to make this work. With no argument it opened the file then unlinked the name. That gives it access to the data but it&#39;s no longer in the abstract idea of a file that you&#39;ll see in a directory. It opens a new file with the same name and outputs to that new file. If you put a &lt;code&gt;sleep&lt;/code&gt; in your code you&#39;ll have a chance to look at the directory listing before the process ends:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ ls -si
8593876032 88 pg27609.txt

$ perl5.26.1 -pi -e &#39;BEGIN { print $^V } sleep 100&#39; *.txt &amp;
v5.26.1

$ ls -si
8593876937 0 pg27609.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The &lt;code&gt;-s&lt;/code&gt; switch shows the file size in blocks. The &lt;code&gt;-i&lt;/code&gt; switch for &lt;span class=&quot;binary&quot;&gt;ls&lt;/span&gt; shows the inode number (Don&#39;t know this stuff? I like &lt;a href=&quot;https://www.grymoire.com/Unix/Inodes.html&quot;&gt;Inodes - an Introduction&lt;/a&gt;). An inode is the meta data for stuff you store on a disk. A filename is a label a directory gives an inode (and more than one directory can link to an inode hence Perl&#39;s &lt;code&gt;link&lt;/code&gt; and &lt;code&gt;unlink&lt;/code&gt;). I&#39;ll keep saying &quot;label&quot; so you don&#39;t have the bias of &quot;filename&quot;. I&#39;m going to keep saying &quot;inode&quot; even though it&#39;s unix jargon. Other filesystems do similar things with their own jargon.
&lt;/p&gt;

&lt;p&gt;
Notice that the second &lt;span class=&quot;binary&quot;&gt;ls&lt;/span&gt; shows that the same name points to a different inode. Its size is also 0; it&#39;s a new and different inode and you haven&#39;t output anything to that yet. So, &quot;in place&quot; editing is a bit of a misnomer.
&lt;/p&gt;

&lt;p&gt;
With an argument the &lt;code&gt;-i&lt;/code&gt; relabels the inode (renames the file) based on the original label with that argument added to the end. You can see the inode of the data before the &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; one-liner and that the same inode later has the original label with &lt;code&gt;.old&lt;/code&gt; added. There&#39;s also a new inode with the original label but no contents yet. The &lt;span class=&quot;filename&quot;&gt;pg27609.txt&lt;/span&gt; before and the &lt;span class=&quot;filename&quot;&gt;pg27609.txt.old&lt;/span&gt; after have the same inode number:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ ls -si
8593874285 88 pg27609.txt

$ perl -pi.old -e &#39;BEGIN { print $^V } sleep 100&#39; *.txt &amp;
v5.26.1

$ ls -si
8593874356 0 pg27609.txt
8593874285 88 pg27609.txt.old
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What could go wrong with this approach?
&lt;/p&gt;

&lt;p&gt;
In the first case, what happens if you can&#39;t get through all of the data? Maybe &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; panics, your machine loses power, you fill up the disk, or something else bad. You&#39;ve already unlinked the original file. You lose data (unless there&#39;s another link somewhere). In the second case, you still have the backup file but the final file is incomplete. Various problems are noted in &lt;a href=&quot;https://github.com/Perl/perl5/issues/6177&quot;&gt;RT #19333&lt;/a&gt;, &lt;a href=&quot;https://github.com/Perl/perl5/issues/9436&quot;&gt;RT #57512&lt;/a&gt;, and &lt;a href=&quot;https://github.com/Perl/perl5/issues/15216&quot;&gt;RT #127663&lt;/a&gt;. 
&lt;/p&gt;

&lt;p&gt;
This problem shows up if your program exits with a true value (so, not the 0 that means normal execution). If you exit by &lt;code&gt;die&lt;/code&gt;-ing or by using a true value with &lt;code&gt;exit&lt;/code&gt;, the output is incomplete and the original data are gone:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ ls -si
8593878186 88 pg27609.txt

$ perl5.26.1 -pi -e &#39;BEGIN { print $^V } die if $. = 10&#39; *.txt
Died at -e line 1, &amp;lt;&gt; line 10.
v5.26.1

$ ls -si
8593878227 0 pg27609.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In either case the original label points to incomplete data. Maybe something else freaks out because its incomplete state is invalid XML or something similar. Maybe that incomplete state turns a big number into a small one because the rest of the characters are still in the output buffer. There&#39;s a race condition here; with an empty configuration file I might be allowed to do something that should have been forbidden.
&lt;/p&gt;

&lt;p&gt;
If I were creating such a feature for a production system I would have already thought to completely create the new file, check the new file, then move it into place if it worked out.
&lt;/p&gt;

&lt;p&gt;
In the latest development version, v5.27, the &lt;code&gt;-p&lt;/code&gt; and &lt;code&gt;-i&lt;/code&gt; have changed. Run the same command again and look at the directory listing. The original label still connects to the data. There&#39;s a temporary label to collect the data. At the end of the process that inode is relabeled with the original label:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ ls -si
8593874476 88 pg27609.txt

$ perl5.27.7 -pi.old -e &#39;BEGIN { print $^V } sleep 100&#39; *.txt &amp;
v5.27.7

$ ls -si
8593874538 0 Cr1uEizj
8593874476 88 pg27609.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
There are a few gotchas here. If you change directories during this process the relabeling will probably fail. Trying this across threads (or trying anything across threads) will likely fail.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Beware of the removal of when in Perl v5.28</title>
		<link>https://www.effectiveperlprogramming.com/2017/12/beware-of-the-removal-of-when-in-perl-v5-28/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/12/beware-of-the-removal-of-when-in-perl-v5-28/</guid>
		<pubDate>Sun, 24 Dec 2017 06:58:50 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Although I haven&#39;t seen an official notice besides a &lt;a href=&quot;https://github.com/Perl/perl5/commit/7896dde7482a2851e73f0ac2c32d1c71f6e97dca&quot;&gt;git commit that reverts the changes&lt;/a&gt;, by popular outcry these changes won&#39;t be in v5.28. It&#39;s not that they won&#39;t happen but they won&#39;t be in v5.28. People who depend on Perl should stay vigilant. My advice in the first paragraph stands—change is coming and we don&#39;t know what it is yet.
&lt;/p&gt;
&lt;/div&gt;




&lt;p&gt;
Perl v5.28 might do away with &lt;code&gt;when&lt;/code&gt;—v5.27.7 already has. Don&#39;t upgrade to v5.28 until you know you won&#39;t be affected by this! This change doesn&#39;t follow the normal Perl deprecation or experimental feature policy. If you are using &lt;code&gt;given-when&lt;/code&gt;, stop doing that. If you aren&#39;t using it, don&#39;t start. And everyone should consider if a major change like this on such short notice is comfortable for them. It&#39;s not a democracy but you can still let the core developers know which way you want your favorite language to go.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
There are several knock-on consequences with the removal of &lt;code&gt;when&lt;/code&gt; since so many broken features from v5.10 were entangled with each other. This is related to smart matching. I won&#39;t go into the simplified smart matching and you can read the &lt;a href=&quot;https://www.nntp.perl.org/group/perl.perl5.porters/2017/08/msg245769.html&quot;&gt;p5p thread&lt;/a&gt; for yourself. Blame &lt;span class=&quot;person&quot; data-fullname=&quot;Sawyer X&quot;&gt;Sawyer&lt;/span&gt; for kicking the hornets&#39; nest!
&lt;/p&gt;



&lt;p&gt;
Remind yourself about &lt;code&gt;when&lt;/code&gt;. You have to use it in something that topicalizes (sets the current element to &lt;code&gt;$_&lt;/code&gt;). You can have multiple &lt;code&gt;when&lt;/code&gt; blocks but the first one that runs its block stops that iteration because it has an implicit &lt;code&gt;break&lt;/code&gt; at the end unless you use &lt;code&gt;continue&lt;/code&gt;. You can have code between the &lt;code&gt;when&lt;/code&gt; blocks. With no comparison operator in the condition it uses an implicit smart match against &lt;code&gt;$_&lt;/code&gt; (the topic):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10.1;

for( $ARGV[0] ) {
	when( $_ )         { say &quot;$_ is true&quot;; continue }
	when( not $_ % 2 ) { say &quot;$_ is even&quot;; continue }
	when( 137 )        { say &quot;Number is 137&quot;; continue }
	when( @array )     { say &quot;In array branch&quot; }
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You could rewrite this with &lt;code&gt;if&lt;/code&gt; but notice how ugly that last &lt;code&gt;if&lt;/code&gt; condition is. A smart match against an array smart matches the topic against items in the array. If one of those is true the smart match is true. The &lt;code&gt;grep&lt;/code&gt; checks every element and returns a count; the smart match can stop midway. You could fix that with &lt;code&gt;first&lt;/code&gt; from &lt;code&gt;List::Util&lt;/code&gt; (a core module) but it&#39;s still a bit uglier:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;for( $ARGV[0] ) {
	if( $_ )         { say &quot;$_ is true&quot; }
	if( not $_ % 2 ) { say &quot;$_ is even&quot; }
	if( $_ == 137 )  { say &quot;Always true branch&quot; }
	my $value = $_;  # doubled $_!
	if( grep {$_ == $value} @array ) { say &quot;In array branch&quot;; next }
	#if( first {$_ == $value} @array ) { say &quot;In array branch&quot;; next }
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The job of &lt;code&gt;when&lt;/code&gt; is now decomposed into two new keywords: &lt;code&gt;whereso&lt;/code&gt; and &lt;code&gt;whereis&lt;/code&gt;. Good luck explaining these in a way that anyone in your code review will remember. The keywords can still change and there&#39;s no hardcore insistence on them. However, the words we normally use in speaking (&quot;if&quot; and &quot;when&quot;) are either taken or banished. I have my own suggestion later.
&lt;/p&gt;



&lt;p&gt;
The &lt;code&gt;whereso&lt;/code&gt; always tests its expression for a simple true or false. It does not impose any smart matching. Instead of an implicit &lt;code&gt;break&lt;/code&gt; it has an implicit &lt;code&gt;next&lt;/code&gt; (which is the same thing). &lt;code&gt;break&lt;/code&gt; is banished with &lt;code&gt;when&lt;/code&gt; because we never needed it (even if it was a C keyword): 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.27.7;
no warnings qw(experimental);

my @array = qw( 1 3 7 137 );

for( $ARGV[0] ) {
	whereso( $_ )         { say &quot;$_ is true&quot;; continue }
	whereso( not $_ % 2 ) { say &quot;$_ is even&quot;; continue }
	whereso( 137 )        { say &quot;Always true&quot;; continue }
	whereso( @array )     { say &quot;In array branch&quot; }
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In that last &lt;code&gt;whereso&lt;/code&gt; the test is the number of elements in the array. It&#39;s just an array in scalar context. If there&#39;s at least one element it&#39;s true. If I don&#39;t want that I&#39;m back to the ugly &lt;code&gt;grep&lt;/code&gt;. Perl solves that ugly &lt;code&gt;grep&lt;/code&gt; with a confusing (so far) smart match. I think many people would be satisfied with a new comparator &lt;code&gt;in&lt;/code&gt; (as in Python) that evaluated to true or false if &lt;code&gt;$_&lt;/code&gt; was in the list. I don&#39;t think we need it but it&#39;s a request (and use) I see often.
&lt;/p&gt;



&lt;p&gt;
I don&#39;t like this keyword and I think most of the people who chimed in on the p5p thread hated it in the same way. &quot;whereso&quot; is an existing English word although I can&#39;t recall if I&#39;ve ever heard it in a modern context, I don&#39;t think I could properly construct a sentence with it, and my spellchecker doesn&#39;t recognize it. Perl 6 also uses &lt;code&gt;so&lt;/code&gt; to boolify something; there&#39;s a tenuous connection between the colloquial &quot;so&quot; as a logical connector and its use in Perl 6. I think it&#39;s too clever for its own sake and sure to annoy the workaday programmer. I&#39;m not sure how &lt;code&gt;whereso&lt;/code&gt; is better than &lt;code&gt;if&lt;/code&gt; other than the implicit &lt;code&gt;next&lt;/code&gt;. But the keyword is not set in stone and I&#39;m betting it will change before v5.28.
&lt;/p&gt;



&lt;p&gt;
The smartmatching version is &lt;code&gt;whereis&lt;/code&gt;. Now the array test checks if &lt;code&gt;$_&lt;/code&gt; is an element of the array. In the &lt;code&gt;137&lt;/code&gt; case it checks that the value is exactly that:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.27.7;
no warnings qw(experimental);

my @array = qw( 1 3 7 137 );

for( $ARGV[0] ) {
	whereis( $_ )         { say &quot;$_ is true&quot;; continue }
	whereis( not $_ % 2 ) { say &quot;$_ is even&quot;; continue }
	whereis( 137 )        { say &quot;Is 137&quot;; continue }
	whereis( @array )     { say &quot;In array branch&quot; }
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice that this works without explicitly enabling an experimental feature although I do get experimental warnings. I think this is an oversight because it reuses the &lt;code&gt;switch&lt;/code&gt; feature name which is loaded automatically when you &lt;code&gt;use&lt;/code&gt; (not &lt;code&gt;require&lt;/code&gt;) v5.10 or above. It should get a new feature name but I&#39;m guessing that the wide-ranging scope of the change can&#39;t co-exist with the old ways. I&#39;m not happy that this hasn&#39;t gone through the &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;formal experimental feature process&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Perl v5.27.7 has an experimental feature that unexpectedly loads. I could turn off the switch feature (&lt;code&gt;no feature qw(switch)&lt;/code&gt;) but I have no way to get back to its old behavior. This is quite unfriendly and breaks the social contract and comfort that &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;perlpolicy&lt;/a&gt; was supposed to provide to enterprise users. This trust, once broken, will be almost impossible to regain. And, it&#39;s easy to foresee future changes to this feature because it&#39;s more probable that it will miss something or users won&#39;t like it simply because so many new features have had that outcome.
&lt;/p&gt;



&lt;p&gt;
I quite admire &lt;a href=&quot;https://peps.python.org/&quot;&gt;Python&#39;s Enhancement Proposals&lt;/a&gt; and the visibility they give to the wider community. Hat tip to &lt;a href=&quot;https://blogs.perl.org/users/leon_timmermans/2017/12/smartmatch-in-5277.html&quot;&gt;&lt;span class=&quot;person&quot;&gt;Leon Timmermans&lt;/span&gt; for posting on blogs.perl.org about the already merged change&lt;/a&gt;. Perhaps part of perl5porter&#39;s policy for experimental changes should include fully fleshed out discussions posted somewhere (manywheres) that have a wide audience beyond core developers. Making the workaday programmer pay attention to the minutiae of daily perl5porters to catch these things isn&#39;t reasonable. However, remember what &lt;a href=&quot;https://www.perl.com/pub/2000/11/perl6rfc.html/&quot;&gt;&lt;span class=&quot;person&quot;&gt;Mark Jason Dominus&lt;/span&gt; said about the Perl 6 RFC process&lt;/a&gt;: serious applicants only!
&lt;/p&gt;



&lt;p&gt;
Perl (either current flavor) makes us think we could have natural language programming (even if we didn&#39;t) and now new language features try to be linguistically clever even though that&#39;s one of the biggest complaints about my favorite language. The debacle of &lt;code&gt;given&lt;/code&gt; and its lexical &lt;code&gt;$_&lt;/code&gt; (&lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/&quot;&gt;removed in v5.24&lt;/a&gt;) pushed that a bit too far with implied magic and inadequate testing.
&lt;/p&gt;



&lt;p&gt;
As part of this change &lt;code&gt;given&lt;/code&gt; is now just a loop that happens to run exactly one time. Even though the keyword v5.10 chose was not &lt;code&gt;switch&lt;/code&gt; (as in C), that&#39;s how people seem to pronounce &lt;code&gt;given&lt;/code&gt;. The language people want to use and what Perl actually uses doesn&#39;t match. 
&lt;/p&gt;



&lt;p&gt;
I&#39;ve already written that people should &lt;a href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;use &lt;code&gt;for&lt;/code&gt; instead&lt;/a&gt; even though p5p fixed the &lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/&quot;&gt;topicalizing bug&lt;/a&gt; that led to that advice. With this shift in the language we should get rid of &lt;code&gt;given&lt;/code&gt; too. It&#39;s likely at the same spots you&#39;ll already have to modify with this change.
&lt;/p&gt;



&lt;p&gt;
As long as anyone tries to be clever with the natural language words most people are not going to remember which one smart matches. Many other interesting words are already functions in modules (&lt;code&gt;whereis&lt;/code&gt; in &lt;a href=&quot;https://metacpan.org/pod/Unix::Whereis&quot;&gt;Unix::Whereis&lt;/a&gt;, &lt;code&gt;where&lt;/code&gt; in &lt;a href=&quot;https://metacpan.org/pod/MooseX::Types&quot;&gt;MooseX::Types&lt;/a&gt; and &lt;a href=&quot;https://metacpan.org/pod/PDL&quot;&gt;PDL&lt;/a&gt;, and &lt;code&gt;with&lt;/code&gt; in various modules). The core developers could have long conversations and eventually settle on something that they like, but all those conversations will have to be replayed for each programmer on their first encounter with the new words. We shouldn&#39;t settle for words we like; we should use words our audience will like and understand.
&lt;/p&gt;



&lt;p&gt;
I&#39;m in the camp that thinks the keywords should mostly explain themselves. Why not call the one that smart matches &lt;code&gt;smartmatch&lt;/code&gt; and let &lt;code&gt;if&lt;/code&gt; with an explicit loop control do the rest? I&#39;m sure I&#39;ll remember which one of these does what:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;for( $ARGV[0] ) {
	if( $_ )             { say &quot;$_ is true&quot;; next }
	smartmatch( @array ) { say &quot;In array branch&quot;  }
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Not only that, this reads better in English. It&#39;s an imperative sentence that tells me exactly what to do. Heck, even a &lt;code&gt;smartif&lt;/code&gt; will do. This could be a new feature with a new name that doesn&#39;t disturb the deprecated &lt;code&gt;when&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt;, and &lt;code&gt;given&lt;/code&gt; that would disappear in v5.32. That&#39;s a lot more than five months notice of code breaking changes.
&lt;/p&gt;



&lt;p&gt;
If your system automatically upgrades to the latest &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt;, I suggest that you separately install the &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; you want to use and target that (&lt;span class=&quot;item&quot;&gt;Item 110. Compile and install your own perls.&lt;/span&gt;). I&#39;ve written about &lt;a href=&quot;/2011/02/choose-the-right-perl-version-for-you/&quot;&gt;Choose the right perl version for you&lt;/a&gt; (which I might have to update to recommend not using v5.28) and I&#39;ve noted that &lt;a href=&quot;/2015/11/apple-recommends-installing-your-own-perl/&quot;&gt;Apple recommends that you don&#39;t use their distributed &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt;&lt;/a&gt;. It&#39;s easy to &lt;a href=&quot;/2010/10/compile-a-development-version-of-perl/&quot;&gt;compile a development version&lt;/a&gt; so you can test these things on your code base before major changes make it into a maintenance release. I&#39;ve also explained &lt;a href=&quot;/2011/02/choose-the-right-perl-version-for-you/&quot;&gt;how to tell the difference between maintenance and development releases&lt;/a&gt;.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Don&#39;t use POSIX to create temporary files</title>
		<link>https://www.effectiveperlprogramming.com/2017/09/dont-use-posix-to-create-temporary-files/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/09/dont-use-posix-to-create-temporary-files/</guid>
		<pubDate>Sun, 03 Sep 2017 19:04:18 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Stop making huge security holes with POSIX &lt;code&gt;tmpnam&lt;/code&gt;. You don&#39;t need it in Perl because &lt;a href=&quot;https://metacpan.org/pod/File::Temp&quot;&gt;File::Temp&lt;/a&gt;, which comes with Perl, does it for you. Perl v5.22 deprecated &lt;code&gt;tmpnam&lt;/code&gt; (and recommended replacements for &lt;code&gt;tmpfile&lt;/code&gt;) and v5.26 has removed it.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;The problem&lt;/h2&gt;


&lt;p&gt;
This isn&#39;t a change to Perl so much as a reaction in the Perl source to the recommendations of the underlying C toolkit. The POSIX &lt;code&gt;tmpnam&lt;/code&gt; tries to create a filename that no one else is using and that you can safely use as a new file name. This is often used where you don&#39;t want to keep the file but you need a place to store some data for a bit. Maybe you want to completely create a file before you move it into its final path (as in &lt;a href=&quot;/2017/12/in-place-editing-gets-safer-in-v5-28/&quot;&gt;In-place editing gets safer in v5.28&lt;/a&gt;).
&lt;/p&gt;



&lt;p&gt;
The problem is that there&#39;s some time between the creation of the possible filename and its use. If another process creates that filename before you can, there&#39;s a problem. That&#39;s a &quot;race condition&quot; where you are competing with everything in the world to finish your process and hoping that nothing disturbs your assumptions.
&lt;/p&gt;



&lt;p&gt;
&lt;a href=&quot;https://owasp.org/&quot;&gt;The Open Web Application Security Project (OWASP)&lt;/a&gt; has a good summary of the issues in &lt;a href=&quot;https://wiki.owasp.org/index.php/Insecure_Temporary_File&quot;&gt;Insecure Temporary File&lt;/a&gt;. &lt;a href=&quot;https://web.archive.org/web/20181107175927/http://www.linuxsecurity.com/content/view/115462/81&quot;&gt;Safely Creating Temporary Files in Shell Scripts&lt;/a&gt; has interesting things to say.
&lt;/p&gt;


&lt;h2&gt;Using a temp file&lt;/h2&gt;


&lt;p&gt;
Instead of using &lt;a href=&quot;https://metacpan.org/pod/POSIX&quot;&gt;POSIX&lt;/a&gt;, you can use &lt;a href=&quot;https://metacpan.org/pod/File::Temp&quot;&gt;File::Temp&lt;/a&gt;&#39;s &lt;code&gt;tempfile&lt;/code&gt;. With no arguments it opens a file somewhere and returns the writable filehandle and filename in one shot:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use File::Temp qw(tempfile);
my($fh, $filename) = tempfile();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s a run to show the crazy filename:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -MFile::Temp=tempfile -le &#39;print +((tempfile())[1])&#39;
/var/folders/jf/7sn23hrs11jcrn2w39wm6k_r0000gn/T/YRqlUzR6zZ
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can set several options for &lt;code&gt;tempfile&lt;/code&gt;:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;code&gt;DIR&lt;/code&gt; (string)—create the temporary file in this directory. The generated filename is &lt;code&gt;catfile&lt;/code&gt;d with this path. Otherwise, it uses the value of &lt;code&gt;TMPDIR&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;TMPDIR&lt;/code&gt; (boolean)—use the operating system-specific value from &lt;code&gt;File::Spec-&gt;tmpdir&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;TEMPLATE&lt;/code&gt; (string)—a string template for the filename. There must be at least four trailing &lt;code&gt;X&lt;/code&gt; characters which will be replaced by a generated substring.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SUFFIX&lt;/code&gt; (string)—add this suffix to the name (so the &lt;code&gt;TEMPLATE&lt;/code&gt; can end in four or more &lt;code&gt;X&lt;/code&gt;s)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;UNLINK&lt;/code&gt; (boolean)—if true, remove the file when nothing references it anymore.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;EXLOCK&lt;/code&gt; (boolean)—if true, open the file with an exclusive lock&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
Here are the defaults (so you wouldn&#39;t call it like this):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $fh, $filename ) = tempfile(
	DIR      =&gt;  undef,     # where to put the file
	SUFFIX   =&gt;  &#39;&#39;,        # add this at the end of the name
	UNLINK   =&gt;  0,         # clean-up the file at the end
	TMPDIR   =&gt;  0,         # where to put the file
	EXLOCK   =&gt;  0,         # exclusive lock
	TEMPLATE =&gt;  &#39;X&#39; x 10,  # increment files with the same pattern
	);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might like to name your files after the program that produces it, add a suffix, and automatically remove it when it&#39;s done:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use File::Basename;
my( $fh, $filename ) = tempfile(
	SUFFIX   =&gt;  &#39;.tmp&#39;,
	UNLINK   =&gt;  1,
	TEMPLATE =&gt;  basename($0) . &#39;-XXXXXXXXXXX&#39;,
	);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s a perlfaq &lt;a href=&quot;https://perldoc.perl.org/perlfaq5#How-do-I-change%2c-delete%2c-or-insert-a-line-in-a-file%2c-or-append-to-the-beginning-of-a-file%3f&quot;&gt;How do I change, delete, or insert a line in a file, or append to the beginning of a file?&lt;/a&gt;. The basic answer is that you open a new file and write data to it in whatever order you want. That new file can then become the old file.
&lt;/p&gt;



&lt;p&gt;
It&#39;s important in these operations that you don&#39;t replace the existing data until you know you&#39;ve successfully created its replacement. You can do that in a temporary file first. After the temporary file is completely done you can move it in place of the original.
&lt;/p&gt;



&lt;p&gt;
Here&#39;s an example from the FAQ answer:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open my $in,  &#39;&amp;lt;&#39;,  $file      or die &quot;Can&#39;t read old file: $!&quot;;
open my $out, &#39;&gt;&#39;, &quot;$file.new&quot; or die &quot;Can&#39;t write new file: $!&quot;;
while( &amp;lt;$in&gt; ) {
    print $out $_;
}
close $out;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Let &lt;code&gt;File::Temp&lt;/code&gt; take care of all the details. Don&#39;t remove the original data immediately when you are done, though; you want to move the original out of the way and move the new version into place. Something might go wrong at each step and you don&#39;t want to lose the data anywhere along the way:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use File::Temp qw(tempfile);

open my $in,  &#39;&amp;lt;&#39;,  $file or die &quot;Can&#39;t read old file: $!&quot;;
my( $out, $tempfile ) = tempfile( UNLINK =&gt; 0 );

while( &amp;lt;$in&gt; ) {
    print $out $_;
}
close $in;
close $out;

rename $file     =&gt; &quot;$file.orig&quot; or die &quot;Could not move original&quot;;
rename $tempfile =&gt; $file        or die &quot;Could not move new&quot;;
unlink &quot;$file.orig&quot;              or die &quot;Could not remove original&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Don&#39;t make temporary filenames with POSIX&lt;/li&gt;
&lt;li&gt;Use &lt;a href=&quot;https://metacpan.org/pod/File::Temp&quot;&gt;File::Temp&lt;/a&gt; to create temporary files and directories&lt;/li&gt;
&lt;li&gt;Rewrite data completely to a temporary file before you replace the original data&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>A hash in scalar context now returns the number of keys</title>
		<link>https://www.effectiveperlprogramming.com/2017/08/keys-in-scalar-context-now-returns-the-number-of-keys/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/08/keys-in-scalar-context-now-returns-the-number-of-keys/</guid>
		<pubDate>Fri, 25 Aug 2017 15:23:18 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Starting in v5.26, a hash in scalar context evaluates to the number of keys in the hash. You might have thought that it always did that just like an array (not a list!) in scalar context evaluates to the number of items. But nope—it evaluated to a seemingly useless number called the &quot;hash statistics&quot;. Now it&#39;s fixed to do what most people thought it already did. For what it&#39;s worth, &lt;code&gt;keys&lt;/code&gt; (or &lt;code&gt;values&lt;/code&gt;) in scalar context already provided the count.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Here are several uses of a hash in scalar context. With an empty hash you accidentally get the number of keys, but after that it looks like a fraction. That&#39;s the statistics of hash bucket use. The hash allocated a certain number of &quot;buckets&quot; to store things. Don&#39;t worry about what that is unless you feel like spending your free time perusing the &lt;code&gt;perl&lt;/code&gt; source code. The bottom number of the fraction is the count of the allocated buckets. The top is the number of buckets actually used. A normal hash spreads out what it stores and allocates extra buckets for future values. In these programs you can see that the hash starts with a lot of buckets and allocates more when over half of them are used (and adding more keys doesn&#39;t necessarily mean using a new bucket):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.24.3 -le &#39;%h = qw(); print scalar %h&#39;
0
$ perl5.24.3 -le &#39;%h = qw(q b); print scalar %h&#39;
1/8
$ perl5.24.3 -le &#39;%h = qw(a b); print scalar %h&#39;
1/8
$ perl5.24.3 -le &#39;%h = qw(a b c d); print scalar %h&#39;
2/8
$ perl5.24.3 -le &#39;%h = qw(a b c d e f g h ); print scalar %h&#39;
3/8
$ perl5.24.3 -le &#39;%h = qw(a b c d e f g h i j ); print scalar %h&#39;
4/8
$ perl5.24.3 -le &#39;%h = qw(a b c d e f g h i j k l); print scalar %h&#39;
4/8
$ perl5.24.3 -le &#39;%h = qw(a b c d e f g h i j k l m n ); print scalar %h&#39;
5/8
$ perl5.24.3 -le &#39;%h = qw(a b c d e f g h i j k l m n o p); print scalar %h&#39;
6/16
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Running these same programs under v5.26 is boring. You get back the number of keys in all cases:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.26.2 -le &#39;%h = qw(a b c d e f g h i j k l m n o p); print scalar %h&#39;
8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This doesn&#39;t mean that I recommend using a hash like this, but then I use an array like this all the time. I&#39;ve probably spent too much time writing &lt;code&gt;keys %hash&lt;/code&gt; to change now.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.28 new features</title>
		<link>https://www.effectiveperlprogramming.com/2017/08/perl-v5-28-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/08/perl-v5-28-new-features/</guid>
		<pubDate>Fri, 11 Aug 2017 09:16:14 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-28</category>
		<description>&lt;p&gt;
Perl v5.28 has a few new features and several removals of old features that you shouldn&#39;t have been using anyway (so I&#39;m not going to mention them). 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
 	&lt;li&gt;&lt;a href=&quot;/2017/08/deprecation-warnings-now-tell-you-when-it-will-disappear/&quot;&gt;The zero-width heredoc delimiter is fatal&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2017/12/beware-of-the-removal-of-when-in-perl-v5-28/&quot;&gt;Beware of the removal of when in Perl v5.28&lt;/a&gt; (now reverted but not dead)&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2017/12/in-place-editing-gets-safer-in-v5-28/&quot;&gt;In-place editing doesn&#39;t replace the original file until you are done&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2019/02/use-alpha-assertions-for-more-understandable-regexes/&quot;&gt;Regex alpha assertions give maintainers a better clue about which feature you&#39;re using&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2018/08/perl-v5-28-can-delete-key-value-slices/&quot;&gt;&lt;code&gt;delete&lt;/code&gt; now works on key/value slices&lt;/a&gt;. &lt;!-- https://rt.perl.org/Ticket/Display.html?id=131328 --&gt;&lt;/li&gt;
 	&lt;li&gt;Subroutines no longer need type globs.&lt;/li&gt;
 	&lt;li&gt;Opening the same symbol as both a file and directory handle is no longer allowed&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2019/03/match-only-the-same-unicode-script/&quot;&gt;Avoid mixed Unicode scripts (a data spoofing tactic)&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2018/08/initialize-array-and-hash-variables-with-state/&quot;&gt;Initialisation of aggregate state variables&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2015/07/make-bitwise-operators-always-use-numeric-context/&quot;&gt;String- and numeric-specific bitwise operators introduced in v5.22 are no longer experimental&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;/2018/01/unquoted-empty-heredoc-terminators-are-now-fatal/&quot;&gt;Unquoted bare heredocs are now fatal&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;a href=&quot;/2018/08/use-unicode-10-in-perl-v5-28/&quot;&gt;Updated to Unicode 10&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Deprecation warnings now tell you when the features will disappear</title>
		<link>https://www.effectiveperlprogramming.com/2017/08/deprecation-warnings-now-tell-you-when-it-will-disappear/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/08/deprecation-warnings-now-tell-you-when-it-will-disappear/</guid>
		<pubDate>Fri, 11 Aug 2017 09:15:07 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Perl v5.28 will have better deprecation warnings. Most of this I picked up from the &lt;a href=&quot;https://act.perlconference.org/tpc-2017-amsterdam/talk/7165&quot;&gt;lightning talk that &lt;span class=&quot;person&quot;&gt;Abigail&lt;/span&gt;&lt;/a&gt; gave at &lt;a href=&quot;https://act.perlconference.org/tpc-2017-amsterdam/&quot;&gt;The Perl Conference&lt;/a&gt;. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Perl 5, released in 1994, deprecated many things that it never removed. It&#39;s over 20 years later and those things are still there. Even though they are deprecated they had no schedule for actual removal. The Perl 5 Porters are now paying attention to these things and following &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;perlpolicy&lt;/a&gt; by removing them after two (or more) stable releases. Some things have been scheduled for particular releases as noted in &lt;a href=&quot;https://perldoc.perl.org/perldeprecation&quot;&gt;perldeprecation&lt;/a&gt;. Similarly, you might be interested in &lt;a href=&quot;https://perldoc.perl.org/perlexperiment&quot;&gt;perlexperiment&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Consider the misfeature of an opening heredoc with no label:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use strict;
use warnings;

my $string = &amp;lt;&amp;lt;;
	This is the heredoc

print $string;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You get the warning:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.8.9 bare-heredoc.pl
Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is deprecated
	This is the heredoc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In the just released v5.26.0 the warning got a bit more interesting. The deprecations now have a schedule and the warnings note that:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.26.0 heredoc.pl
Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is deprecated. Its use will be fatal in Perl 5.28 at heredoc.pl line 5.
	This is the heredoc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In v5.28 you&#39;ll need to use the quote marks:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use strict;
use warnings;

my $string = &amp;lt;&amp;lt;&#39;&#39;;
	This is the heredoc

print $string;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
v5.27 already makes this fatal:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.27.2 -c heredoc.pl
Use of bare &amp;lt;&amp;lt; to mean &amp;lt;&amp;lt;&quot;&quot; is forbidden
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
It might look odd to have no visible closer for the heredoc. But, here&#39;s a curious use in the new &lt;a href=&quot;/2016/12/strip-leading-spaces-from-here-docs-with-v5-26/&quot;&gt;indented heredoc&lt;/a&gt;. With a zero-length delimiter and an indented closing line you have to be sure the whitespace stays in the file. I&#39;ve used ⤷ to represent a tab:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use strict;
use warnings;

my $string = &amp;lt;&amp;lt;~&#39;&#39;;
⤷   This is the heredoc
⤷

print $string;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
I&#39;ve set my editor to automatically remove trailing whitespace. That will change the output of this program. But I don&#39;t plan on using a zero-length delimiter for my heredocs. I only know I can because &lt;span class=&quot;person&quot;&gt;Abigail&lt;/span&gt; gave a lightning talk about it.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>You must escape the left brace in a regex</title>
		<link>https://www.effectiveperlprogramming.com/2017/04/you-must-escape-the-left-brace-in-a-regex/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/04/you-must-escape-the-left-brace-in-a-regex/</guid>
		<pubDate>Wed, 26 Apr 2017 01:53:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
v5.30 brings this back. The exceptions to allow people to catch up should have already been fixed.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
In v5.26 and later, you have to escape the left brace, &lt;code&gt;{&lt;/code&gt;, in Perl regular expressions. You probably never thought about this overworked character, but lots of other people have. This is an important change because it&#39;s a fatal issue that may cause your modules and other tools (such as an old version of &lt;a href=&quot;https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=787531&quot;&gt;autoconf&lt;/a&gt;!) to stop working. But, we&#39;ve also known about this for a bit, so if you are up-to-date, things may have already been fixed.
&lt;/p&gt;



&lt;p class=&quot;text-center display-4 fw-bold&quot;&gt;{&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The short answer is to test your code with v5.26 before you deploy it. If you encounter an unescaped left brace that isn&#39;t part of an existing feature, your test &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; will tell you. Find that brace and escape it with a backslash in front of it: &lt;code&gt;\{&lt;/code&gt;. You can also put it in a character class: &lt;code&gt;[{]&lt;/code&gt;. You might have to do this in code for tools that you didn&#39;t write (I did it for my local autotools, for instance). Although we shouldn&#39;t have to be told, don&#39;t push to master on Friday afternoon and leave for vacation.
&lt;/p&gt;



&lt;p&gt;
Now, consider braces a bit more. First, think of the things that braces do or can do:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;block delimiter&lt;/li&gt;
&lt;li&gt;identifier delimiter, &lt;code&gt;${ident}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;hash subscript&lt;/li&gt;
&lt;li&gt;hexadecimal character code points, &lt;code&gt;\x{ABCD}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;octal character code points, &lt;code&gt;\o{777}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Unicode character names, &lt;code&gt;\N{UNICODE NAME}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;delimiters for match, substitution, and transliteration operators&lt;/li&gt;
&lt;li&gt;Perl special variables, such as &lt;code&gt;${^MATCH}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
Inside regular expressions, they do even more (including the double-quoted features):
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;generalized quantifiers, &lt;code&gt;a{N}&lt;/code&gt;, &lt;code&gt;a{N,}&lt;/code&gt;, and &lt;code&gt;a{M,N}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;property names &lt;code&gt;\p{PROPERTY NAME}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/&quot;&gt;some zero width assertions&lt;/a&gt;, such as &lt;code&gt;\b{BREAKNAME}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;embedded code, &lt;code&gt;(?{ ... })&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;postponed regular subexpression, &lt;code&gt;(??{ ... })&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;relative references and backreferences, such as &lt;code&gt;\g{-1}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
I&#39;ve probably missed some, but that&#39;s already an impressive list. What&#39;s not there? The literal braces. It&#39;s starting to get crowded in there, and the list is growing.
&lt;/p&gt;



&lt;p&gt;
Perl has to figure out if that brace character in your pattern starts a feature (also making the preceding characters part of a feature). As Perl&#39;s regexes get new features, that &lt;code&gt;{&lt;/code&gt; is being used for much more.
&lt;/p&gt;



&lt;p&gt;
To make it easier for future expansion, you now have to escape a literal &lt;code&gt;{&lt;/code&gt;. You don&#39;t want a preceding character that used to be literal turning into a feature that uses &lt;code&gt;{&lt;/code&gt;. Conversely, you don&#39;t want a missing right brace to turn an unescaped left brace into a literal.
&lt;/p&gt;



&lt;p&gt;
If you want to read more about this, there&#39;s a short thread &lt;a href=&quot;https://www.nntp.perl.org/group/perl.perl5.porters/2012/11/msg195425.html&quot;&gt;On deprecating unescaped literal left brace&lt;/a&gt; from perl5porters in 2012.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>/xx means more meaningless whitespace (and that&#39;s good)</title>
		<link>https://www.effectiveperlprogramming.com/2017/01/xx-means-more-meaningless-whitespace-and-thats-good/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/01/xx-means-more-meaningless-whitespace-and-thats-good/</guid>
		<pubDate>Tue, 24 Jan 2017 20:24:22 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
The &lt;code&gt;/xx&lt;/code&gt; match operator flag lets you add insignificant horizontal whitespace to character classes. You&#39;ll have to upgrade to v5.26 to use it though, but that release is right around the corner.
&lt;/p&gt;



&lt;p&gt;
The &lt;code&gt;/x&lt;/code&gt; has been around since Perl 5. That allows you to spread out the parts of your pattern and to add internal comments. The new &lt;code&gt;/xx&lt;/code&gt; flag expands on that to move it inside character classes too.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;figure class=&quot;text-center&quot;&gt;
&lt;img src=&quot;/images/amsterdam_flag.png&quot; width=&quot;450&quot; height=&quot;300&quot; class=&quot;figure-img img-fluid&quot; alt=&quot;The flag of Amsterdam, not yet a match operator&quot;&gt;
&lt;figcaption class=&quot;figure-caption&quot;&gt;The flag of Amsterdam, not yet a match operator&lt;/figcaption&gt;
&lt;/figure&gt;



&lt;p&gt;
Suppose you started with this compact and dense pattern representing an Apple product number (partially, for brevity):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $string = &#39;FC007LL/A&#39;;

my $pattern = qr/[MFP][A-Z]\d+(?:LL|[FBEJTXYC])/;

say $string =~ $pattern ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can rewrite that with insignificant space and comments to show what the pieces mean. That&#39;s important for you to remember what you did, for other people to see what you did, and to double check your work. Remember, there&#39;s no such thing as &quot;self-documenting&quot; because you only see what you did, not what you were supposed to do. And, this is a good reason to construct a pattern with &lt;code&gt;qr//&lt;/code&gt; so you don&#39;t distract the later code with a bunch of regex lines:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $string = &#39;FC007LL/A&#39;;

my $pattern = qr/
	[MFP]  # M = First sale, F = Refurbished, P = Personalized
	[A-Z]  # probably not all these letters valid here
	\d+
	(?: # country codes
		LL |
		[FBEJTXYC]
	)
	\/
	[ABC] # revision, maybe beyond C
	\z
	/x;

say $string =~ $pattern ? &#39;Matched&#39; : &#39;Missed&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can&#39;t use spaces in the character classes, thinking that they are also insignificant. What you think is insignificant actually isn&#39;t. This allows an invalid space in the input to match:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $string = &#39; Z007LL/A&#39;;  # shouldn&#39;t match, but will in this example

my $pattern = qr/
	\A
	[ M  F  P ] # M = First sale, F = Refurbished, P = Personalized
	[ A-Z ]  # probably not all these letters here
	\d+
	(?: # country codes
		LL |
		[ FBEJTXYC ]
	)
	\/
	[ A B C ] # revision
	\z
	/x;

say $string =~ $pattern ? &#39;Matched&#39; : &#39;Missed&#39;;  # Matches, but oops!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
So, you might not have thought to do that since the innards of a character class are not that complicated.
&lt;/p&gt;



&lt;p&gt;
Prior to v5.26 (v5.25.9, really), you could use multiple &lt;code&gt;/x&lt;/code&gt; on the same match without additional effect, although with a warning since v5.21. Using two, three, or even more &lt;code&gt;/x&lt;/code&gt; didn&#39;t make the whitespace any more insignificant (but wouldn&#39;t that be cool!). But, with v5.26, an extra &lt;code&gt;/x&lt;/code&gt; allows you to add horizontal whitespace (space and tab) inside a character class. The previous example now fails to match because the spaces inside the character class are meaningless:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;  # or v5.25.9

my $string = &#39; Z007LL/A&#39;;  # doesn&#39;t match now

my $pattern = qr/
	\A
	[ M  F  P ]
	[ A-Z ]  # probably not all these letters here
	\d+
	(?: # country codes
		LL |
		[ FBEJTXYC ]
	)
	\/
	[ A B C ] # revision
	\z
	/xx;

say $string =~ $pattern ? &#39;Matched&#39; : &#39;Missed&#39;;  # Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
I can see how this will mostly help to emphasize ranges in the character class:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;

my $pattern = qr/
	[ a-t v-z A-T V-Z ]
	/xx;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What would be more interesting to me is the full use of the usual &lt;code&gt;/x&lt;/code&gt; inside the character classes. I could then annotate why each character is there and use vertical whitespace:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# THIS WILL NOT WORK, BUT IT WOULD BE NICE
my $pattern = qr/
	\A

	[
	M  # First sale
	F  # Refurbished
	P  # Personalized
	]

	[A-Z]  # probably not all these letters here

	\d+

	# The country code, many more I omit for brevity
	(?:L [
		A   # Colombia, Ecuador, El Salvador, Guatemala, Honduras, Peru
		E   # Argentina
		L   # US
		Z   # Chile, Paraguay, Uruguay
		]
	|
		[
		F   # France
		B   # Ireland, UK
		E   # Mexico
		J   # Japan
		T   # Italy
		X   # Australia, New Zealand
		Y   # Spain
		C   # Canada
		]
	)

	\/
	[ABC] # revision

	\z
	/xx;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perhaps there will be a future &lt;code&gt;/xxx&lt;/code&gt; operator that allows this. I could do the same thing with alternations, but that&#39;s a bit sloppy and I&#39;d rather make it easier for the regex engine.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>v5.26 removes dot from @INC</title>
		<link>https://www.effectiveperlprogramming.com/2017/01/v5-26-removes-dot-from-inc/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2017/01/v5-26-removes-dot-from-inc/</guid>
		<pubDate>Mon, 09 Jan 2017 19:18:56 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;figure class=&quot;text-center&quot;&gt;
&lt;img src=&quot;/images/no_dot.png&quot; height=&quot;244&quot; width=&quot;319&quot; class=&quot;figure-img img-fluid d-block mx-auto&quot; alt=&quot;A dot inside a red prohibition sign&quot;&gt;
&lt;figcaption class=&quot;figure-caption&quot;&gt;No more &lt;code&gt;.&lt;/code&gt; in &lt;code&gt;@INC&lt;/code&gt;&lt;/figcaption&gt;
&lt;/figure&gt;



&lt;p&gt;
As of v5.26, the &lt;code&gt;.&lt;/code&gt; in &lt;code&gt;@INC&lt;/code&gt; is gone by default. When you compile &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt;, it bakes the default module search path (based on your &lt;span class=&quot;binary&quot;&gt;configure&lt;/span&gt; settings) into the binary. These are the paths that &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; searches without you adding to &lt;code&gt;@INC&lt;/code&gt; with command-line switches or environment variables, and the paths you see when you run &lt;i&gt;perl -V&lt;/i&gt;:
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -V
... lots of other output ...
  @INC:
    /usr/local/perls/perl-5.24.0/lib/site_perl/5.24.0/darwin-2level
    /usr/local/perls/perl-5.24.0/lib/site_perl/5.24.0
    /usr/local/perls/perl-5.24.0/lib/5.24.0/darwin-2level
    /usr/local/perls/perl-5.24.0/lib/5.24.0
    .
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since that &lt;code&gt;.&lt;/code&gt; is not a directory you know about until &lt;code&gt;use&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt; starts to do their work and someone might fool your program into being in the wrong directory, that can lead to you loading a module you don&#39;t want or expect. I write more about that in &lt;a href=&quot;/2017/01/perl-v5-26-removes-from-inc-but-dont-think-youre-safe/&quot;&gt;Perl v5.26 removes . from @INC, but don&#39;t think you’re safe!&lt;/a&gt; on my &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt; blog. Consider the &lt;a href=&quot;/2012/12/the-storable-security-problem/&quot;&gt;Storable security problem&lt;/a&gt;, for instance.
&lt;/p&gt;



&lt;p&gt;
If you really, really want to find modules in the current working directory as the last place to look, you can compile your &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; with &lt;code&gt;-Ddefault_inc_excludes_dot&lt;/code&gt; not set &lt;em&gt;and&lt;/em&gt; the &lt;code&gt;PERL_USE_UNSAFE_INC&lt;/code&gt; environment variable set to 1 (exactly, not just true). But, this is a kludge to make a newly-compiled &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; work like the old ones. If you intend to keep looking in the current working directory for your libraries, you should include it through the normal library manipulation practices, such as &lt;code&gt;use lib&lt;/code&gt; and &lt;a href=&quot;https://metacpan.org/pod/FindBin&quot;&gt;FindBin&lt;/a&gt;.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Strip leading spaces from here-docs with v5.26</title>
		<link>https://www.effectiveperlprogramming.com/2016/12/strip-leading-spaces-from-here-docs-with-v5-26/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/12/strip-leading-spaces-from-here-docs-with-v5-26/</guid>
		<pubDate>Sun, 25 Dec 2016 05:24:01 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
I shut down the Learning Perl 6 site, so I removed the link to it. You can still see &lt;a href=&quot;https://web.archive.org/web/20191022135450/https://www.learningperl6.com/&quot;&gt;the last archive.org copy&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Perl v5.26 allows you to indent heredocs, that special multi-line string quoting mechanism. In v5.24 and earlier, the content of any here-doc included the entire line with any leading whitespace that might be there. That meant you typed here-docs in a way that broke the indention of the block that contained them. Perl 6 envisioned a way that this could work and now Perl 5 has stolen some of that.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
In this example, I have a here-doc in a subroutine just to have another scope. The lines of the string start at the beginning of the line rather than the beginning of the indentation:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub say_something {
	my $string =&amp;lt;&amp;lt;&#39;HERE&#39;;
This line is not indented
Neither is this line
And the delimiter is not indented
HERE

	print $string;
	}

say_something();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s a bit annoying to some people (and not a problem to some others). v5.26 (not yet released) allows you to modify the here-doc to strip leading whitespace. Put a ~ after the &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub say_something {
	my $string =&amp;lt;&amp;lt;~&#39;HERE&#39;;
		This line is not indented
		Neither is this line
		And the delimiter is not indented
		HERE

	print $string;
	}

say_something();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The parser looks at the space in front of the final delimiter and strips that much space:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;This line is not indented
Neither is this line
And the delimiter is not indented
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Any whitespace left over is part of the string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub say_something {
	my $string =&amp;lt;&amp;lt;~&#39;HERE&#39;;
		This line is not indented
			But this line is indented
		And the delimiter is not indented
		HERE

	print $string;
	}

say_something();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The second line kept the space after the whitespace that was before the delimiter:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;This line is not indented
	But this line is indented
And the delimiter is not indented
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
If each line does not start with at least that much space, it&#39;s a compile-time error. This won&#39;t work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.25;
sub say_something {

	my $string =&amp;lt;&amp;lt;~&quot;TAB_BEFORE&quot;;
		This is the first line
Another line that is a compilation error
		This is the last line
	TAB_BEFORE

	say $string;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You get an error at compile time (although in Perl 6 it tries to figure it out, which I don&#39;t think is a good solution):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Indentation on line 2 of here-doc doesn&#39;t match delimiter at 526.pl line 5.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The whitespace before the end delimiter must match exactly at the beginning of each line. You can mix tabs and spaces, but it won&#39;t try to figure out how many spaces are in a tab (although the answer is four).
&lt;/p&gt;



&lt;p&gt;
This also means that you have to be careful about de-tabbing or en-tabbing code, or when you change indention levels. You might accidentally change the number of whitespace characters.
&lt;/p&gt;


&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;v5.26 allows insignificant leading whitespace from here-docs&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;&amp;lt;&amp;lt;~&lt;/code&gt; to start the whitespace-stripping version of the here-doc.&lt;/li&gt;
&lt;li&gt;The here-doc will strip the exact same whitespace before the final delimiter.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl v5.24 adds a line break word boundary</title>
		<link>https://www.effectiveperlprogramming.com/2016/06/perl-v5-24-adds-a-line-break-word-boundary/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/06/perl-v5-24-adds-a-line-break-word-boundary/</guid>
		<pubDate>Sat, 11 Jun 2016 00:59:56 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-24</category>
		<description>&lt;p&gt;
Perl v5.24 adds a linebreak word boundary, &lt;code&gt;\b{lb}&lt;/code&gt;, to go along with the &lt;a href=&quot;/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/&quot;&gt;new word boundaries added in v5.22&lt;/a&gt;. This is part of Perl&#39;s increasing conformance with the regular expression requirements in &lt;a href=&quot;https://unicode.org/reports/tr18/&quot;&gt;Unicode Technical Standard #18&lt;/a&gt;. The &lt;a href=&quot;https://metacpan.org/pod/Unicode::LineBreak&quot;&gt;Unicode::LineBreak&lt;/a&gt; module implements the same thing, although you have to do a lot more work.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Consider this example that takes a long string that contains no vertical whitespace (despite how it may appear on a screen). You want to wrap it within a window of 50 to 70 columns wide, but you don&#39;t want to break in the middle of something:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.24;

my $string = &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&quot;;

$string =~ s/(\X{50,70}\b{lb})/$1\n/g;

say &quot;-&quot; x 73;
say $string;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The result is a wrapped paragraph:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The line-boundary break knows where to not break a line. It&#39;s not as simple as whitespace because some whitespace is non-breaking. Some hyphens can wrap around lines, and some sorts can&#39;t. It&#39;s a bit too complicated to deal with here but you can read about it in &lt;a href=&quot;https://www.unicode.org/reports/tr14/&quot;&gt;Unicode Standard Annex #14: &quot;Unicode Line Breaking Algorithm&quot;.&lt;/a&gt;
&lt;/p&gt;



&lt;p&gt;
You might also consider the &lt;a href=&quot;https://metacpan.org/pod/Text::Autoformat&quot;&gt;Text::Autoformat&lt;/a&gt; module for a complete string you already have.
&lt;/p&gt;



&lt;p&gt;
But, what about text you get piece-by-piece? You can add it to a buffer then check if there&#39;s a good place to break a line once the buffer gets too big:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.24;

my @words = split /\s/, &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&quot;;

my $buffer;
foreach my $word ( @words ) {
	$buffer .= $word . &#39; &#39;;
	next if length $buffer &amp;lt;= 40;
	if( $buffer =~ m/(\X{30,45}\b{lb})(.*)/ ) {
		say $1;
		$buffer = $2;
		}
	}

say $buffer;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Again, you get a wrapped paragraph:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Perl v5.22 adds fancy Unicode word boundaries</title>
		<link>https://www.effectiveperlprogramming.com/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/</guid>
		<pubDate>Sun, 05 Jun 2016 22:50:17 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;p&gt;
Perl v5.22&#39;s regexes added three Unicode boundaries to go along with the vanilla &quot;word&quot; boundary, &lt;code&gt;\b&lt;/code&gt;, that you&#39;ve been using for years. These new assertions aren&#39;t going to match perfectly with your expectations of human languages (the holy grail of natural language processing), but they do okay-ish. Although these appear in v5.22.0, as a late addition to the language they were partially broken in the initial release. They were fixed for v5.22.1.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Remember that &lt;code&gt;\b&lt;/code&gt; in all of its forms is an anchor. It doesn&#39;t match text, but specifies a condition at a position. Since it doesn&#39;t match text, it&#39;s also called a &quot;zero-width assertion&quot;.
&lt;/p&gt;


&lt;a href=&quot;https://unicode.org/reports/tr18/&quot;&gt;Unicode Technical Standard #18&lt;/a&gt; specifies several things a regular expression engine needs to handle for Unicode conformance. These new boundaries add to Perl&#39;s Unicode regex support.

&lt;h2&gt;Sentence Boundaries&lt;/h2&gt;


&lt;p&gt;
We don&#39;t have an end-of-sentence character. We use the full stop (.) or other punctuation, but we use those same characters for other things. &lt;a href=&quot;https://unicode.org/reports/tr29/&quot;&gt;Unicode Standard Annex #29&lt;/a&gt; lays out several heuristic rules for breaking text into sentences; the new &lt;code&gt;\b{sb}&lt;/code&gt; codifies those.
&lt;/p&gt;



&lt;p&gt;
This program inserts a bit of text wherever Perl matches a sentence boundary:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22.1;

$_ = &quot;See Spot. (Spot is a dog.) See Spot run. RunSpot, run!&quot;;

s/\b{sb}/#SB#/g;
say $_;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The string &lt;code&gt;#SB#&lt;/code&gt; shows up at the guessed sentence boundaries:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#SB#See Spot. #SB#(Spot is a dog.) #SB#See Spot run. #SB#RunSpot, run!#SB#
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There are several sentence boundaries:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;The start of the string&lt;/li&gt;
&lt;li&gt;The end of the string&lt;/li&gt;
&lt;li&gt;After closing punctuation and the succeeding whitespace.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
The guesses aren&#39;t perfect though. Try it with a different string.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22.1;

$_ = &quot;Welcome to U.S.A. Mr. Smith. How are you?&quot;;

s/\b{sb}/#SB#/g;
say $_;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This sentence has an abbreviation with full stops to separate the words (&lt;code&gt;U.S.A.&lt;/code&gt;) and a single word abbreviation (&lt;code&gt;Mr.&lt;/code&gt;). The guessing almost gets it right:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#SB#Welcome to U.S.A. #SB#Mr. #SB#Smith. #SB#How are you?#SB#
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The heuristic is smart enough not to break inside a group of uppercase letters separated by full stops. It&#39;s not smart enough to not break after &lt;code&gt;Mr.&lt;/code&gt; though. That&#39;s not a problem with Perl or a problem with Unicode. It&#39;s a problem with natural language representation.
&lt;/p&gt;


&lt;h2&gt;Word Boundaries&lt;/h2&gt;


&lt;p&gt;
Perl&#39;s &quot;word&quot; boundary, &lt;code&gt;\b&lt;/code&gt;, is a bit of a misnomer that we have to explain carefully in &lt;a href=&quot;https://www.learning-perl.com/&quot;&gt;Learning Perl&lt;/a&gt; classes. Perl v5.22 adds a somewhat better word boundary, &lt;code&gt;\b{wb}&lt;/code&gt;, based on the heuristics in &lt;a href=&quot;https://unicode.org/reports/tr29/&quot;&gt;Unicode Standard Annex #29&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Perl&#39;s idea of a &quot;word&quot; is something that contains only &quot;word&quot; characters. Those are letters, digits, and the underscore. That&#39;s the character class shortcut &lt;code&gt;\w&lt;/code&gt;. It&#39;s also locale dependent. And, it&#39;s different for ASCII and the Universal Character Set (&lt;a href=&quot;https://perldoc.perl.org/perlrecharclass&quot;&gt;perlrecharclass&lt;/a&gt; explains it). It&#39;s not what we think of as natural words. For instance, consider the &lt;a href=&quot;https://perldoc.perl.org/perlfaq4&quot;&gt;perlfaq4&lt;/a&gt; answer to &lt;a href=&quot;https://perldoc.perl.org/perlfaq4#How-do-I-capitalize-all-the-words-on-one-line%3f&quot;&gt;How do I capitalize all the words on one line?&lt;/a&gt; It&#39;s in the FAQ because it&#39;s a common mistake based on people&#39;s misusing &lt;code&gt;\w&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $string = &quot;fred and barney&#39;s lodge&quot;;
$string =~ s/(\w+)/\u\L$1/g;
say $string;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This code matches groups of &quot;word&quot; characters and capitalizes them. The &lt;code&gt;s&lt;/code&gt; after the apostrophe is also capitalized:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Fred And Barney&#39;S Lodge
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now look for the word boundaries, which are the positions between &quot;word&quot; characters and non-&quot;word&quot; characters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
my $string = &quot;fred and barney&#39;s lodge v2.0&quot;;
$string =~ s/\b/#WB#/g;
say $string;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There are plenty of boundaries:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#WB#fred#WB# #WB#and#WB# #WB#barney#WB#&#39;#WB#s#WB# #WB#lodge#WB# #WB#v2#WB#.#WB#0#WB#
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You see boundaries at:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Start of the string (a virtual non-&quot;word&quot; character)&lt;/li&gt;
&lt;li&gt;End of the string (a virtual non-&quot;word&quot; character)&lt;/li&gt;
&lt;li&gt;Between numbers and non-{letters|digits|underscore}&lt;/li&gt;
&lt;li&gt;Between whitespace and letters&lt;/li&gt;
&lt;/ul&gt;

&lt;a href=&quot;https://unicode.org/reports/tr29/&quot;&gt;Unicode Standard Annex #29&lt;/a&gt; defines some rules to avoid problems such as the apostrophe and decimal points. Try the same thing with the Unicode rules for word boundaries by using &lt;code&gt;\b{wb}&lt;/code&gt;:

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22.1;
my $string = &quot;fred and barney&#39;s lodge v2.0&quot;;
$string =~ s/\b{wb}/#WB#/g;
say $string;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now &lt;code&gt;barney&#39;s&lt;/code&gt; and &lt;code&gt;v2.0&lt;/code&gt; are considered complete words:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#WB#fred#WB# #WB#and#WB# #WB#barney&#39;s#WB# #WB#lodge#WB# #WB#v2.0#WB#
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Try it in a different way. Here&#39;s a program to pull out each word. It uses the match operator in scalar context to find text between word boundaries:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $string = &quot;fred and barney&#39;s lodge v2.0&quot;;

while( $string =~ m/\b(\w.*?)\b/g ) {
	say $1;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You get some words that we don&#39;t think of as words:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;fred
and
barney
s
lodge
v2
0
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Change those &lt;code&gt;\b&lt;/code&gt; to &lt;code&gt;\b{wb}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22.1;
my $string = &quot;fred and barney&#39;s lodge v2.0&quot;;

while( $string =~ m/\b{wb}(\w.*?)\b{wb}/g ) {
	say $1;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now &lt;code&gt;barney&#39;s&lt;/code&gt; and &lt;code&gt;v2.0&lt;/code&gt; stick together:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;fred
and
barney&#39;s
lodge
v2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Grapheme Boundaries&lt;/h2&gt;


&lt;p&gt;
The regex dot matches any character except the newline. However, in the Unicode world, a &quot;character&quot; isn&#39;t the same thing we think of as a character.
&lt;/p&gt;



&lt;p&gt;
We consider the whole concept and representation of an idea of the &quot;character&quot;. The &lt;i&gt;é&lt;/i&gt; is a &quot;character&quot; in our human minds, but that&#39;s not how we use the word &quot;character&quot; in the jargon. Speaking casually about these topics often leads to pain and frustration.
&lt;/p&gt;



&lt;p&gt;
In the bowels of computers, that single idea &lt;i&gt;é&lt;/i&gt; can be constructed of two parts: the base &lt;i&gt;e&lt;/i&gt; and the accent &lt;i&gt;´&lt;/i&gt;. Those are U+0065 (LATIN SMALL LETTER E) and U+0301 (COMBINING ACUTE ACCENT). Or, it might be a single part: U+00E9 (LATIN SMALL LETTER E WITH ACUTE). We call those forms the &quot;decomposed&quot; and &quot;composed&quot; version even though they represent the same idea. Some of that is a quirk of history. I write more about that in one of the appendices for &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Everything that you need to build the complete idea is the &quot;grapheme cluster&quot; (See &lt;a href=&quot;/2011/06/treat-unicode-strings-as-grapheme-clusters/&quot;&gt;Treat Unicode strings as grapheme clusters&lt;/a&gt;). That&#39;s more akin to what we think of as a written &quot;character&quot;. We probably want to match grapheme clusters and don&#39;t care how they are represented or constructed.
&lt;/p&gt;



&lt;p&gt;
If you are trying to break up text, you don&#39;t want to break apart the characters of a grapheme. Instead of matching the dot, you can use the &lt;code&gt;\X&lt;/code&gt; introduced in Perl v5.6. That matches an &quot;extended grapheme cluster&quot;, or, in the language of regexes, &lt;code&gt;(?&gt;\PM\pM*)&lt;/code&gt;. That&#39;s the sub-pattern that matches a non-mark character followed by zero or more mark characters.
&lt;/p&gt;



&lt;p&gt;
Perl v5.22.1 goes even further by adding a grapheme cluster boundary, &lt;code&gt;\b{gcb}&lt;/code&gt;, that recognizes the position between two grapheme clusters. This is also available as &lt;code&gt;\b{g}&lt;/code&gt;. Again, this is probably what we think of as &quot;characters&quot; in casual conversation.
&lt;/p&gt;



&lt;p&gt;
This demonstration takes a bit of setup. You have a literal string that has &lt;i&gt;é&lt;/i&gt; and &lt;i&gt;ö&lt;/i&gt;s. These can be composed or decomposed, and to guard against what editors and other translators might do, you use the &lt;code&gt;NFD&lt;/code&gt; from &lt;a href=&quot;https://metacpan.org/pod/Unicode::Normalize&quot;&gt;Unicode::Normalize&lt;/a&gt; to ensure you get the decomposed form. With the composed form this won&#39;t work. Then, you break that string apart in a variety of ways:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/Users/brian/bin/perls/perl5.22.1
use v5.22.1;
use utf8;
use open qw(:std :utf8);

use Unicode::Normalize qw(NFD);

my $string = NFD( &quot;The résumé of Ms. Sörensen.&quot; );

my @dot_chars = $string =~ m/(.)/g;
say &quot;    .: &quot;, join &#39; &#39;, @dot_chars;

my @gcb_chars = $string =~ m/(\X)/g;
say &quot;   \\X: &quot;, join &#39; &#39;, @gcb_chars;

my @split_chars = split /\b{gcb}/, $string;
say &quot;split: &quot;, join &#39; &#39;, @split_chars;

&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Matching with the dot distinguishes between the &lt;i&gt;e&lt;/i&gt; and the &lt;i&gt;´&lt;/i&gt; as well as the &lt;i&gt;o&lt;/i&gt; and the &lt;i&gt;¨&lt;/i&gt;. It sees those as different characters.
&lt;/p&gt;



&lt;p&gt;
The &lt;code&gt;\X&lt;/code&gt; sees the combination of &lt;i&gt;e&lt;/i&gt; and &lt;i&gt;´&lt;/i&gt; as a single grapheme cluster and keeps them together. Splitting on the &lt;code&gt;\b{gcb}&lt;/code&gt; also keeps the characters in their proper clusters:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;    .: T h e   r e ́ s u m e ́   o f   M s .   S o ̈ r e n s e n .
   \X: T h e   r é s u m é   o f   M s .   S ö r e n s e n .
split: T h e   r é s u m é   o f   M s .   S ö r e n s e n .
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now, consider this example that reverses the order of the matched elements before it prints them:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22.1;
use utf8;
use open qw(:std :utf8);

use Unicode::Normalize qw(NFD);

my $string = NFD( &quot;The résumé of Ms. Sörensen.&quot; );

my @dot_chars = $string =~ m/(.)/g;
say &quot;    .: &quot;, join &#39;&#39;, reverse @dot_chars;

my @split_chars = split /\b{gcb}/, $string;
say &quot;split: &quot;, join &#39;&#39;, reverse @split_chars;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can easily see the problem of pulling apart grapheme clusters as the accent characters now attach to the wrong characters. Splitting on the grapheme cluster boundary gets it right:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;    .: .nesner̈oS .sM fo ́emuśer ehT
split: .nesneröS .sM fo émusér ehT
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Perl v5.22 expands the word boundary for particular types with the &lt;code&gt;\b{}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;\b{sb}&lt;/code&gt; matches a sentence boundary.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;\b{wb}&lt;/code&gt; matches an improved word boundary.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;\b{gcb}&lt;/code&gt; matches at a grapheme cluster boundary.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl v5.26 new features</title>
		<link>https://www.effectiveperlprogramming.com/2016/06/perl-v5-26-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/06/perl-v5-26-new-features/</guid>
		<pubDate>Wed, 01 Jun 2016 01:25:36 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-26</category>
		<description>&lt;p&gt;
Perl v5.26 has lots of improvements and some big changes with module loading. This might be the most backward-incompatible version this decade! As with any new perl, try it before you commit to it. You don&#39;t want to install it over your currently working perl to later find out something doesn&#39;t work.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/&quot;&gt;Mix conventional list assignments and ref aliasing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2018/08/find-the-new-emojis-in-perls-unicode-support/&quot;&gt;Updated to Unicode 9 with 72 new emojis&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/04/you-must-escape-the-left-brace-in-a-regex/&quot;&gt;You must escape literal left braces, &lt;code&gt;{&lt;/code&gt;, in regular expressions&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/01/xx-means-more-meaningless-whitespace-and-thats-good/&quot;&gt;The &lt;code&gt;/xx&lt;/code&gt; match operator flag allows insignificant whitespace in character class&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2016/12/strip-leading-spaces-from-here-docs-with-v5-26/&quot;&gt;Here-docs can strip leading whitespace&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/01/v5-26-removes-dot-from-inc/&quot;&gt;Removal of . from &lt;code&gt;@INC&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2018/10/use-capture-to-get-a-list-of-all-the-capture-buffers/&quot;&gt;New variables &lt;code&gt;@{^CAPTURE}&lt;/code&gt;, &lt;code&gt;%{^CAPTURE}&lt;/code&gt;, and &lt;code&gt;%{^CAPTURE_ALL}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/08/keys-in-scalar-context-now-returns-the-number-of-keys/&quot;&gt;A hash in scalar context will return the number of keys, not the hash statistics.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/01/named-lexical-subroutines/&quot;&gt;Named lexical subroutines are no longer experimental&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/09/dont-use-posix-to-create-temporary-files/&quot;&gt;&lt;code&gt;POSIX::tmpnam()&lt;/code&gt; is out; use &lt;code&gt;File::Temp&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/08/deprecation-warnings-now-tell-you-when-it-will-disappear/&quot;&gt;Deprecation warnings now tell you when the feature will be removed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2018/04/perl-v5-26-now-recognizes-version-control-conflict-markers/&quot;&gt;Perl detects version control markers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
You can read through the &lt;a href=&quot;https://github.com/Perl/perl5/blob/blead/pod/perl5260delta.pod&quot;&gt;perldelta5.26.0&lt;/a&gt; documentation. 
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Lexical $_ and autoderef are gone in v5.24</title>
		<link>https://www.effectiveperlprogramming.com/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/</guid>
		<pubDate>Thu, 14 Apr 2016 21:02:26 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-24</category>
		<description>&lt;p&gt;
Two features that I have previously discouraged are now gone from Perl: the lexical &lt;code&gt;$_&lt;/code&gt; and auto dereferencing.
&lt;/p&gt;



&lt;p&gt;
The lexical &lt;code&gt;$_&lt;/code&gt; was a consequence of the way Perl wanted smart match to work. In a &lt;code&gt;given-when&lt;/code&gt;, instead of aliasing &lt;code&gt;$_&lt;/code&gt; like &lt;code&gt;foreach&lt;/code&gt; does, the block had an implicit &lt;code&gt;my $_ = ...&lt;/code&gt;. This interfered with the package version, as I wrote about in &lt;a href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;Use for() instead of given()&lt;/a&gt; and &lt;a href=&quot;/2014/11/perl-v5-16_now_sets_proper_magic_on_lexical/&quot;&gt;Perl v5.16 now sets proper magic on lexical $_&lt;/a&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

In &lt;span class=&quot;book-title&quot;&gt;Effective Perl Programming&lt;/span&gt;, we initially promoted this feature in &lt;span class=&quot;item&quot;&gt;Item 24: Use given-when to make a switch statement&lt;/span&gt; and &lt;span class=&quot;item&quot;&gt;Item 15: Use $_ for elegance and brevity&lt;/span&gt;. Our advice on lexical &lt;code&gt;$_&lt;/code&gt; is dated and contrary to what we suggested for years. Now, with v5.24, it doesn&#39;t matter because the feature is gone. (&lt;span class=&quot;person&quot;&gt;Reini Urban&lt;/span&gt; has more to say about this in &lt;a href=&quot;https://blogs.perl.org/users/rurban/2016/04/the-removal-of-the-lexical-topic-feature-in-524.html&quot;&gt;The removal of the lexical topic feature in 5.24&lt;/a&gt;).


&lt;p&gt;
The other feature, automatic dereferencing for array and hash operators, I was never sold on. I&#39;m not even settled on the notion that &lt;code&gt;keys&lt;/code&gt; or &lt;code&gt;values&lt;/code&gt; should be able to operate on both (&lt;span class=&quot;person&quot;&gt;chromatic&lt;/span&gt; wrote about it in &lt;a href=&quot;https://www.modernperlbooks.com/mt/2012/03/inadvertent-inconsistencies-each-in-perl-512.html&quot;&gt;Inadvertent Inconsistencies: each in Perl 5.12&lt;/a&gt;). This feature from v5.14 let you pass a scalar variable to the array and hash operators. You had to trust that the right sort of value would end up there since Perl could not check this at compile-time:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @keys = keys $hash_or_array_reference;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
I discouraged this in &lt;a href=&quot;/2012/03/dont-use-auto-dereferencing-with-each&quot;&gt;Don&#39;t use auto-dereferencing with each or keys&lt;/a&gt;. The feature was confused even when it was stable, as you can read in &lt;a href=&quot;https://github.com/Perl/perl5/issues/10896&quot;&gt;Bug #80626 for perl5: More problems with push/keys $scalar&lt;/a&gt;. You can read the conversation about removing it in &lt;a href=&quot;https://github.com/Perl/perl5/issues/13181&quot;&gt;Bug #119437 for perl5: [EXPERIMENT] autoderef, the implicit deref in push REF and others&lt;/a&gt;. Now it&#39;s gone too.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Postfix dereferencing is stable in v5.24</title>
		<link>https://www.effectiveperlprogramming.com/2016/04/postfix-dereferencing-is-stable-is-v5-24/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/04/postfix-dereferencing-is-stable-is-v5-24/</guid>
		<pubDate>Thu, 14 Apr 2016 19:51:08 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-24</category>
		<description>&lt;p&gt;
Perl&#39;s dereferencing syntax might be, or even should be, responsible for people&#39;s disgust at the language. In v5.20, Perl added the &lt;a href=&quot;/2014/09/use-postfix-dereferencing/&quot;&gt;experimental postfix dereferencing syntax&lt;/a&gt; that made this analogous to method chaining. This is one of the most pleasing Perl features I&#39;ve encountered in years.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Now you can leave out the pragmas to enable the experimental feature and turn off its warning. This example defines a subroutine &lt;code&gt;$gimme_a_ref&lt;/code&gt; that returns an array ref that contains a hash ref. In the &lt;code&gt;foreach&lt;/code&gt; I dereference the subroutine to get the array ref, use postfix dereferencing to pull out the first array element, then dereference that value to be a hash that I can use with &lt;code&gt;keys&lt;/code&gt;. That&#39;s a lot going on, but just like I explained it in this paragraph, I write it from left to right in code:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.24;

my $gimme_a_ref = sub {
	state $hash_ref = {
		cat =&gt; &#39;Buster&#39;,
		dog =&gt; &#39;Addy&#39;,
		};

	[ $hash_ref ];
	};

foreach my $key ( keys $gimme_a_ref-&gt;()-&gt;@[0]-&gt;%* ) {
	say &quot;key is $key&quot;;
	}

my %hash = reverse $gimme_a_ref-&gt;()-&gt;@[0]-&gt;%*;
say Dumper( \%hash ); use Data::Dumper;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you don&#39;t remove the &lt;code&gt;feature&lt;/code&gt; or &lt;code&gt;no warnings&lt;/code&gt; or update the version, v5.24 will not issue the experimental feature warnings, or even warnings that this feature is no longer experimental:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# v5.24 won&#39;t warn about any of these
use v5.22;
use feature qw(postderef);
no warnings qw(experimental::postderef);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To review the alternatives to this new feature, see the examples I provided in &lt;a href=&quot;/2014/09/use-postfix-dereferencing/&quot;&gt;Use postfix dereferencing&lt;/a&gt;.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>No more -no_match_vars</title>
		<link>https://www.effectiveperlprogramming.com/2016/03/no-more-no_match_vars/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/03/no-more-no_match_vars/</guid>
		<pubDate>Tue, 22 Mar 2016 05:20:27 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/English&quot;&gt;English&lt;/a&gt; module translates Perl&#39;s cryptic variable names to English equivalents. For instance, &lt;code&gt;$_&lt;/code&gt; becomes &lt;code&gt;$ARG&lt;/code&gt;. This means that the match variable &lt;code&gt;$&amp;&lt;/code&gt; becomes &lt;code&gt;$MATCH&lt;/code&gt;. This also means that using the &lt;a href=&quot;https://metacpan.org/pod/English&quot;&gt;English&lt;/a&gt; module triggered the performance issue associated with the match variables &lt;code&gt;$`&lt;/code&gt;, &lt;code&gt;$&amp;&lt;/code&gt;, and &lt;code&gt;$&#39;&lt;/code&gt; even if you didn&#39;t use those variables yourself—the module used them for you. The &lt;a href=&quot;https://metacpan.org/pod/Devel::NYTProf&quot;&gt;Devel::NYTProf&lt;/a&gt; profiler had a &lt;code&gt;sawampersand&lt;/code&gt; feature to tell you one of those variables appeared in the code. We covered this in &lt;span class=&quot;item&quot;&gt;Item 33. Watch out for the match variables&lt;/span&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/English&quot;&gt;English&lt;/a&gt; module uses typeglob assignments to do its work (something I cover in &lt;a href=&quot;https://www.masteringperl.org&quot;&gt;Mastering Perl&lt;/a&gt;). The code looks like:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# The ground of all being.

	*ARG                        = *_	;

# Matching.

	*LAST_PAREN_MATCH           = *+	;
	*LAST_SUBMATCH_RESULT       = *^N ;
	*LAST_MATCH_START           = *-{ARRAY} ;
	*LAST_MATCH_END             = *+{ARRAY} ;
&lt;/code&gt;&lt;/pre&gt; 


&lt;p&gt;
The module skips those match variables if you specify &lt;code&gt;-no_match_vars&lt;/code&gt; as part of the import list:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use English qw(-no_match_vars);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl v5.17.7 re-engineered internal strings with a new copy-on-write system. Since it doesn&#39;t have to copy a string into &lt;code&gt;$&amp;&lt;/code&gt; every time it makes a match, you don&#39;t get the performance penalty. This also means that you don&#39;t need to pass around strings by reference just to prevent a copy.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.24 new features</title>
		<link>https://www.effectiveperlprogramming.com/2016/01/perl-v5-24-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2016/01/perl-v5-24-new-features/</guid>
		<pubDate>Mon, 25 Jan 2016 21:11:16 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-24</category>
		<description>&lt;p&gt;
Perl v5.24 may not look like it&#39;s packed full of exciting features (indeed, it removes some of them) but it has lots of improvements under the hood. Here are some of the user-visible features you might like.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2016/06/perl-v5-24-adds-a-line-break-word-boundary/&quot;&gt;The &lt;code&gt;\b{lb}&lt;/code&gt; is a zero-width assertion for a location where it&#39;s suitable to break a line&lt;/a&gt;. This steals from the &lt;a href=&quot;https://metacpan.org/pod/Unicode::LineBreak&quot;&gt;Unicode::LineBreak&lt;/a&gt; module. This adds to the other special &lt;code&gt;\b{...}&lt;/code&gt; sequences. See &lt;a href=&quot;https://perldoc.perl.org/perlrebackslash&quot;&gt;perlrebackslash&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;bitwise operations on wide strings are disallowed, but also see the &lt;code&gt;bitwise&lt;/code&gt; feature.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/&quot;&gt;Lexical &lt;code&gt;$_&lt;/code&gt; is gone, as is &lt;code&gt;autoderef&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2016/04/postfix-dereferencing-is-stable-is-v5-24/&quot;&gt;Postfix dereferencing is stable.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;printf&lt;/code&gt; and &lt;code&gt;sprintf&lt;/code&gt; allowed re-ordered precision arguments.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sysread&lt;/code&gt;, &lt;code&gt;syswrite&lt;/code&gt;, &lt;code&gt;recv&lt;/code&gt;, and &lt;code&gt;send&lt;/code&gt; are deprecated for &lt;code&gt;:utf8&lt;/code&gt; handles&lt;/li&gt;
&lt;li&gt;There&#39;s some faster math&lt;/li&gt;
&lt;li&gt;Subroutine calls are faster&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
You can read through the &lt;i&gt;perldelta&lt;/i&gt; documentation. 
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Apple recommends installing your own perl</title>
		<link>https://www.effectiveperlprogramming.com/2015/11/apple-recommends-installing-your-own-perl/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/11/apple-recommends-installing-your-own-perl/</guid>
		<pubDate>Tue, 10 Nov 2015 12:45:14 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>miscellany</category>
		<description>&lt;p&gt;
Apple recommends installing your own perl (or python or ruby) for your private development to not interfere with the work the bundled perl (or python or ruby) does. In &lt;span class=&quot;item&quot;&gt;Item 110. Compile and install your own perls.&lt;/span&gt; we recommended the same thing. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
In their &lt;span class=&quot;book-title&quot;&gt;System Integrity Protection Guide&lt;/span&gt; under &quot;File System Protections&quot;, they recommend:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
&lt;p&gt;
Developers using Perl, Python, Ruby, or any other scripting languages that ship with OS X, are encouraged to manage their own installations of the language and dependencies in /usr/local/. When distributing programs written with a scripting language, developers are encouraged to bundle the language runtime and any required components into a self-contained binary.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
They also note that you shouldn&#39;t be mucking around under &lt;span class=&quot;filename&quot;&gt;/usr&lt;/span&gt; unless you are playing in &lt;span class=&quot;filename&quot;&gt;/usr/local&lt;/span&gt;. That&#39;s not really news to anyone though, and many people prefer to completely stay out of &lt;span class=&quot;filename&quot;&gt;/usr&lt;/span&gt;.
&lt;/p&gt;



&lt;p&gt;
On my development machines, I install several perls and make links to all of them in my personal &lt;span class=&quot;filename&quot;&gt;~/bin&lt;/span&gt; directory as I described in &lt;a href=&quot;/2010/03/make-links-to-per-version-tools/&quot;&gt;Make links to per-version tools&lt;/a&gt;. Some people use &lt;a href=&quot;https://perlbrew.pl&quot;&gt;perlbrew&lt;/a&gt; or &lt;a href=&quot;https://github.com/tokuhirom/plenv&quot;&gt;plenv&lt;/a&gt; to do the same sort of thing, although those tools install everything in your personal directory by default. &lt;span class=&quot;person&quot;&gt;David Golden&lt;/span&gt; writes about this setup in &lt;a href=&quot;https://xdg.me/setting-up-a-perl-development-environment-with-plenv/&quot;&gt;Setting up a Perl Development Environment with plenv&lt;/a&gt;.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Use subroutine signatures with anonymous subroutines</title>
		<link>https://www.effectiveperlprogramming.com/2015/10/use-subroutine-signatures-with-anonymous-subroutines/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/10/use-subroutine-signatures-with-anonymous-subroutines/</guid>
		<pubDate>Tue, 06 Oct 2015 18:22:17 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>subroutines</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Perl 6 changed its name to Raku. We&#39;ve tried to update it everywhere, but we may have missed some.
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
While working on my &lt;a href=&quot;/2015/10/doing-less-work-to-compute-excellent-numbers/&quot;&gt;excellent number project&lt;/a&gt;, I created a subroutine that took a callback as an argument. When I dereferenced the callback I wanted to supply arguments. Since I was using Perl v5.22, I tried using a subroutine signature with it. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $max_a = bisect(
	$k+1,
	sub ( $a, $k ) { sqrt( $a * 10**$k + $a**2 ) },
	my $threshold = 1
	);
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
I had no reason to think that it wouldn&#39;t work but the idea hadn&#39;t occurred to me when I wrote about &lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt;subroutine signatures&lt;/a&gt; as a new feature in v5.20. The section on &quot;Signatures&quot; in &lt;a href=&quot;https://perldoc.perl.org/perlsub#Signatures&quot;&gt;perlsub&lt;/a&gt; doesn&#39;t have an example with an anonymous subroutine. And it works. But, maybe it&#39;s not supposed to. This is an experimental feature and it is undocumented, but I hope it sticks around.
&lt;/p&gt;



&lt;p&gt;
Consider what I would have to write without the signatures. I could assign the arguments myself:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub { my( $a, $k ) = @_; sqrt( $a * 10**$k + $a**2 ) }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is different than the signatures, though. I don&#39;t check the number of arguments, possibly set defaults, or do many other niggling tasks I don&#39;t like re-typing.
&lt;/p&gt;



&lt;p&gt;
I could also use the elements in &lt;code&gt;@_&lt;/code&gt; directly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub { sqrt( $_[0] * 10**$_[1] + $_[0]**2 ) }
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
That is ugly in a way that even a staunch Perler will admit, and I know that single-element access to Perl special arrays tends to throw off newbies. I&#39;m guilty of this because I haven&#39;t had a prettier alternative that I&#39;ve seen in other languages:
&lt;/p&gt;



&lt;pre class=&quot;python-code-listing&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;#python
import math
lambda a, k: math.sqrt( a * 10**k + a**2 )&lt;/code&gt;&lt;/pre&gt;


&lt;pre class=&quot;ruby-code-listing&quot;&gt;&lt;code class=&quot;language-ruby&quot;&gt;# ruby
lambda { |a, k| Math.sqrt(a * 10**k + a**2) }&lt;/code&gt;&lt;/pre&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;# smalltalk
[ :a :k | (a * (10 raisedTo: k) + a squared) sqrt ]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Raku (the new name for Perl 6) has the &lt;a href=&quot;https://docs.raku.org/language/functions#index-entry-pointy_blocks&quot;&gt;pointy block&lt;/a&gt; syntax along with other cool ways to create fancy subroutines:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;# perl6
-&gt; $a, $k { sqrt($a * 10**$k + $a**2) };
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now that I have this syntax in Perl, even if it is experimental, I know that I&#39;m going to abuse it and come to rely on it.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Use /aa to get ASCII semantics in regexes, for reals this time</title>
		<link>https://www.effectiveperlprogramming.com/2015/08/use-aa-to-get-ascii-semantics-in-regexes-for-reals-this-time/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/08/use-aa-to-get-ascii-semantics-in-regexes-for-reals-this-time/</guid>
		<pubDate>Thu, 20 Aug 2015 15:03:58 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-14</category>
		<description>&lt;p&gt;
When Perl made regexes more Unicode aware, starting in v5.6, some of the character class definitions and match modifiers changed. What you expected to match &lt;code&gt;\d&lt;/code&gt;, &lt;code&gt;\s&lt;/code&gt;, or &lt;code&gt;\w&lt;/code&gt; are more expansive now (&lt;a href=&quot;/2011/01/know-your-character-classes/&quot;&gt;Know your character classes under different semantics&lt;/a&gt;). Most of us probably didn&#39;t notice because the range of our inputs is limited.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
To get the ASCII semantics back, you can use v5.14&#39;s &lt;code&gt;/a&lt;/code&gt; match flag to restore their pre-v5.6 meanings. I wrote about that in &lt;a href=&quot;/2014/12/enforce-ascii-semantics-when-you-only-want-ascii/&quot;&gt;Enforce ASCII semantics when you only want ASCII&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
That&#39;s not all, though. There&#39;s a problem with case-insensitive matches. Some of the &quot;wide&quot; characters lowercase into the ASCII range. Well, so far there&#39;s exactly one, but that doesn&#39;t mean there might be more later. The Kelvin symbol, K (U+212A), lowercases into &lt;i&gt;k&lt;/i&gt; (U+006B). To avoid problems with fonts and indistinguishable characters, I create the characters with the &lt;code&gt;\x{}&lt;/code&gt; sequences:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;

my $lower_k = &quot;\x{006B}&quot;;

if( $lower_k =~ /\x{212A}/ ) {
	say &quot;Matches without case insensitivity&quot;;
	}

if( $lower_k =~ /\x{212A}/i ) {
	say &quot;Matches with case insensitivity&quot;;
	}

if( $lower_k =~ /\x{212A}/ia ) {
	say &quot;Matches with case insensitivity, /a&quot;;
	}

if( $lower_k =~ /\x{212A}/iaa ) {
	say &quot;Matches with case insensitivity, /aa&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;
 

&lt;p&gt;
The Kelvin sign matches with case insensitivity, even with the &lt;code&gt;/a&lt;/code&gt; flag. However, it doesn&#39;t match when you double up with &lt;code&gt;/aa&lt;/code&gt;. That extra &lt;code&gt;/a&lt;/code&gt; means, &quot;I mean ASCII, j/k, but seriously.&quot;
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Matches with case insensitivity
Matches with case insensitivity, /a
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Unless you want to handle that Kelvin symbol specially, you should be safe always doubling up on the &lt;code&gt;/a&lt;/code&gt;.
&lt;/p&gt;



&lt;p&gt;
Read &lt;a href=&quot;https://perldoc.perl.org/perlre&quot;&gt;perlre&lt;/a&gt; for the rest of the story.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Turn capture groups into cluster groups</title>
		<link>https://www.effectiveperlprogramming.com/2015/08/turn-capture-groups-into-cluster-groups/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/08/turn-capture-groups-into-cluster-groups/</guid>
		<pubDate>Wed, 19 Aug 2015 18:53:43 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>regular-expressions</category>
		<description>&lt;p&gt;
Perl v5.22 adds the &lt;code&gt;/n&lt;/code&gt; regex flag that turns all parentheses groups in its scope into non-capturing groups. This can be handy when you want to capture almost nothing but still need many cluster parts. You do less typing to get that.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
(The language for parentheses groups changed in Perl 5.004 to &lt;a href=&quot;https://www.learning-perl.com/2011/01/captures-versus-memories/&quot;&gt;&quot;capturing&quot; and &quot;clustering&quot;&lt;/a&gt;. The captures set the match variables. The clusters merely group parts of the pattern. &lt;a href=&quot;https://www.learning-perl.com/2011/01/captures-versus-memories-group-versus-buffer/&quot;&gt;Sometimes the docs said &quot;buffers&quot; and sometimes &quot;groups&quot;&lt;/a&gt;.)
&lt;/p&gt;



&lt;p&gt;
Perl already has non-capturing parentheses with the &lt;code&gt;(?:PATTERN)&lt;/code&gt; syntax available in all versions of Perl 5. If you have several of those clusters but no captures, you might be annoyed while typing all those &lt;code&gt;?:&lt;/code&gt; characters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;/(?:abc)+(?:def){2}(?:xyz)+/
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You could leave off the &lt;code&gt;?:&lt;/code&gt; and let those capture, but that&#39;s a bit slower since Perl has to move data around and assign values.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;/(abc)+(def){2}(xyz)+/  # slower
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With the &lt;code&gt;/n&lt;/code&gt;, these are equivalent:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;/(?:abc)+(?:def){2}(?:xyz)+/
/(abc)+(def){2}(xyz)+/n
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can apply &lt;code&gt;/n&lt;/code&gt; to parts of a pattern just as you can other pattern operators (see &lt;a href=&quot;/2010/04/know-the-difference-between-regex-and-match-operator-flags/&quot;&gt;Know the difference between regex and match operator flags&lt;/a&gt;). The &lt;code&gt;(?&lt;i&gt;flags&lt;/i&gt;:SUBPATTERN)&lt;/code&gt; works on the subpattern:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;

my $string = &quot;abc1234defdefxyzABCDEf&quot;;

$string =~ /
	abc 
	(\w{3,5}) # capture this in $1 

	(?n:    # only clusters in here
		(abc)+|(def){2}|(hij)+
	)

	xyz
	(\w{3,5}) # capture this in $2 
	/x;

say &quot;\$1: $1\n\$2: $2&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s a capture before the middle and another capture after. The bare parentheses in the middle (the ones in the alternation) don&#39;t count for the numbering:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$1: 1234
$2: ABCDE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can interpolate a pre-compiled pattern into the larger pattern and it still works (but recall that &lt;a href=&quot;https://www.intermediateperl.com/2015/04/pre-compiled-patterns-retain-their-settings-if-they-are-interpolated/&quot;&gt;precompiled patterns retain their settings and don&#39;t change the larger pattern&#39;s settings&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;

my $string = &quot;abc1234defdefxyzABCDEf&quot;;

my $subpattern = qr/(abc)+|(def){2}|(hij)+/n;

$string =~ /
	abc 
	(\w{3,5}) # capture this in $1 

	$subpattern

	xyz
	(\w{3,5}) # capture this in $2 
	/x;

say &quot;\$1: $1\n\$2: $2&quot;;
&lt;/code&gt;&lt;/pre&gt; 


&lt;p&gt;
There&#39;s a slight problem, though. You can&#39;t count on that subpattern not capturing anything. The &lt;code&gt;/n&lt;/code&gt; turns off the capturing parentheses but doesn&#39;t turn off named captures (&lt;span class=&quot;item&quot;&gt;Item 31: Use named captures to label matches&lt;/span&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;

my $string = &quot;abc1234defdefxyzABCDEf&quot;;

my $subpattern = qr/(abc)+|(?&amp;lt;middle&gt;def){2}|(hij)+/n;

$string =~ /
	abc 
	(\w{3,5}) # capture this in $1 

	$subpattern

	xyz
	(\w{3,5}) # capture this in $2 
	/x;

say &amp;lt;&amp;lt;&quot;HERE&quot;;
\$1: $1
\$2: $2
\$3: $3
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you label one of the captures as &lt;code&gt;middle&lt;/code&gt;, it also sets the numbered capture variables since the named capture hashes are really tied hashes into the numbered capture variables. You get an extra capture here:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$1: 1234
$2: def
$3: ABCDE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You see the same behavior with a non-pre-compiled interpolated pattern, although you have to set the &lt;code&gt;/n&lt;/code&gt; flag in the larger pattern:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $subpattern = &quot;(abc)+|(?&amp;lt;middle&gt;def){2}|(hij)+&quot;;
say &quot;subpattern is $subpattern&quot;;

my $pattern = qr/
	abc 
	(\w{3,5}) # capture this in $1 

	(?n:  $subpattern  )

	xyz
	(\w{3,5}) # capture this in $2 
	/x;
say &quot;pattern is $pattern&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This means you can&#39;t trust the positions of the match variables after an interpolated pattern &lt;em&gt;even if you had turned off capturing&lt;/em&gt;. If you&#39;re not interpolating a pre-compiled pattern that&#39;s not a problem. That it&#39;s a problem in some cases is a problem; Perl has enough special cases already. This is by design so a named capture still works (a poor design I think). So far, this isn&#39;t an experimental feature but I think it should be; I foresee a lot of confusion for this feature.
&lt;/p&gt;



&lt;p&gt;
There&#39;s another small issue with this feature. You can turn off the no-capture feature, but you need extra parens to do it.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;

$_ = &#39;cat dog bird&#39;;

if( /(?-n:cat) (dog) (bird)/n ) {
	say &quot;1: \$1 is &amp;lt;$1&gt;&quot;;
	}

if( /(?-n:(cat)) (dog) (bird)/n ) {
	say &quot;2: \$1 is &amp;lt;$1&gt;&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that it&#39;s the second one that works:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: $1 is &amp;lt;&gt;
2: $1 is &amp;lt;cat&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
At first blush, you might think that the &lt;code&gt;-n&lt;/code&gt; turns on capturing in the parentheses that contain it. Or you might not. But someone is going to think that and they might even ask about it on StackOverflow. However, turning off no-capture applies to the parentheses that come after it. It&#39;s a little odd, but it makes sense after you think about it a bit. Or maybe it doesn&#39;t.
&lt;/p&gt;



&lt;p&gt;
Make a more complicated situation to push the boundaries of ridiculousness. You can build up a pattern with several levels of interpolation that alternately turn on and turn off capturing:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;

my $dog    = qr/(dog)/;
my $kitty  = qr/(cat)/n;
my $cat    = qr/(?-n:($kitty))/n;
my $middle = qr/a( $cat and a)? $dog/n;

$_ = &quot;I have a cat and a dog&quot;;

if( /(?-n:I have ($middle))/n ) {
	say &amp;lt;&amp;lt;&quot;HERE&quot;;
\$1: $1
\$2: $2
\$3: $3
HERE
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You may have thought that &lt;i&gt;cat&lt;/i&gt; should not be captured, but it shows up in &lt;code&gt;$2&lt;/code&gt;: 
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$1: a cat and a dog
$2: cat
$3: dog
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s not the pattern from &lt;code&gt;$kitty&lt;/code&gt; that&#39;s causing that. It&#39;s the one in &lt;code&gt;$cat&lt;/code&gt; that has capturing turned off with the &lt;code&gt;/n&lt;/code&gt; but then turned on inside the pattern. That&#39;s not what causes the capture though; it&#39;s the extra parens inside the pattern in &lt;code&gt;$cat&lt;/code&gt; that form a new capture. It&#39;s not so easy to see what&#39;s causing what anymore, even if this is a silly example.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Create named variable aliases with ref aliasing</title>
		<link>https://www.effectiveperlprogramming.com/2015/08/create-named-variable-aliases-with-ref-aliasing/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/08/create-named-variable-aliases-with-ref-aliasing/</guid>
		<pubDate>Mon, 10 Aug 2015 21:27:38 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;p&gt;
Perl v5.22&#39;s experimental &lt;code&gt;refaliasing&lt;/code&gt; feature adds the ability to alias a named variable to another named variable, or alias a named variable to a reference. This doesn&#39;t copy the data; you end up with another named variable for the same data. As with all such features, the details and syntax may change or the feature may be removed altogether (according to &lt;a href=&quot;https://perldoc.perl.org/perlpolicy&quot;&gt;perlpolicy&lt;/a&gt;).
&lt;/p&gt;

&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Also see v5.26&#39;s &lt;a href=&quot;/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/&quot;&gt;&lt;code&gt;declared_refs&lt;/code&gt; experimental feature&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;

&lt;!--more--&gt;

&lt;p&gt;
Remember that variables are labels for data somewhere (and in Perl we aren&#39;t supposed to care about that somewhere). We use the variable name in our code and Perl handles the details of finding and using the right data. This experimental feature allows you to add a new label that&#39;s a named variable.
&lt;/p&gt;



&lt;p&gt;
Review what you can already do. Take a reference to the named variable:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @animals = qw(Buster Mimi Addy);
my $animals_ref = \@animals
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This isn&#39;t a copy. It&#39;s the same data with two different references. The &lt;a href=&quot;https://metacpan.org/pod/Devel::Peek&quot;&gt;Devel::Peek&lt;/a&gt; module can expose some of these details. Remember that the stringified reference has a (mostly useless to you) memory address:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;

my @animals = qw(Buster Mimi Addy);
Dump( @animals );

my $animals_ref = \@animals;
warn &quot;$animals_ref&quot;;

Dump( @animals );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows Perl&#39;s memory address for the data. In the first part of the output you see &lt;code&gt;REFCNT = 1&lt;/code&gt;. At the point of the first &lt;code&gt;Dump&lt;/code&gt; only &lt;code&gt;@animals&lt;/code&gt; connects to the data. In the middle output, you see that the reference memory address matches one of the numbers in the &lt;code&gt;Dump&lt;/code&gt; output (and that&#39;s all you&#39;ll read about that here). In the last part of the output you see that there&#39;s another reference to the same data (&lt;code&gt;REFCNT = 2&lt;/code&gt;):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PVAV(0x7fc78d803f48) at 0x7fc78d81b390
  REFCNT = 1
  FLAGS = ()
  ARRAY = 0x7fc78cd00f20
--------
ARRAY(0x7fc78d81b390) at /Users/brian/Desktop/test3.pl line 16.
--------
SV = PVAV(0x7fc78d803f48) at 0x7fc78d81b390
  REFCNT = 2
  FLAGS = ()
  ARRAY = 0x7fc78cd00f20
...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can skip the named variable to create the reference directly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $animals_ref = [ qw(Buster Mimi Addy) ];
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Prior to v5.22, there wasn&#39;t a built-in way to go from that reference to a named variable. You could get the same data in the named variable but it would be different data. The &lt;a href=&quot;https://metacpan.org/pod/Data::Alias&quot;&gt;Data::Alias&lt;/a&gt; and &lt;a href=&quot;https://metacpan.org/pod/Lexical::Alias&quot;&gt;Lexical::Alias&lt;/a&gt; modules could do it, but then you have another dependency.
&lt;/p&gt;


&lt;code&gt;refaliasing&lt;/code&gt; lets you go from one named variable to another so that both are labels for the same data. Changing one &quot;changes&quot; the other because they are the same data:

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use feature qw(refaliasing);
no warnings qw(experimental::refaliasing);

my @animals = qw(Buster Mimi Addy);
\my @pets = \@animals;
$pets[0] = &#39;Ginger&#39;;
say $animals[0];   # Ginger
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;my&lt;/code&gt; isn&#39;t magical here. You can still alias the named variables after you declare them:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use feature qw(refaliasing);
no warnings qw(experimental::refaliasing);

my @animals = qw(Buster Mimi Addy);
my @pets;
\@pets = \@animals;
$pets[0] = &#39;Ginger&#39;;
say $animals[0];   # Ginger
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
The most compelling case may be iterating through a list of references. The control variable must be a scalar. A &lt;code&gt;foreach&lt;/code&gt; could alias a reference value to it but you still have to dereference it. This is handy when you want to reduce dereferencing inside your block:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use feature qw(refaliasing);
no warnings qw(experimental::refaliasing);

my @hash_refs = (
	{ name =&gt; &#39;Buster&#39;, type =&gt; &#39;cat&#39; },
	{ name =&gt; &#39;Mimi&#39;, type =&gt; &#39;cat&#39; },
	{ name =&gt; &#39;Addy&#39;, type =&gt; &#39;dog&#39; },
	);

foreach \my %hash ( @hash_refs ) {
	say $hash{&#39;name&#39;};    # otherwise it&#39;s $hash-&gt;{&#39;name&#39;}
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows each animal&#39;s name:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster
Mimi
Addy
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Looking ahead to signatures&lt;/h2&gt;


&lt;p&gt;
Sometime in the future you can imagine this being part of the (also experimental) &lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt; subroutine signatures&lt;/a&gt; to pass data structure references into named parameters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;some_task( \@array, \%hash );

sub some_task ( \@a, \%h ) { ... }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can still get this with conventional argument handling by using the reference operator before the declaration (the &lt;code&gt;my&lt;/code&gt;). Remember that you aren&#39;t taking a reference to a list. Instead, the reference operator (&lt;code&gt;\&lt;/code&gt;) distributes through the list:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use feature qw(refaliasing);
no warnings qw(experimental::refaliasing);

use Data::Dumper;

my @names = qw( Ginger Addy Buster );
my %types  = (
	&#39;Ginger&#39; =&gt; &#39;cat&#39;,
	&#39;Addy&#39;   =&gt; &#39;dog&#39;,
	);
some_task( \@names, \%types );

sub some_task {
	\my( @a, %h ) = @_;    # \ distributes, not a single ref
	say Dumper( \@a, \%h );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Don&#39;t get too excited: these are experimental features. Fingers crossed though!
&lt;/p&gt;


&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This was updated in v5.26&#39;s &lt;a href=&quot;/2018/09/mix-assignment-and-reference-aliasing-with-declared_refs/&quot;&gt;&lt;code&gt;declared_refs&lt;/code&gt; experimental feature&lt;/a&gt; that allows you to declare the references singly:
&lt;/p&gt;
&lt;/div&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.26;
use feature qw(refaliasing declared_refs);
no warnings qw(experimental::refaliasing experimental::declared_refs);
sub some_task {
	my( \@a, \%h ) = @_;    # \ distributes, not a single ref
	say Dumper( \@a, \%h );
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;refaliasing&lt;/code&gt; feature is experimental, so it might change or disappear&lt;/li&gt;
&lt;li&gt;You can alias a reference to a named variable&lt;/li&gt;
&lt;li&gt;You can alias a named variable to another named variable.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Make bitwise operators always use numeric context</title>
		<link>https://www.effectiveperlprogramming.com/2015/07/make-bitwise-operators-always-use-numeric-context/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/07/make-bitwise-operators-always-use-numeric-context/</guid>
		<pubDate>Fri, 31 Jul 2015 05:59:59 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This feature is no longer experimental, starting in v5.28. Declaring &lt;code&gt;use v5.28&lt;/code&gt; automatically enables it.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Most Perl operators force their context on the values. For example, the numeric addition operator, &lt;code&gt;+&lt;/code&gt;, forces its values to be numbers. To &quot;add&quot; strings, you use a separate operator, the string concatenation operator, &lt;code&gt;.&lt;/code&gt; (which looks odd at the end of a sentence).
&lt;/p&gt;



&lt;p&gt;
The bitwise operators, however, look at the values to determine their context. If either value has a numeric component, the bitwise operators do numeric things. If both values are strings, the &lt;a href=&quot;https://perldoc.perl.org/perlop#Bitwise-String-Operators&quot;&gt;bit operators become string operators&lt;/a&gt;. That&#39;s certainly one of Perl&#39;s warts, which I&#39;ll fix at the end of this article with a &lt;a href=&quot;/2015/05/perl-5-22-new-features/&quot;&gt;new feature from v5.22&lt;/a&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Consider this demonstration where the operator is the same but the type of argument changes:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $number  = 0x80 ^ 0x20;
my $mixed   = &quot;P&quot;  ^ 0x20;
my $string  = &quot;P&quot; ^ &quot; &quot;;

print &amp;lt;&amp;lt;&quot;HERE&quot;;
Number is $number
Mixed  is $mixed
String is $string
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The result is different in each case:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Number is 160
Mixed  is 32
String is p
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This leads to the joke about the difference between Perl and perl being one bit:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -e &#39;printf &quot;[%b]\n&quot;, ord(&quot;perl&quot; ^ &quot;Perl&quot;)&#39;
[100000]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is a problem if you want a particular context. You have to force either the string or numeric context. If either of the operands has a numeric component (see &lt;a href=&quot;/2011/12/create-your-own-dualvars/&quot;&gt;Create your own dualvars&lt;/a&gt;) it&#39;s a numeric operation:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;&quot;$var&quot; ^ &quot;$var2&quot;
$var + 0 ^ $var2
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Or, you can skip all this messiness with a new experimental feature from v5.22. The &lt;code&gt;bitwise&lt;/code&gt; feature makes the bitwise operators always work in numeric context. You have to import this one explicitly because it&#39;s not automatically imported as part of the default new feature set:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use feature qw(bitwise);
no warnings qw(experimental::bitwise);

my $number = 0x80 ^ 0x20;
my $mixed  = &quot;P&quot;  ^ 0x20;
my $string = &quot;P&quot; ^ &quot; &quot;;

print &amp;lt;&amp;lt;&quot;HERE&quot;;
Number is $number
Mixed  is $mixed
String is $string
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the output for the last case is &lt;code&gt;0&lt;/code&gt; instead of &lt;code&gt;p&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Number is 160
Mixed  is 32
String is 0
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is probably what you want most of the time. And, if you don&#39;t want this, do you really want a bitwise operator?
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.22 adds hexadecimal floating point literals</title>
		<link>https://www.effectiveperlprogramming.com/2015/06/perl-v5-22-adds-hexadecimal-floating-point-literals/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/06/perl-v5-22-adds-hexadecimal-floating-point-literals/</guid>
		<pubDate>Wed, 17 Jun 2015 00:24:22 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;p&gt;
You can specify literal hexadecimal floating-point numbers in v5.22, just as you can in C99, Java, Ruby, and other languages. Perl, which uses doubles to store floating-point numbers, can represent a limited set of values. Up to now, you&#39;ve had to specify those floating-point numbers in decimal, hoping that a double could exactly represent that number. That hope, sometimes unfounded, is the basis for the common &lt;a href=&quot;https://perldoc.perl.org/perlfaq4#Why-am-I-getting-long-decimals-(eg%2c-19.9499999999999)-instead-of-the-numbers-I-should-be-getting-(eg%2c-19.95)%3f&quot;&gt;newbie question about floating-point errors&lt;/a&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Before you get into the details, here&#39;s the new feature. You can write a hexadecimal floating-point number with hex digits. You specify the power of two exponent with &lt;code&gt;p&lt;/code&gt;, which must be present:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $number = 0x1.aaaaaaaaaaaabp+1;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This allows you to represent a floating-point number exactly because this representation maps almost directly onto the actual storage. If you can type it as a literal, you represent that number exactly to the limit of its precision. Exact numbers have no rounding errors.
&lt;/p&gt;



&lt;p&gt;
Don&#39;t get too excited though. Now you have exact values, but you still represent the same number of them that you already had. The decimal numbers that you couldn&#39;t represent exactly are still inexact. It&#39;s the other side of the coin. More on that in a moment.
&lt;/p&gt;



&lt;p&gt;
You print numbers as hexadecimal floating-point numbers in the same form with the new &lt;code&gt;%a&lt;/code&gt; specifier for &lt;a href=&quot;https://perldoc.perl.org/functions/sprintf&quot;&gt;sprintf&lt;/a&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;printf &#39;%a&#39;, $number;  # 0x1.aaaaaaaaaaaabp+1 again
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
I write more about the actual storage in &lt;a href=&quot;https://www.intermediateperl.com/2015/06/using-inlinec-to-look-at-a-numbers-double-representation/&quot;&gt;Using Inline::C to look at a number’s double representation&lt;/a&gt;.
&lt;/p&gt;


&lt;h2&gt;Some snags&lt;/h2&gt;


&lt;p&gt;
But, as with many new features, there&#39;s a catch. Although you can specify a hexadecimal floating-point literal, &lt;a href=&quot;https://perldoc.perl.org/functions/oct&quot;&gt;&lt;code&gt;oct&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://perldoc.perl.org/functions/hex&quot;&gt;&lt;code&gt;hex&lt;/code&gt;&lt;/a&gt; don&#39;t understand them (and probably never will):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $number = hex( &#39;0x1f.0p3&#39; );
print &quot;number is $number&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The number conversion gets as far as the decimal point, which it doesn&#39;t think belongs there (as documented):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;number is 31
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/Scalar::Util&quot;&gt;Scalar::Util&lt;/a&gt; module is similarly afflicted (not so clearly documented):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Scalar::Util qw(looks_like_number);

print &#39;Does not look like a number&#39; 
	unless looks_like_number(&quot;0x1f.0p3&quot;);
&lt;/code&gt;&lt;/pre&gt;	


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Does not look like a number
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This happens because there are two places where &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; parses numbers. There&#39;s &lt;span class=&quot;filename&quot;&gt;toke.c&lt;/span&gt;, which is the lexer that decides how &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; parses the source. That handles the literals. Then there&#39;s &lt;span class=&quot;filename&quot;&gt;numeric.c&lt;/span&gt;&#39;s &lt;code&gt;grok_number&lt;/code&gt; that decides how to turn strings into numbers. That code doesn&#39;t support hexadecimal floats or some other notations. &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; only claims to handle hexadecimal floating-point &lt;i&gt;literals&lt;/i&gt;.
&lt;/p&gt;



&lt;p&gt;
But, &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; doesn&#39;t claim to handle floating-points with &lt;a href=&quot;https://perldoc.perl.org/functions/oct&quot;&gt;&lt;code&gt;oct&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;https://perldoc.perl.org/functions/hex&quot;&gt;&lt;code&gt;hex&lt;/code&gt;&lt;/a&gt;, both of which clearly state that they only handle non-negative integers. The &lt;code&gt;looks_like_number&lt;/code&gt; case is tougher since it handles exponential notation but not hexadecimal notation. It would be nice if everything that interpreted numbers supported the same formats. This has pinched me a few times, but that&#39;s my fault because &lt;code class=&quot;binary perl&quot;&gt;perl&lt;/code&gt; does act as documented.
&lt;/p&gt;



&lt;p&gt;
To convert a string in the hexadecimal floating-point notation to a number, you can use a string &lt;a href=&quot;https://perldoc.perl.org/functions/eval&quot;&gt;&lt;code&gt;eval&lt;/code&gt;&lt;/a&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $number = eval( &#39;0x1f.0p3&#39; );
print &quot;number is $number&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now you get the right number:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;number is 248
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Or maybe you&#39;d feel better using &lt;a href=&quot;https://metacpan.org/pod/Safe&quot;&gt;Safe&lt;/a&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.22;
use Safe;

# why these ops? No idea, but they all have to be there
my @ops = qw( lineseq leaveeval const padsv padany rv2gv );

my $compartment = Safe-&gt;new;
$compartment-&gt;permit_only( @ops );
my $result = $compartment-&gt;reval( &#39;0x1fp3&#39; );

say &quot;result is $result&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But back to the reason you&#39;d do this.
&lt;/p&gt;


&lt;h2&gt;Rounding&lt;/h2&gt;


&lt;p&gt;
You can&#39;t represent all real numbers as a double; there&#39;s a granularity that some of the numbers fall between. When you represent fractional numbers in decimal, they end up as bits that represent a power of two. For numbers without that many decimal places, it doesn&#39;t matter. This code looks like it works fine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $e = 0.1;
my $n = 0;

foreach ( 1 .. 10 ) {
	$n += $e;
	printf &quot;%f\n&quot;, $n;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output looks like it correctly adds &lt;code&gt;0.1&lt;/code&gt; and ends up with exactly &lt;code&gt;1&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0.100000
0.200000
0.300000
0.400000
0.500000
0.600000
0.700000
0.800000
0.900000
1.000000
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can show more decimal places:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $e = 0.1;
my $n = 0;

foreach ( 1 .. 10 ) {
	$n += $e;
	printf &quot;%.17f\n&quot;, $n;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You see a bit of fuzziness at the end, although not enough to cause problems unless you&#39;re using that many digits:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0.10000000000000001
0.20000000000000001
0.30000000000000004
0.40000000000000002
0.50000000000000000
0.59999999999999998
0.69999999999999996
0.79999999999999993
0.89999999999999991
0.99999999999999989
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Do this addition enough times and it might matter:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $e = 1/10;
my $n = 0;

foreach ( 1 .. 100_000_000 ) {
	$n += $e;
	printf &quot;%.17f\n&quot;, $n unless $_ % 10_000_000;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That seemingly insignificant error moves up the decimal places:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;999999.99983897537458688
2000000.00071374792605639
3000000.00164507050067186
4000000.00257639307528734
4999999.99975590128451586
5999999.99603061098605394
6999999.99230532068759203
7999999.98858003038913012
8999999.98485474102199078
9999999.98112945072352886
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If &lt;code&gt;$e&lt;/code&gt; was a number that could be represented exactly as a power of two in a finite number of bits, you don&#39;t see this problem:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $e = 1/8; # or 0.125
my $n = 0;

foreach ( 1 .. 100_000_000 ) {
	$n += $e;
	printf &quot;%.17f\n&quot;, $n unless $_ % 10_000_000;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since &lt;code&gt;1/8&lt;/code&gt; is a power of two, you can represent it exactly as a power of two with no round off error even after millions of additions:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1250000.00000000000000000
2500000.00000000000000000
3750000.00000000000000000
5000000.00000000000000000
6250000.00000000000000000
7500000.00000000000000000
8750000.00000000000000000
10000000.00000000000000000
11250000.00000000000000000
12500000.00000000000000000
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Instead of a division of a decimal number or a decimal literal, v5.22 now allows you to write that out as &lt;code&gt;0x0.2p0&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $e = 0x0.2p0;
my $n = 0;

foreach ( 1 .. 100_000_000 ) {
	$n += $e;
	printf &quot;%.17f\n&quot;, $n unless $_ % 10_000_000;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This isn&#39;t something new since you could represent that number as a decimal, but in some cases it may be more convenient to specify it in hex. If you have an example of that convenience, let me know!
&lt;/p&gt;



&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;v5.22 supports hexadecimal floating-point literals (but not strings). The literal starts with &lt;code&gt;0x&lt;/code&gt; and ends with &lt;code&gt;p&lt;i&gt;EXP&lt;/i&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Hexadecimal floating-point literals specify exact numbers that won&#39;t have round-off errors.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use Perl 5.22&#39;s &lt;&lt;&gt;&gt; operator for safe command-line handling</title>
		<link>https://www.effectiveperlprogramming.com/2015/05/use-perl-5-22s-operator-for-safe-command-line-handling/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/05/use-perl-5-22s-operator-for-safe-command-line-handling/</guid>
		<pubDate>Mon, 25 May 2015 00:42:02 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;p&gt;
We&#39;ve had the three argument &lt;code&gt;open&lt;/code&gt; since Perl 5.6. This allows you to separate the way you want to interact with the file from the filename. 
&lt;/p&gt;



&lt;p&gt;
Old Perl requires you to include the mode and filename together, giving Perl the opportunity to interpret what you mean:
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open FILE, &#39;&gt; some_file&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can write that in three arguments so Perl doesn&#39;t get to guess what the filename argument means. Now it&#39;s a literal filename and nothing else:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open FILE, &#39;&gt;&#39;, &#39;some_file&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is important when you use a variable for the filename argument.
&lt;/p&gt;


&lt;p&gt;
With two-argument open you probably expect to open a file for reading
because that&#39;s the implied mode:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open FILE, $filename;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What if, however, &lt;code&gt;$filename&lt;/code&gt; isn&#39;t just a literal filename? &lt;code&gt;open&lt;/code&gt; gets a string and acts on what&#39;s in that string. If it sees &lt;code&gt;&amp;gt;&lt;/code&gt; at the beginning or a &lt;code&gt;|&lt;/code&gt; at the end, it opens the file or the process in a particular way. This &quot;magic open&quot; feature is Perl trying to be helpful.
&lt;/p&gt;



&lt;p&gt;
I discussed this in &quot;Secure Programming Techniques&quot; in &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt;, but there&#39;s another place that has this problem. The diamond operator, &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;, automatically goes through the values in &lt;code&gt;@ARGV&lt;/code&gt; and tries to open them. If those values have the special mode characters, Perl treats them as special. This is the same as &lt;code&gt;&amp;lt;ARGV&amp;gt;&lt;/code&gt;, since that&#39;s the implied filehandle.
&lt;/p&gt;



&lt;p&gt;
Here&#39;s a short Perl program that acts like &lt;i&gt;cat -n&lt;/i&gt;, which numbers the lines:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/usr/bin/perl
# single-diamond.pl

print &quot;$. $_&quot; while( &amp;lt;&gt; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you call this with the name of a regular file, the &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; opens that file and prints its contents line by line. It can print its own source code:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl single-diamond.pl single-diamond.pl
1 #!/usr/bin/perl
2 # single-diamond.pl
3
4 print &quot;$. $_&quot; while( &amp;lt;&gt; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, if you create a filename with a pipe on the end, it uses the name for the program it should launch then redirects its output into &lt;code&gt;ARGV&lt;/code&gt;. Here it happens with the &lt;span class=&quot;binary&quot;&gt;date&lt;/span&gt; command:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl single-diamond.pl &#39;date |&#39;
1 Sat Oct  4 02:16:49 EDT 2014
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
What if you use the &lt;code&gt;&amp;gt;&lt;/code&gt; to try to open a file for writing? That mode truncates a file before you can write to it. You see that you have lines in &lt;i&gt;not_empty&lt;/i&gt; before you run the program but those lines disappear when you run the program:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ cat not_empty.txt
this is a line
here&#39;s another line
this is the last line
$ perl5.20.0 single-diamond.pl &#39;&gt; not_empty.txt&#39;
$ cat not_empty.txt
$
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is a problem. Perl provides this convenient idiom to open the files on the command line, but now it&#39;s dangerous.
&lt;/p&gt;



&lt;p&gt;
To get around this, Perl v5.22 adds a new form of the diamond operator. The double diamond &lt;code&gt;&amp;lt;&amp;lt;&amp;gt;&amp;gt;&lt;/code&gt; interprets the command-line arguments as literal filenames.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/usr/bin/perl
# double-diamond.pl
use v5.22;

print while( &amp;lt;&amp;lt;&gt;&gt; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you try to fool this program with the pipe, you get an error that the file named &lt;i&gt;date |&lt;/i&gt; does not exist:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.22.0 double-diamond.pl &#39;date |&#39;
Can&#39;t open date |: No such file or directory at double-diamond.pl line 5.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Similarly, the double diamond doesn&#39;t truncate the &lt;span class=&quot;filename&quot;&gt;not_empty.txt&lt;/span&gt; file:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ cat not_empty.txt
this is a line
here&#39;s another line
this is the last line
$ perl5.22.0 double-diamond.pl &#39;&gt; not_empty.txt&#39;
Can&#39;t open &gt; not_empty.txt: No such file or directory at double-diamond.pl line 5.
$ cat not_empty.txt
this is a line
here&#39;s another line
this is the last line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, what if you can&#39;t use v5.22? You&#39;re stuck with the dangerous single diamond. You could abandon the convenience altogether and handle it yourself:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/usr/bin/perl

FILE: while( my $file = shift @ARGV ) {
	open my $fh, &#39;&amp;lt;&#39;, $file or next FILE;
	print while( &amp;lt;$fh&gt; );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s not satisfying though, but neither is where you prepend a &lt;code&gt;&amp;lt;&lt;/code&gt; to make the mode read-only:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/usr/bin/perl
# escaped.pl
use v5.14;

@ARGV = map { &quot;&amp;lt; $_&quot; } @ARGV;

print while( &amp;lt;&gt; ); 
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That works, but it&#39;s a little bit ugly and probably has problems of its own:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl escaped.pl &#39;echo &quot;foo&quot; |&#39; &#39;&gt; not empty&#39;
Can&#39;t open &amp;lt; echo &quot;foo&quot; |: No such file or directory at escaped.pl line 7, &amp;lt;&gt; line 7.
Can&#39;t open &amp;lt; &gt; not empty: No such file or directory at escaped.pl line 7, &amp;lt;&gt; line 7.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Instead of trying a workaround, perhaps you can use this extremely dangerous and perverse security issue to convince your bosses to upgrade Perl.
&lt;/p&gt;


&lt;h2&gt;Further reading&lt;/h2&gt;


&lt;p&gt;
I&#39;ve written about these issues at &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt; too.
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.masteringperl.org/2014/10/the-double-diamond-a-more-secure/&quot;&gt;The double diamond, a more secure &amp;lt;&amp;gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.masteringperl.org/2014/10/more-fun-with-the-diamond-operator/&quot;&gt;More fun with the diamond operator&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The filename in the two argument &lt;code&gt;open&lt;/code&gt; can contain the file open mode.&lt;/li&gt;
&lt;li&gt;The diamond operator might use &quot;magic open&quot;&lt;/li&gt;
&lt;li&gt;The double-diamond operator interprets the command-line arguments as literal filenames&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl 5.22 new features</title>
		<link>https://www.effectiveperlprogramming.com/2015/05/perl-5-22-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/05/perl-5-22-new-features/</guid>
		<pubDate>Wed, 20 May 2015 20:46:53 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-22</category>
		<description>&lt;p&gt;
The first Perl 5.22 release candidate is out and there are some new operators and many enhancements to regular expressions that look interesting, along with some improvements that don’t require any work from you. Some of the features are experimental, so be careful that you don’t create problems by overusing them until they settle down.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You can download the &lt;a href=&quot;https://www.cpan.org/src/README.html&quot;&gt;Perl source from CPAN&lt;/a&gt;. For Windows, &lt;a href=&quot;https://strawberryperl.com&quot;&gt;Strawberry Perl&lt;/a&gt; 5.22 should be available once there&#39;s an official release.
&lt;/p&gt;


&lt;h2&gt;New features&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Perl knows what defined methods a class has and can call them faster&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/08/create-named-variable-aliases-with-ref-aliasing/&quot;&gt;Create aliases through references on both sides of an assignment or in a control variable&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/07/make-bitwise-operators-always-use-numeric-context/&quot;&gt;The new bitwise feature always uses numeric context&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/05/use-perl-5-22s-operator-for-safe-command-line-handling&quot;&gt;The double-diamond line-input operator&lt;/a&gt;, &lt;code&gt;&amp;lt;&amp;lt;&amp;gt;&amp;gt;&lt;/code&gt;, reduces magic gotchas when dealing with &lt;code&gt;ARGV&lt;/code&gt; (files implicitly opened from the command line)&lt;/li&gt;
&lt;li&gt;The list form of a pipe open now works on Windows.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/06/perl-v5-22-adds-hexadecimal-floating-point-literals&quot;&gt;Hexadecimal floating point values.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;close&lt;/code&gt; now puts its error in &lt;code&gt;$!&lt;/code&gt; and warns when an implicit close fails&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://metacpan.org/pod/CGI&quot;&gt;CGI&lt;/a&gt; and &lt;a href=&quot;https://metacpan.org/pod/Module::Build&quot;&gt;Module::Build&lt;/a&gt; modules are no longer in core Perl. You can still get them on CPAN.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Regex enhancements&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2016/06/perl-v5-22-adds-fancy-unicode-word-boundaries/&quot;&gt;Special &lt;code&gt;\b&lt;/code&gt; boundaries in regexes, including grapheme clusters, Unicode word boundaries, and sentence boundaries&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/08/turn-capture-groups-into-cluster-groups/&quot;&gt;The new &lt;code&gt;/n&lt;/code&gt; non-capturing regular expression flag makes all paren groups non-capturing&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;re&lt;/code&gt; module now has a &lt;code&gt;strict&lt;/code&gt; mode.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2017/01/xx-means-more-meaningless-whitespace-and-thats-good/&quot;&gt;The &lt;code&gt;/xx&lt;/code&gt; regex flag (doubled x) is deprecated because it will mean something later&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use v5.20 subroutine signatures</title>
		<link>https://www.effectiveperlprogramming.com/2015/04/use-v5-20-subroutine-signatures/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/04/use-v5-20-subroutine-signatures/</guid>
		<pubDate>Thu, 16 Apr 2015 03:33:21 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
&lt;img class=&quot;float-start me-3&quot; src=&quot;/images/perl-new-features-small.jpg&quot; width=&quot;182&quot; height=&quot;237&quot; alt=&quot;Cover of Perl New Features by brian d foy&quot;&gt; This is a chapter in &lt;span class=&quot;book-title&quot;&gt;Perl New Features&lt;/span&gt;, a book from &lt;a href=&quot;https://perlschool.com&quot;&gt;Perl School&lt;/a&gt; that you can buy on &lt;a href=&quot;https://leanpub.com/perl_new_features&quot;&gt;LeanPub&lt;/a&gt; or &lt;a href=&quot;https://amzn.to/3QVsScf&quot;&gt;Amazon&lt;/a&gt;. Your support helps me to produce more content.
&lt;/p&gt;


&lt;hr/&gt;



&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
This feature was promoted to a stable version in &lt;a href=&quot;/2024/07/perl-v5-36-new-features/&quot;&gt;v5.36&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Subroutine signatures, in a rudimentary form, have shown up in Perl v5.20 as an experimental feature. After a few years of debate and a couple of competing implementations, we have something we can use. And, because it was such a contentious subject, it got the attention a new feature deserves. They don&#39;t have all the features we want (notably type and value constraints), but Perl is in a good position to add those later.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Perl&#39;s roots were in simplicity and getting started as quickly as possible. We wanted to define subroutines without much work. Instead of creating signatures in a C header file and worrying about inputs and outputs, &lt;span class=&quot;person&quot; data-fullname=&quot;Larry Wall&quot;&gt;Larry&lt;/span&gt; made subroutines take in lists and return lists. Done and done.
&lt;/p&gt;



&lt;p&gt;
This simplicity means you have to do quite a bit of work yourself. You have to process the input list, in &lt;code&gt;@_&lt;/code&gt;, assign your own default values, and declare the variables to possibly store them. You ended up not saving much for the typical programmer.
&lt;/p&gt;



&lt;p&gt;
To start, you need to enable the experimental feature (see &lt;span class=&quot;item&quot;&gt;Item 2. Enable new Perl features when you need them.&lt;/span&gt; and &lt;a href=&quot;/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/&quot;&gt;turn off the experimental warnings&lt;/a&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To define a signature, you use the spot after the subroutine name where so far Perl has only allowed a prototype. This is not a prototype though; it&#39;s something different. Notably, the new subroutine signatures work with methods, whereas prototypes are compiler hints that method calls ignore:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

sub some_sub ($$) {
	say &quot;I got $_[0] and $_[1]&quot;;
	}

some_sub( &#39;Buster&#39;, &#39;Mimi&#39; );

main-&gt;some_sub();             # &quot;works&quot; just fine

some_sub( qw(Buster) );       # compile time error
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, you have a limited set of characters you can put in a prototype, and those exclude identifier characters (those we use to make names). If you&#39;ve enabled this experimental feature and Perl sees un-prototype like characters, it tries signatures instead. Note that Perl has another feature like this: the diamond operator, &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;, which might actually be the &lt;code&gt;glob&lt;/code&gt; operator if Perl sees glob characters in the argument. You can still use a prototype with signatures, but you probably shouldn&#39;t use prototypes. The &lt;a href=&quot;https://perldoc.perl.org/perlsub&quot;&gt;perlsub&lt;/a&gt; documentation shows you how you can use an attribute to make a prototype.
&lt;/p&gt;



&lt;p&gt;
First, be aware that using a signature does not mess with the normal argument list in &lt;code&gt;@_&lt;/code&gt;. You can still play with that yourself.
&lt;/p&gt;


&lt;h2&gt;The simplest signature&lt;/h2&gt;


&lt;p&gt;
The simplest signature is like the simplest prototype. Like prototypes, the signature enforces the number of arguments. To make a constant in Perl you can use a subroutine that takes no arguments. This is essentially what the &lt;a href=&quot;https://metacpan.org/pod/constant&quot;&gt;constant&lt;/a&gt; pragma does:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub cat () { &#39;Buster&#39; }

say &quot;Cat is &quot;, cat;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you try to pass an argument, you&#39;ll get an error but at runtime:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub cat () { &#39;Buster&#39; }
say &quot;Running...&quot;;
say &quot;Cat is &quot;, cat( &#39;Mini&#39;);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The first &lt;code&gt;say&lt;/code&gt; works, but the second fails when it calls &lt;code&gt;cat&lt;/code&gt; incorrectly:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Running...
Too many arguments for subroutine at cat.pl line 5.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
A prototype would have raised a compile-time error because the compiler already knows how many arguments there should be. This doesn&#39;t mean that the experimental signatures might do that later, but the implementation now is more of a code mangler. Deparsing it (&lt;a href=&quot;/2010/05/use-bdeparse-to-see-what-perl-thinks-the-code-is/&quot;&gt;Use B::Deparse to see what perl thinks the code is&lt;/a&gt;) shows that the &lt;code&gt;cat&lt;/code&gt; subroutine has a &lt;code&gt;die&lt;/code&gt; triggered by a check of &lt;code&gt;@_&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.20.0 -MO=Deparse cat.pl
sub BEGIN {
    require v5.20;
}
sub cat {
    BEGIN {${^WARNING_BITS} = &quot;\020\001\000\000\000P\004\000\000\000\000\000\000U\005&quot;}
    use strict;
    use feature &#39;current_sub&#39;, &#39;evalbytes&#39;, &#39;fc&#39;, &#39;say&#39;, &#39;signatures&#39;, &#39;state&#39;, &#39;switch&#39;, &#39;unicode_strings&#39;, &#39;unicode_eval&#39;;
    no feature &#39;array_base&#39;;
    die &#39;Too many arguments for subroutine&#39; unless @_ &amp;lt;= 0;
    ();
    &#39;Buster&#39;;
}
BEGIN {${^WARNING_BITS} = &quot;\020\001\000\000\000P\004\000\000\000\000\000\000U\005&quot;}
use strict;
use feature &#39;current_sub&#39;, &#39;evalbytes&#39;, &#39;fc&#39;, &#39;say&#39;, &#39;signatures&#39;, &#39;state&#39;, &#39;switch&#39;, &#39;unicode_strings&#39;, &#39;unicode_eval&#39;;
no feature &#39;array_base&#39;;
say &#39;Cat is &#39;, cat(&#39;Mini&#39;);
test.pl syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Don&#39;t get too hung up on that because it might be a temporary implementation detail. This does mean, however, that you can catch this error with &lt;code&gt;eval&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub cat () { &#39;Buster&#39; }
say &quot;Running...&quot;;
say &quot;Cat is &quot;, eval { cat( &#39;Mini&#39;) };

say &quot;Caught error: $@&quot; if $@;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now we catch the error, but notice it comes from the line of the subroutine definition, not the point where you called the subroutine like you would with a &lt;code&gt;croak&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Running...
Cat is 
Caught error: Too many arguments for subroutine at cat.pl line 5.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Mandatory, positional parameters&lt;/h2&gt;


&lt;p&gt;
The meat of this feature is your ability to assign to variables in what many perceive as a prettier way. Instead of declaring variables, usually with &lt;code&gt;my&lt;/code&gt;, and performing list operations on &lt;code&gt;@_&lt;/code&gt;, you list the variables in the signature in the order you would assign from &lt;code&gt;@_&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

cat( &#39;Buster&#39; );

sub cat ($cat) { 
	say &quot;The cat is $cat&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Again, this checks the number of parameters. With no arguments or more than one argument you get a runtime error.
&lt;/p&gt;



&lt;p&gt;
You separate multiple arguments with commas, just like a list assignment:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( &#39;Buster&#39;, &#39;Nikki&#39;, &#39;Godzilla&#39; );

sub animals ($cat, $dog, $lizard) { 
	say &quot;The cat is $cat&quot;;
	say &quot;The dog is $dog&quot;;
	say &quot;The lizard is $lizard&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
These variables are lexical variables in the scope of the subroutine (as you&#39;d see if you deparsed this code). That means you can&#39;t assign to special variables, which would cause a compile-time error. That is, except for &lt;code&gt;$_&lt;/code&gt;, which is experimentally lexical from a v5.10 misadventure with &lt;code&gt;given-when&lt;/code&gt; (&lt;a href=&quot;/2014/11/perl-v5-16_now_sets_proper_magic_on_lexical/&quot;&gt;Perl v5.16 now sets proper magic on lexical $_&lt;/a&gt; and &lt;a href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;Use for() instead of given()&lt;/a&gt;).  
&lt;/p&gt;



&lt;h2&gt;Placeholder parameters&lt;/h2&gt;


&lt;p&gt;
You don&#39;t have to name all of the parameters. You can use the lone &lt;code&gt;$&lt;/code&gt; to not immediately assign a value, probably because you&#39;ll process it yourself through &lt;code&gt;@_&lt;/code&gt;. In this example we don&#39;t care about the second argument, but the signature still needs the right number of positions and in the right sequence:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( &#39;Buster&#39;, &#39;Nikki&#39;, &#39;Godzilla&#39; );

sub animals ($cat, $, $lizard) {  # unnamed second parameter
	say &quot;The cat is $cat&quot;;
	say &quot;The lizard is $lizard&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is a bit tricky really. That second argument is &lt;em&gt;mandatory&lt;/em&gt; even though you&#39;ve neglected to give it a name. It will still be in &lt;code&gt;@_&lt;/code&gt; even though you haven&#39;t assigned it to a variable.
&lt;/p&gt;


&lt;h2&gt;Slurpy parameters&lt;/h2&gt;


&lt;p&gt;
At the end of the parameter list, you can have a &lt;i&gt;slurpy&lt;/i&gt; parameter, which is either a named array or hash. You can do this in a list assignment too, but a list assignment lets you put it in the middle despite the fact that any succeeding elements get nothing:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $cat, $dog, @animals, $lizard ) # $lizard will never get anything
	= qw( Buster Nikki Ginger Mimi Godzilla );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In the subroutine signature, that slurpy thing has to be at the end of the list:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub animals ( $cat, $dog, @animals ) { # @animals must be at the end
	...;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The rest of the arguments past the second show up in &lt;code&gt;@animals&lt;/code&gt;. But, here&#39;s the interesting thing; the number of things that can show up in the array can be zero.
&lt;/p&gt;



&lt;p&gt;
If you don&#39;t want to completely specify the number of arguments that your subroutine may take but you also don&#39;t want to create a named array, you can use a bare &lt;code&gt;@&lt;/code&gt; as a placeholder to mean that the argument list is unlimited:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub animals ( $cat, $dog, @ ) { # @ slurps the rest
	...;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The hash can also be a slurpy parameter, and just like the slurpy array it must be at the end of the signature:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub animals ( $cat, $dog, %args ) { # %args demands an even number
	...;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
For the hash, if there isn&#39;t an even number of elements left in &lt;code&gt;@_&lt;/code&gt;, you&#39;ll get a runtime exception. You don&#39;t have to name the hash, and a bare &lt;code&gt;%&lt;/code&gt; still demands an even number of elements:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub animals ( $cat, $dog, % ) { # % still demands an even number
	...;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Default values (optional parameters)&lt;/h2&gt;


&lt;p&gt;
Perhaps the best feature of signatures is default values. You can use Perl to decide what the default values are, even if that is a literal. However, you can only assign default values to optional parameters, which means that they have to appear after the mandatory arguments. In this example, the third argument is &lt;em&gt;optional&lt;/em&gt; and gets the default value &#39;MechaGodzilla&#39; when no argument is present:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

say &quot;First try-----------&quot;;
animals( &#39;Mimi&#39;, &#39;Nikki&#39;, &#39;Godzilla&#39; );

say &quot;Second try-----------&quot;;
animals( &#39;Buster&#39;, &#39;Nikki&#39;, );

sub animals ( $cat, $dog, $lizard = &#39;MechaGodzilla&#39; ) {
	say &quot;The cat is $cat&quot;;
	say &quot;The dog is $dog&quot;;
	say &quot;The lizard is $lizard&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
On the second try, you get the default value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;First try-----------
The cat is Mimi
The dog is Nikki
The lizard is Godzilla
Second try-----------
The cat is Buster
The dog is Nikki
The lizard is MechaGodzilla
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is only checking the number of arguments and assigning a value when the argument list is too short. If you pass &lt;code&gt;undef&lt;/code&gt; as an argument, that&#39;s the (un)value that parameter will get: 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

say &quot;First try-----------&quot;;
animals( &#39;Mimi&#39;, &#39;Nikki&#39;, &#39;Godzilla&#39; );

say &quot;Second try-----------&quot;;
animals( &#39;Buster&#39;, &#39;Nikki&#39;, );

say &quot;undef try-----------&quot;;
animals( &#39;Buster&#39;, &#39;Nikki&#39;, undef );

sub animals ( $cat, $dog, $lizard = &#39;MechaGodzilla&#39;, $bird = &#39;Poppy&#39; ) {
	say &quot;The cat is $cat&quot;;
	say &quot;The dog is $dog&quot;;
	say &quot;The lizard is $lizard&quot;;
	say &quot;The bird is $bird&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;undef&lt;/code&gt; does not trigger a default value, which may surprise many of you. Notice the third, &quot;undef try&quot; where &lt;code&gt;$lizard&lt;/code&gt; gets no value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;First try-----------
The cat is Mimi
The dog is Nikki
The lizard is Godzilla
The bird is Poppy
Second try-----------
The cat is Buster
The dog is Nikki
The lizard is MechaGodzilla
The bird is Poppy
undef try-----------
The cat is Buster
The dog is Nikki
The lizard is 
The bird is Poppy
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can also have a null default value. You might want to have one optional parameter but not assign a value if the argument list isn&#39;t long enough. This signature allows one or two arguments, with no defaults:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

say &quot;First try-----------&quot;;
animals( &#39;Mimi&#39;, &#39;Nikki&#39; );

say &quot;Second try-----------&quot;;
animals( &#39;Buster&#39; );

sub animals ( $cat, $= ) {  # second argument is optional
	say &quot;The cat is $cat&quot;;
	say &quot;Second argument is $_[1]&quot; if $#_ == 1;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You see that the second argument only exists if you specify it yourself:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;First try-----------
The cat is Mimi
Second argument is Nikki
Second try-----------
The cat is Buster
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
These default values don&#39;t work with slurpy types, though. Perl will complain at compile-time. If you want that sort of thing, though, you can make the argument a scalar and assign an array or hash reference to it:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( &#39;Buster&#39; );

sub animals ( $cat, $hash={} ) {  # hashref argument is optional
	say &quot;The cat is $cat&quot;;
	...;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Fancy optional values&lt;/h3&gt;


&lt;p&gt;
So far your defaults have been simple values, but you can use Perl. That means you can do almost anything. This one uses the value in another variable and increments it as it assigns defaults:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( &#39;Mimi&#39; );
animals( &#39;Buster&#39; );

{
my $auto_id = 0;
sub animals ( $cat, $id = ++$auto_id ) {
	say &quot;$id: The cat is $cat&quot;;
	}
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Each cat automatically gets its own sequence value since the &lt;code&gt;animals&lt;/code&gt; subroutine closed over &lt;code&gt;$auto_id&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: The cat is Mimi
2: The cat is Buster
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, you can&#39;t do something tricky to bring &lt;code&gt;$auto_id&lt;/code&gt; into the subroutine since the parser doesn&#39;t know about the variable soon enough. Neither of these work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub animals ( $cat, $id = do { state $auto_id = 0; $auto_id++ } ) {
	say &quot;$auto_id: The cat is $cat&quot;;
	}

sub animals ( $cat, $id = $auto_id++ ) {
	state $auto_id++;
	say &quot;$auto_id: The cat is $cat&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can make the default value a call to a subroutine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( &#39;Mimi&#39; );
animals( &#39;Buster&#39; );

sub animals ( $cat, $auto_id = get_id() ) {
	say &quot;$auto_id: The cat is $cat&quot;;
	}

sub get_id () {
	state $id = 0;
	++$id;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Method signatures&lt;/h2&gt;


&lt;p&gt;
My favorite part of signatures is that they work with methods. This doesn&#39;t mean that we have multi-dispatch in Perl (yet) (well, Perl 6 does but that&#39;s a different language). The signatures aren&#39;t any different; they follow all the same rules:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

package Local::Animals {
	use feature qw(signatures);
	no warnings qw(experimental::signatures);

	sub new ( $class, $args = {&#39;name&#39; =&gt; &#39;Buster&#39;} ) {
		bless $args, $class
		}

	sub name ( $self ) {
		$self-&gt;{name};
		}
	}

my $cat = Local::Animals-&gt;new;
say &quot;Cat is &quot;, $cat-&gt;name;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;A quick guide&lt;/h2&gt;

&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Arguments&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Signature&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Exactly zero arguments—empty signature&lt;/td&gt;&lt;td&gt;&lt;code&gt;()&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Exactly one named argument &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Zero or one arguments, with no default, unnamed &lt;/td&gt;&lt;td&gt;&lt;code&gt;($=)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Zero or one arguments, with no default, named &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat=)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;Zero or one arguments, with a default &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat=&#39;Buster&#39;)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Exactly two arguments, &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two or more arguments, any number &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, @)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two or more arguments, but an even number &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, %)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two or more arguments, slurping rest into an array &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, @animals)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two or more arguments, slurping rest into a hash &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, %animals)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two, three, or four arguments, no defaults &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, $=, $=)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two, three, or four arguments, one default &lt;/td&gt;&lt;td&gt;&lt;code&gt;($cat, $dog, $lizard=&#39;Godzilla&#39;, $=)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Class method &lt;/td&gt;&lt;td&gt;&lt;code&gt;( $class, ... )&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Object method &lt;/td&gt;&lt;td&gt;&lt;code&gt;( $self, ... )&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Subroutine signatures are experimental&lt;/li&gt;
&lt;li&gt;Signatures are not prototypes&lt;/li&gt;
&lt;li&gt;Signatures enforce the number of arguments, but at runtime&lt;/li&gt;
&lt;li&gt;Default values only apply to optional parameters, and only apply to parameters past the number of actual passed arguments&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use the :prototype attribute</title>
		<link>https://www.effectiveperlprogramming.com/2015/04/use-the-prototype-attribute/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/04/use-the-prototype-attribute/</guid>
		<pubDate>Thu, 16 Apr 2015 01:47:08 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl 5.20 introduced &lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt;experimental subroutine signatures&lt;/a&gt;. Now two features vie for the parentheses that come after the name in a subroutine definition. To get around that, v5.20 introduced the &lt;code&gt;:prototype&lt;/code&gt; attribute.
&lt;/p&gt;



&lt;p&gt;
There&#39;s not much to this. Here&#39;s a prototype for a subroutine that takes two arguments:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub add ($$) { ... }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Change that to the attribute form:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
sub add :prototype($$) { ... }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, you probably &lt;a href=&quot;/2011/10/understand-why-you-probably-dont-need-prototypes/&quot;&gt;don&#39;t need prototypes&lt;/a&gt; in the first place. You might consider simply getting rid of them altogether.
&lt;/p&gt;



&lt;p&gt;
Subroutine signatures are experimental and have to go through at least two maintenance releases before they can graduate out of an experimental feature, so be ready for that. I expect that most people will have problems with module code they don&#39;t control, so you might be at the mercy of the module maintainers.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.18 adds character class set operations</title>
		<link>https://www.effectiveperlprogramming.com/2015/02/perl-v5-18-adds-character-class-set-operations/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/02/perl-v5-18-adds-character-class-set-operations/</guid>
		<pubDate>Wed, 25 Feb 2015 02:30:07 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
Perl v5.18 added experimental character class set operations, a requirement for full Unicode support according to &lt;a href=&quot;https://unicode.org/reports/tr18/&quot;&gt;Unicode Technical Standard #18&lt;/a&gt;, which specifies what a compliant language must support and divides those into three levels.
&lt;/p&gt;



&lt;p&gt;
The &lt;a href=&quot;https://perldoc.perl.org/perlunicode#Unicode-Regular-Expression-Support-Level&quot;&gt;perlunicode&lt;/a&gt; documentation lists each requirement and its status in Perl. Besides some regular expression anchors handling all forms of line boundaries (which might break older programs), set subtraction and intersection in character classes was the last feature Perl needed to be Level 1 compliant.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Perl calls this experimental feature &quot;Extended Bracketed Character Classes&quot; in &lt;a href=&quot;https://perldoc.perl.org/perlrecharclass#Extended-Bracketed-Character-Classes&quot;&gt;perlrecharclass&lt;/a&gt;. Inside the &lt;code&gt;(?[  ])&lt;/code&gt;, a regular expression does character class set operations. Inside the brackets, whitespace is insignificant (as if &lt;code&gt;/x&lt;/code&gt; is on). Here&#39;s a simple example to find the character &lt;i&gt;z&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ [z] ])/;

while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	say &quot;[$_] &quot;, /$regex/ ? &#39;Matched&#39; : &#39;Missed&#39;;
	}

__DATA__
This is a line
This is the next line
And here&#39;s another line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
None of the input lines have a letter &lt;i&gt;z&lt;/i&gt;, so nothing matches:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[This is a line] Missed
[This is the next line] Missed
[And here&#39;s another line] Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To add more characters to the set, in old Perl (and still, even), you would add that character in the same set of brackets. If you want to find an &lt;i&gt;x&lt;/i&gt;, you add that next to the &lt;i&gt;z&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ [xz] ])/;

while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	say &quot;[$_] &quot;, /$regex/ ? &#39;Matched&#39; : &#39;Missed&#39;;
	}

__DATA__
This is a line
This is the next line
And here&#39;s another line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
And now the middle input line matches:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[This is a line] Missed
[This is the next line] Matched
[And here&#39;s another line] Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, you can do this with set math. Since you want either of those to match, you would take a union. Inside the &lt;code&gt;(?[  ])&lt;/code&gt;, a &lt;code&gt;+&lt;/code&gt; is the union operator (the &lt;code&gt;|&lt;/code&gt; is also the union operator). Almost everything inside &lt;code&gt;(?[  ])&lt;/code&gt; is a metacharacter, which is why you had to have another set of brackets around the literal characters in the previous example:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ [x] + [z] ])/;

while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	say &quot;[$_] &quot;, /$regex/ ? &#39;Matched&#39; : &#39;Missed&#39;;
	}

__DATA__
This is a line
This is the next line
And here&#39;s another line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output is the same as before because it&#39;s the same character class:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[This is a line] Missed
[This is the next line] Matched
[And here&#39;s another line] Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can also do intersections with the &lt;code&gt;&amp;&lt;/code&gt;. In this example, you have two separate character classes that each have one character that matches each input line and they only have one character in common:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ [sxy] &amp; [exw] ])/;


while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	say &quot;[$_] &quot;, /$regex/ ? &#39;Matched&#39; : &#39;Missed&#39;;
	}

__DATA__
This is a line
This is the next line
And here&#39;s another line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Their intersection is only &lt;i&gt;x&lt;/i&gt;, so only that character matches and you get the same input, again:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[This is a line] Missed
[This is the next line] Matched
[And here&#39;s another line] Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;-&lt;/code&gt; is the set subtraction operator. In this example, the first character class is Perl word characters. You subtract from that the ASCII alphabetical characters, leaving only the digits and underscore:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ [\w] - [a-zA-Z] ])/;


while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	say &quot;[$_] &quot;, /$regex/ ? &#39;Matched&#39; : &#39;Missed&#39;;
	}

__DATA__
This is 1 line
This is the next line
And here&#39;s another line
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Only the first line has a digit, so only it matches:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[This is 1 line] Matched
[This is the next line] Missed
[And here&#39;s another line] Missed
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This gets more interesting with named properties, the only Level 2 feature Perl supports so far (see &lt;a href=&quot;https://perldoc.perl.org/perluniprops&quot;&gt;perluniprops&lt;/a&gt;). Some character classes may be easier to construct, read, and maintain without losing their literal characters. Suppose you want to get just the Eastern Arabic digits, perhaps because you&#39;re in a country that uses Arabic as I am as I write this. You can take the intersection of the &lt;i&gt;Arabic&lt;/i&gt; property and the &lt;i&gt;Digit&lt;/i&gt; property. The Universal Character Set has this wonderful feature to assign many labels to its characters so we can identify subsets of a particular script:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ \p{Arabic} &amp; \p{Digit} ])/;

foreach my $ord ( 0 .. 0x10fffd ) {
	my $char = chr( $ord );
	say $char if $char =~ m/$regex/;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now you see just the digits from that script:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;۰
۱
۲
۳
۴
۵
۶
۷
۸
۹
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can get more complicated. If you wanted the Western Arabic Digits too (what we normally call just &quot;Arabic numerals&quot;), you could join two intersections in a union. Although some of this problem is easy, that doesn&#39;t show off the operations. In this example, you have two separate intersections that are joined in a union:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ 
	( \p{Arabic} &amp; \p{Digit} ) 
		+ 
	( \p{ASCII}  &amp; \p{Digit} ) 
	])/;

foreach my $ord ( 0 .. 0x10fffd ) {
	my $char = chr( $ord );
	say $char if $char =~ m/$regex/;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now you see two sets of numerals:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0
1
2
3
4
5
6
7
8
9
۰
۱
۲
۳
۴
۵
۶
۷
۸
۹
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There is one more character class set operator, the &lt;code&gt;^&lt;/code&gt;, which acts like an exclusive-or (the &lt;code&gt;xor&lt;/code&gt; bit operator uses the same character). This operator takes the union of the two character classes then subtracts their intersection. That is, the resulting set has all the characters in both classes except for the ones they both have.
&lt;/p&gt;



&lt;p&gt;
In this example, you have two intersections to extract the hex digits and digits from ASCII. That&#39;s important since other scripts in the UCS have characters with these properties. From those intersections, you use the &lt;code&gt;^&lt;/code&gt; to get the set that only contains the characters that show up in exactly one set.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use utf8;
use open qw(:std :utf8);

no warnings qw(experimental::regex_sets);

my $regex = qr/(?[ 
	( \p{ASCII} &amp; \p{HexDigit} )
		^ 
	( \p{ASCII} &amp; \p{Digit} )
	])/;

foreach my $ord ( 0 .. 0x10fffd ) {
	my $char = chr( $ord );
	say $char if $char =~ m/$regex/;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In this case, it&#39;s the uppercase and lowercase letters:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;A
B
C
D
E
F
a
b
c
d
e
f
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s another example. In &lt;a href=&quot;/2010/07/match-unicode-characters-by-property-value/&quot;&gt;Match Unicode characters by property value&lt;/a&gt;, you could find characters by their numerical value. But not all of those are &quot;digits&quot;. You can construct a character class for the digits (already the shortcut &lt;code&gt;\d&lt;/code&gt;) and the character class with numerical values. Only when both of those character classes match in the same position does the overall character class set match: 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
no warnings qw(experimental::regex_sets);

use open qw(:std :utf8);

foreach ( 1 .. 0x10fffd ) {
	next unless chr =~ m/ (?[ \d &amp; [\p{nv=1}\p{nv=3}\p{nv=7}] ]) /x;
	printf &quot;%s (U+%04X)\n&quot;, chr, $_;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Regular expression character class set operations satisfy UTS #18 Level 1 requirements.&lt;/li&gt;
&lt;li&gt;You can compose character classes from other classes with unions, intersections, and subtractions.&lt;/li&gt;
&lt;li&gt;Inside the &lt;code&gt;(?[  ])&lt;/code&gt;, whitespace is insignificant.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;regex_sets&lt;/code&gt; is an experimental feature.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Don&#39;t use named lexical subroutines</title>
		<link>https://www.effectiveperlprogramming.com/2015/01/named-lexical-subroutines/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2015/01/named-lexical-subroutines/</guid>
		<pubDate>Thu, 15 Jan 2015 08:12:59 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Lexical subroutines are a stable feature starting with v5.26
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.18 allows you to define named subroutines that exist only in the current lexical scope. These act (almost) just like the regular named subroutines that you already know about from &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;&lt;span class=&quot;book-title&quot;&gt;Learning Perl&lt;/span&gt;&lt;/a&gt;, but also like the lexical variables that have limited effect. The problem is that the feature is almost irredeemably broken, which you&#39;ll see at the end of this Item. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;How it&#39;s supposed to work&lt;/h2&gt;


&lt;p&gt;
Remember how lexical variables work. You can have package (global) variables and lexical variables with the same name and they do not affect each other.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$cat = &#39;Buster&#39;;
print &quot;Global scope - package cat is $cat\n&quot;;

{
my $cat = &#39;Mimi&#39;;
print &quot;Lexical scope - my cat is $cat\n&quot;;
}

print &quot;Global scope again - package cat is $cat\n&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Inside the braces, you have a variable with the same name, but it&#39;s a completely different variable. Changing the &lt;code&gt;my&lt;/code&gt; variable does not affect the package one:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Global scope - package cat is Buster
Lexical scope - my cat is Mimi
Global scope again - package cat is Buster
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl v5.6 added the &lt;code&gt;our&lt;/code&gt; keyword, which makes a package variable visible in a lexical scope:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$cat = &#39;Buster&#39;;
print &quot;Global scope - package cat is $cat\n&quot;;

OUTER: {
my $cat = &#39;Mimi&#39;;
print &quot;Lexical scope - my cat is $cat\n&quot;;

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		print &quot;INNER lexical scope - our cat is $cat\n&quot;;
		}

print &quot;After INNER scope - my cat is $cat\n&quot;;
}

print &quot;Global scope again - package cat is $cat\n&quot;;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
Inside the &lt;code&gt;INNER&lt;/code&gt; scope, you change the value of &lt;code&gt;$cat&lt;/code&gt;, which is the package version, and you see the change at the end too. Instead of using the &lt;code&gt;my&lt;/code&gt; variable in the enclosing scope, you get the global version back:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Global scope - package cat is Buster
Lexical scope - my cat is Mimi
INNER lexical scope - our cat is Roscoe
After INNER scope - my cat is Mimi
Global scope again - package cat is Roscoe
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Also remember that the code in a subroutine you define outside of a scope cannot see the &lt;code&gt;my&lt;/code&gt; variables:
&lt;/p&gt;



&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$cat = &#39;Buster&#39;;
show_cat( &#39;1&#39; );

OUTER: {
my $cat = &#39;Mimi&#39;;
show_cat( &#39;2 (my)&#39; );

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		show_cat( &#39;3 (our)&#39; );
		}

show_cat( &#39;4 (my)&#39; );
}

show_cat( &#39;5&#39; );

sub show_cat {
	print &quot;$_[0]: Cat is $cat\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In the calls to &lt;code&gt;show_cat&lt;/code&gt; with the &lt;code&gt;my&lt;/code&gt; labels, you still see the global version because that&#39;s the only &lt;code&gt;$cat&lt;/code&gt; that the subroutine can see (this is not new):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: Cat is Buster
2 (my): Cat is Buster
3 (our): Cat is Roscoe
4 (my): Cat is Roscoe
5: Cat is Roscoe
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This changes if you define &lt;code&gt;show_cat&lt;/code&gt; in the same scope as the lexical variable. If you define it after you declare the &lt;code&gt;my&lt;/code&gt; variable, that&#39;s the only version the subroutine sees: 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$cat = &#39;Buster&#39;;
show_cat( &#39;1&#39; );

OUTER: {
my $cat = &#39;Mimi&#39;;
sub show_cat {
	print &quot;$_[0]: Cat is $cat\n&quot;;
	}

show_cat( &#39;2 (my)&#39; );

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		show_cat( &#39;3 (our)&#39; );
		}

show_cat( &#39;4 (my)&#39; );
}

show_cat( &#39;5&#39; );
&lt;/code&gt;&lt;/pre&gt;
	

&lt;p&gt;
That &lt;code&gt;show_cat&lt;/code&gt; bound to the &lt;code&gt;my&lt;/code&gt; variable and stuck with that one. This is a closure that allows that lexical variable to sneak out of its scope. That last call takes place after the &lt;code&gt;my&lt;/code&gt; variable is gone, even though the subroutine still has a reference to the data:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: Cat is 
2 (my): Cat is Mimi
3 (our): Cat is Mimi
4 (my): Cat is Mimi
5: Cat is Mimi
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You see something different if you define the &lt;code&gt;my&lt;/code&gt; variable &lt;em&gt;after&lt;/em&gt; you define the named subroutine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$cat = &#39;Buster&#39;;
show_cat( &#39;1&#39; );

OUTER: {
sub show_cat {
	print &quot;$_[0]: Cat is $cat\n&quot;;
	}
my $cat = &#39;Mimi&#39;;

show_cat( &#39;2 (my)&#39; );

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		show_cat( &#39;3 (our)&#39; );
		}

show_cat( &#39;4 (my)&#39; );
}

show_cat( &#39;5&#39; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That version only sees the global version of the variable and you get the same output as you saw when you defined the subroutine outside of the scope:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;1: Cat is Buster
2 (my): Cat is Buster
3 (our): Cat is Roscoe
4 (my): Cat is Roscoe
5: Cat is Roscoe
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you are comfortable with the variable version of this, you shouldn&#39;t have too much trouble with the subroutine version of it. In Perl, a dynamic language, subroutines are actually data and are also variables. You can change their definitions, and I spend quite a bit of space in &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt; writing about that. 
&lt;/p&gt;



&lt;p&gt;
Define two versions of &lt;code&gt;show_cat&lt;/code&gt;. Prefix the definition inside the &lt;code&gt;OUTER&lt;/code&gt; scope with &lt;code&gt;my&lt;/code&gt; even though it&#39;s a named subroutine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

use vars qw($cat);  # use v5.18 turns on strict

$cat = &#39;Buster&#39;;
show_cat( &#39;1&#39; );

OUTER: {
my sub show_cat {
	print &quot;Inside cat! $_[0] is $cat\n&quot;;
	}
my $cat = &#39;Mimi&#39;;

show_cat( &#39;2 (my)&#39; );

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		show_cat( &#39;3 (our)&#39; );
		}

show_cat( &#39;4 (my)&#39; );
}

show_cat( &#39;5&#39; );

sub show_cat {
	print &quot;Outside cat! $_[0] is $cat\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output isn&#39;t that different except the middle three calls use the definition of &lt;code&gt;show_cat&lt;/code&gt; you defined with &lt;code&gt;my&lt;/code&gt;, even though it still looked at the global &lt;code&gt;$cat&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Outside cat! 1 is Buster
Inside cat! 2 (my) is Buster
Inside cat! 3 (our) is Roscoe
Inside cat! 4 (my) is Roscoe
Outside cat! 5 is Roscoe
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you move the lexical subroutine definition after the &lt;code&gt;my $cat&lt;/code&gt;, it binds to a different variable just as before:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

use vars qw($cat);  # use v5.18 turns on strict

$cat = &#39;Buster&#39;;
show_cat( &#39;1&#39; );

OUTER: {
my $cat = &#39;Mimi&#39;;
my sub show_cat {
	print &quot;Inside cat! $_[0] is $cat\n&quot;;
	}

show_cat( &#39;2 (my)&#39; );

	INNER: {
		our $cat = &#39;Roscoe&#39;;
		show_cat( &#39;3 (our)&#39; );
		}

show_cat( &#39;4 (my)&#39; );
}

show_cat( &#39;5&#39; );

sub show_cat {
	print &quot;Outside cat! $_[0] is $cat\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the inner subroutine knows about the &lt;code&gt;my&lt;/code&gt; variable, just as it did without the lexical version.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Outside cat! 1 is Buster
Inside cat! 2 (my) is Mimi
Inside cat! 3 (our) is Mimi
Inside cat! 4 (my) is Mimi
Outside cat! 5 is Roscoe
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s how it works. More importantly, though, is how it doesn&#39;t work and if there&#39;s any new value in this feature.
&lt;/p&gt;


&lt;h2&gt;Problems&lt;/h2&gt;


&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Lexical subroutines are stable features in v5.26.
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Lexical subroutines are experimental, which means they might change or even disappear. It also means they likely have problems. And, from the problems I found, they won&#39;t work the same way in the future if the feature persists.
&lt;/p&gt;



&lt;p&gt;
First, you can&#39;t use a lexical subroutine with &lt;code&gt;sort&lt;/code&gt;.
&lt;/p&gt;

&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
&lt;a href=&quot;https://stackoverflow.com/questions/27913932/does-perl-v5-18s-sort-understand-lexical-subroutines&quot;&gt;This is broken in v5.18 and v5.20 (so far)&lt;/a&gt; but is fixed in v5.22.
&lt;/p&gt;
&lt;/div&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my sub my_way { ... }

my @sorted = sort my_way @elements;  # looks for main::my_way
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This would have been one of the more interesting uses but it doesn&#39;t really add anything that you can&#39;t already do. If you want a private &lt;code&gt;sort&lt;/code&gt; subroutine, a coderef works:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $my_way = sub { ... }

my @sorted = sort $my_way @elements;  # works
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Next, you might immediately think of private methods. It would be quite useful to have a way to do that, but Perl uses the symbol table to resolve methods. An object will not find a lexical subroutine in the symbol table.
&lt;/p&gt;



&lt;p&gt;
So far, I have found only one interesting case where this new feature might provide something that didn&#39;t exist before. The &lt;code&gt;my sub&lt;/code&gt; hides a subroutine. An &lt;code&gt;our sub&lt;/code&gt;, however, can expose a subroutine in one package to all the other packages in the same lexical scope (&lt;a href=&quot;/2010/01/know-what-creates-a-scope/&quot;&gt;Know what creates a scope&lt;/a&gt;; a package isn&#39;t one of them). If you declare a subroutine with &lt;code&gt;our sub&lt;/code&gt;, for the rest of the lexical scope, even across packages, that&#39;s the subroutine you get with that name:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

our sub speak { say &quot;Logging $_[0]&quot; }

package Cat {
	sub meow {
		speak( __PACKAGE__ );
		}
	}

package Dog {
	sub woof {
		speak( __PACKAGE__ );
		}
	}

Cat-&gt;meow;
Dog-&gt;woof;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output isn&#39;t surprising:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Logging Cat
Logging Dog
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, what if &lt;code&gt;Cat&lt;/code&gt; already has a &lt;code&gt;speak&lt;/code&gt; method? In that case, it gets really weird:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

our sub speak { say &quot;Logging $_[0]&quot; }

package Cat {
	sub meow {
		speak( __PACKAGE__ );
		}

	sub speak {
		say &quot;This is a cat speaking!&quot;;
		}
	}

package Dog {
	sub woof {
		speak( __PACKAGE__ );
		}
	}

Dog-&gt;woof;

speak( &quot;At the end&quot; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This time, you only call &lt;code&gt;Dog-&gt;woof&lt;/code&gt;, but you get output from a subroutine that is not in your package nor in your scope:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;This is a cat speaking!
This is a cat speaking!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
How does &lt;code&gt;Dog&lt;/code&gt; know anything about that subroutine? You don&#39;t even get a warning for a redefined subroutine (although &lt;code&gt;use warnings&lt;/code&gt; would take care of that).
&lt;/p&gt;



&lt;p&gt;
You can do the same thing without the named subroutine and it works:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $speak = sub { say &quot;Logging $_[0]&quot; };

package Cat {
	my $speak = sub { &quot;This is a cat speaking!&quot; };
	sub meow {
		$speak-&gt;( __PACKAGE__ );
		}
	}

package Dog {
	sub woof {
		$speak-&gt;( __PACKAGE__ );
		}
	}

Dog-&gt;woof;

$speak-&gt;( &quot;At the end&quot; );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Besides the incredibly bad idea of confusing almost every programmer with the source of a subroutine, it appears to be insanely broken before v5.22. You might like reading &lt;a href=&quot;https://stackoverflow.com/questions/7523757/is-it-a-design-flaw-that-perl-subs-arent-lexically-scoped&quot;&gt;Is it a design flaw that Perl subs aren&#39;t lexically scoped?&lt;/a&gt;.
&lt;/p&gt;


&lt;h2&gt;Things to Remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lexical subroutines are a broken experimental feature you shouldn&#39;t remember.&lt;/li&gt;
&lt;li&gt;You can already do just about everything with a coderef.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Enforce ASCII semantics when you only want ASCII</title>
		<link>https://www.effectiveperlprogramming.com/2014/12/enforce-ascii-semantics-when-you-only-want-ascii/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/12/enforce-ascii-semantics-when-you-only-want-ascii/</guid>
		<pubDate>Fri, 19 Dec 2014 14:39:05 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-14</category>
		<description>&lt;p&gt;
When Perl made regexes more Unicode aware, starting in v5.6, some of the character class definitions and match modifiers changed. What you expected to match &lt;code&gt;\d&lt;/code&gt;, &lt;code&gt;\s&lt;/code&gt;, or &lt;code&gt;\w&lt;/code&gt; are more expansive now (&lt;a class=&quot;item&quot; href=&quot;/2011/01/know-your-character-classes/&quot;&gt;Know your character classes under different semantics&lt;/a&gt;). Most of us probably didn&#39;t notice because the range of our inputs is limited.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
To get the ASCII semantics back, you can use v5.14&#39;s &lt;code&gt;/a&lt;/code&gt; match flag to restore their pre-v5.6 meanings.
&lt;/p&gt;



&lt;p&gt;
If you look for &lt;code&gt;\d&lt;/code&gt; in later Perls, for example, you get a long list:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use charnames qw(:full);

foreach ( 0 .. 0x10_ffff ) {
	next unless chr =~ /\d/;
	printf qq(0x%02X  --&gt; %s\n), $_, charnames::viacode($_);
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The number of matches you get depends on the version of Unicode included with that Perl:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0x30  --&gt; DIGIT ZERO
0x31  --&gt; DIGIT ONE
0x32  --&gt; DIGIT TWO
0x33  --&gt; DIGIT THREE
0x34  --&gt; DIGIT FOUR
0x35  --&gt; DIGIT FIVE
0x36  --&gt; DIGIT SIX
0x37  --&gt; DIGIT SEVEN
0x38  --&gt; DIGIT EIGHT
0x39  --&gt; DIGIT NINE
0x660  --&gt; ARABIC-INDIC DIGIT ZERO
0x661  --&gt; ARABIC-INDIC DIGIT ONE
0x662  --&gt; ARABIC-INDIC DIGIT TWO
...
0x1D7FD  --&gt; MATHEMATICAL MONOSPACE DIGIT SEVEN
0x1D7FE  --&gt; MATHEMATICAL MONOSPACE DIGIT EIGHT
0x1D7FF  --&gt; MATHEMATICAL MONOSPACE DIGIT NINE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can change this with the &lt;code&gt;/a&lt;/code&gt; flag:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use charnames qw(:full);

foreach ( 0 .. 0x10_ffff ) {
	next unless chr =~ /\d/a;
	printf qq(0x%02X  --&gt; %s\n), $_, charnames::viacode($_);
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That makes &lt;code&gt;\d&lt;/code&gt; match only 0 to 9:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0x30  --&gt; DIGIT ZERO
0x31  --&gt; DIGIT ONE
0x32  --&gt; DIGIT TWO
0x33  --&gt; DIGIT THREE
0x34  --&gt; DIGIT FOUR
0x35  --&gt; DIGIT FIVE
0x36  --&gt; DIGIT SIX
0x37  --&gt; DIGIT SEVEN
0x38  --&gt; DIGIT EIGHT
0x39  --&gt; DIGIT NINE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The same goes for whitespace (&lt;code&gt;\s&lt;/code&gt;) and word characters  (&lt;code&gt;\w&lt;/code&gt;).
&lt;/p&gt;



&lt;p&gt;
That&#39;s not all, though. There&#39;s a problem with case insensitive matches. Some of the &quot;wide&quot; characters lowercase into the ASCII range. Well, so far there&#39;s exactly one, but that doesn&#39;t mean there might be more later. The Kelvin symbol, K (U+212A), lowercases into &lt;i&gt;k&lt;/i&gt; (U+006B). To avoid problems with fonts, I create the characters with the &lt;code&gt;\x{}&lt;/code&gt; sequences:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;

my $lower_k = &quot;\x{006B}&quot;;

if( $lower_k =~ /\x{212A}/ ) {
	say &quot;Matches without case insensitivity&quot;;
	}

if( $lower_k =~ /\x{212A}/i ) {
	say &quot;Matches with case insensitivity&quot;;
	}

if( $lower_k =~ /\x{212A}/ia ) {
	say &quot;Matches with case insensitivity, /a&quot;;
	}

if( $lower_k =~ /\x{212A}/iaa ) {
	say &quot;Matches with case insensitivity, /aa&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;
 

&lt;p&gt;
The Kelvin sign matches with case insensitivity, even with the &lt;code&gt;/a&lt;/code&gt; flag. However, it doesn&#39;t match when you double up with &lt;code&gt;/aa&lt;/code&gt;. That extra &lt;code&gt;/a&lt;/code&gt; means, &quot;no really, I mean ASCII.&quot;
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Matches with case insensitivity
Matches with case insensitivity, /a
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you&#39;re upgrading a huge codebase and want this behavior on all the regexes in a lexical scope (including a file (&lt;a href=&quot;/2010/01/know-what-creates-a-scope/&quot;&gt;Know what creates a scope&lt;/a&gt;)), you can &lt;a href=&quot;/2011/06/set-default-regular-expression-modifiers/&quot;&gt;Set default regular expression modifiers&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
Read &lt;a href=&quot;https://perldoc.perl.org/perlre&quot;&gt;perlre&lt;/a&gt; for the rest of the story.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.16 now sets proper magic on lexical $_</title>
		<link>https://www.effectiveperlprogramming.com/2014/11/perl-v5-16_now_sets_proper_magic_on_lexical/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/11/perl-v5-16_now_sets_proper_magic_on_lexical/</guid>
		<pubDate>Tue, 04 Nov 2014 22:51:15 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-16</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
Lexical &lt;code&gt;$_&lt;/code&gt; was removed in &lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24/&quot;&gt;v5.24&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.10 introduced &lt;code&gt;given&lt;/code&gt; and the lexical &lt;code&gt;$_&lt;/code&gt;. That use of &lt;code&gt;$_&lt;/code&gt;, which everyone has assumed is a global variable, turned out to be a huge mistake. The various bookkeeping on the global version didn&#39;t happen with the lexical version, so strange things happened. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You saw the problem in &lt;a class=&quot;item&quot; href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;Use for instead of given&lt;/a&gt;. For that reason, we recommended substituting &lt;code&gt;for&lt;/code&gt; instead of &lt;code&gt;given&lt;/code&gt; since &lt;code&gt;when&lt;/code&gt; acts the same in either (which was a correction to &lt;span class=&quot;item&quot;&gt;Use given-when to make a switch statement&lt;/span&gt; from the book).
&lt;/p&gt;



&lt;p&gt;
Previously I showed this program, which up to v5.16 showed that the &lt;code&gt;$_&lt;/code&gt; remembered the value of &lt;code&gt;pos&lt;/code&gt; from previous runs even with a new variable (each call to &lt;code&gt;try_given&lt;/code&gt; has a new &lt;code&gt;$s&lt;/code&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $s = &quot;abc&quot;;

try_given($s) for 1 .. 3;

sub try_given {
	my $s = shift;
	state $n = 0;

	given ($s) {
		/./g;     # $_ here is now the lexical version
		printf &quot;%d. given: pos=%d\n&quot;, ++$n, pos;
		}
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Under v5.10 to v5.14, the value of &lt;code&gt;pos&lt;/code&gt; advances by one each time although it should work on a new variable:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.10 try_given.pl
1. given: pos=1
2. given: pos=2
3. given: pos=3
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Under v5.16, the magic on &lt;code&gt;$_&lt;/code&gt; works and &lt;code&gt;pos&lt;/code&gt; reports the correct value:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.16 try_given.pl
1. given: pos=1
2. given: pos=1
3. given: pos=1
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Use a computed label with loop controllers</title>
		<link>https://www.effectiveperlprogramming.com/2014/10/use-a-computed-label-with-loop-controllers/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/10/use-a-computed-label-with-loop-controllers/</guid>
		<pubDate>Sat, 25 Oct 2014 01:16:39 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
Not sure which loop you want to break out of? Perl v5.18 makes that easy with &lt;i&gt;computed labels&lt;/i&gt;. The value you give &lt;code&gt;next&lt;/code&gt;, &lt;code&gt;last&lt;/code&gt;, and &lt;code&gt;redo&lt;/code&gt; no longer has to be a literal. You could already do this with &lt;code&gt;goto&lt;/code&gt;, but now you can give the loop controllers an expression. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Here&#39;s a contrived example. While reading lines of input and breaking the lines into words, this program might encounter either &lt;code&gt;skipword&lt;/code&gt; or &lt;code&gt;skipline&lt;/code&gt;, in which case the program should do just that. The problem is that those strings refer to different loops. That&#39;s not a problem, though:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.18;

LINE: while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	WORD: foreach my $word ( split ) {
		next uc($1) if $word  =~ m/\Askip(.*)/;
		say $word;
		}
	}

__DATA__
one two 
three four
five skipword six
seven eight
nine skipline forty Buster
ten eleven
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In the output, you don&#39;t see &lt;code&gt;skipword&lt;/code&gt;, or &lt;code&gt;skipline&lt;/code&gt; and anything that comes after it:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.1 skipline.pl
one
two
three
four
five
six
seven
eight
nine
ten
eleven
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You can do this without much more work without this feature, but you have to hardcode the labels:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

LINE: while( &amp;lt;DATA&amp;gt; ) {
	chomp;
	WORD: foreach my $word ( split ) {
		next LINE if $word eq &#39;skipline&#39;;
		next WORD if $word eq &#39;skipword&#39;;
		say $word;
		}
	}

__DATA__
one two 
three four
five skipword six
seven eight
nine skipline forty Buster
ten eleven
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
I don&#39;t have a specific recommendation for using these, but the &lt;code&gt;goto&lt;/code&gt; case, which has been around for a long time, is much more interesting because a loop label is only one possible target. A &lt;code&gt;goto&lt;/code&gt; can also jump into a code reference so it already knows how to deal with an expression.
&lt;/p&gt;



&lt;p&gt;
This program is a bit different because &lt;code&gt;goto&lt;/code&gt; only works from inside a subroutine; there&#39;s an extra &lt;code&gt;main()&lt;/code&gt; here:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $code_ref = sub {
	print &quot;I&#39;m in the anonymous sub!\n&quot;;
	};
	
sub named_sub {
	print &quot;I&#39;m in a named subroutine!\n&quot;;
	}
	
sub main {
	LINE: while( &amp;lt;DATA&amp;gt; ) {
		chomp;
		goto &#39;LINE&#39; if $_ eq &#39;skipline&#39;;

		goto $code_ref if $_ eq &#39;coderef&#39;;
		
		goto \&amp;amp;named_sub if $_ eq &#39;named_sub&#39;;
		print &quot;$_\n&quot;;
		}
	}

main();

__DATA__
this is line 1
here&#39;s line 2
skipline
and the third line
coderef
fourth line
named_sub
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Once you &lt;code&gt;goto&lt;/code&gt; a subroutine reference, however, you don&#39;t come back to the loop. The program doesn&#39;t resume the &lt;code&gt;while&lt;/code&gt; after it reads the &lt;code&gt;coderef&lt;/code&gt; line:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.8.9 goto.pl
this is line 1
here&#39;s line 2
and the third line
I&#39;m in the anonymous sub!
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Perl v5.20 fixes taint problems with locale</title>
		<link>https://www.effectiveperlprogramming.com/2014/09/perl-v5-20-fixes-taint-problems-with-locale/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/09/perl-v5-20-fixes-taint-problems-with-locale/</guid>
		<pubDate>Tue, 30 Sep 2014 04:28:47 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl v5.20 fixes taint checking in regular expressions that might use the locale in its pattern, even if that part of the pattern isn&#39;t a successful part of the match. The &lt;a href=&quot;https://perldoc.perl.org/perlsec&quot;&gt;perlsec&lt;/a&gt; documentation has noted that taint-checking did that, but until v5.20, it didn&#39;t. 
&lt;/p&gt;



&lt;p&gt;
The only approved way to untaint a variable is through a successful pattern match with captures: 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $tainted = ...;

$tainted =~ m/\A (\w+ ) \z/x;
my $untainted = $1;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The problem is &lt;code&gt;\w&lt;/code&gt;. Which characters does that match? I discussed this in &lt;a href=&quot;/2011/01/know-your-character-classes/&quot;&gt;Know your character classes under different semantics&lt;/a&gt; although I didn&#39;t focus on &lt;code&gt;\w&lt;/code&gt;.
&lt;/p&gt;



&lt;p&gt;
With &lt;code&gt;use locale&lt;/code&gt;, settings from outside the program decide what &lt;code&gt;\w&lt;/code&gt; matches. If you don&#39;t set the locale yourself, someone is setting it for you, possibly letting character classes match what you didn&#39;t intend (or even know about). That&#39;s contrary to the spirit of the advice in &lt;a href=&quot;https://perldoc.perl.org/perlsec&quot;&gt;perlsec&lt;/a&gt;:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
you must be exceedingly careful with your patterns.
&lt;/blockquote&gt;


&lt;p&gt;
If something doesn&#39;t have a knowable meaning and you use it, you aren&#39;t being &quot;exceedingly careful.&quot; &lt;a href=&quot;https://perldoc.perl.org/perlsec&quot;&gt;perlsec&lt;/a&gt; recommends using &lt;code&gt;no locale&lt;/code&gt; to fix this. It&#39;s better to have a better pattern that doesn&#39;t go anywhere near locale issues.
&lt;/p&gt;



&lt;p&gt;
I cover this in greater detail in the &quot;Secure Programming Techniques&quot; in &lt;span class=&quot;book-title&quot;&gt;Mastering Perl&lt;/span&gt;.
&lt;/p&gt;



&lt;p&gt;
The &lt;a href=&quot;https://perldoc.perl.org/perlsec&quot;&gt;perlsec&lt;/a&gt; example is exceedingly non-careful now with recent versions of Perl. Here is the example from the v5.20 docs, which has been the example since at least v5.003 (released in 1996):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt; if ($data =~ /^([-\@\w.]+)$/) {
	$data = $1; 			# $data now untainted
    } else {
	die &quot;Bad data in &#39;$data&#39;&quot;; 	# log this somewhere
    }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You already know the problem with the &lt;code&gt;\w&lt;/code&gt;. What are &lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt;? If you took my &lt;i&gt;&lt;a href=&quot;https://www.learning-perl.com/&quot;&gt;Learning Perl&lt;/a&gt;&lt;/i&gt; class, you know they are the beginning- and end-of-&lt;i&gt;line&lt;/i&gt; anchors. Without any flags, the target string is one line. With the &lt;code&gt;/m&lt;/code&gt; flag, they can match after or before a new line, respectively. They operate differently and you might not get the behavior that you want. 
&lt;/p&gt;



&lt;p&gt;
Prior to v5.14 (more correctly, the version of &lt;code&gt;re&lt;/code&gt; that came with it), you had to put those flags with the &lt;code&gt;qr//&lt;/code&gt; operator to compile the pattern or the operator that uses the pattern (see &lt;a href=&quot;/2010/04/know-the-difference-between-regex-and-match-operator-flags/&quot;&gt;Know the difference between regex and match operator flags&lt;/a&gt;).
&lt;/p&gt;



&lt;p&gt;
For v5.14 and later, the &lt;code&gt;re&lt;/code&gt; module allows you to set default flags that apply to all patterns in its lexical scope (see &lt;a href=&quot;/2011/06/set-default-regular-expression-modifiers/&quot;&gt;Set default regular expression modifiers&lt;/a&gt;). The flags might set something you didn&#39;t intend to use and don&#39;t show up near the code you care about. Those flags might not be there when you write the code, but someone adds them later if they have a fit of &quot;modern Perl&quot; or &lt;span class=&quot;book-title&quot;&gt;Perl Best Practices&lt;/span&gt; fever.
&lt;/p&gt;



&lt;p&gt;
My guiding principle, though, is that any string which doesn&#39;t exactly match what you expect isn&#39;t safe enough to be untainted. If you mean the beginning or end of the absolute string, use the anchors that can only mean the beginning and end of string anchors, &lt;code&gt;\A&lt;/code&gt; and &lt;code&gt;\z&lt;/code&gt; (&lt;span class=&quot;item&quot;&gt;Item 35: Use zero-width assertions to match positions in a string&lt;/span&gt;).
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The locale can change the meaning of character classes&lt;/li&gt;
&lt;li&gt;Default regex flags change behavior from a distance&lt;/li&gt;
&lt;li&gt;Don&#39;t use character classes in regular expressions you use to untaint values&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;\A&lt;/code&gt; and &lt;code&gt;\z&lt;/code&gt; for the absolute beginning
and end of string&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use postfix dereferencing</title>
		<link>https://www.effectiveperlprogramming.com/2014/09/use-postfix-dereferencing/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/09/use-postfix-dereferencing/</guid>
		<pubDate>Mon, 29 Sep 2014 23:17:09 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
&lt;a href=&quot;/2016/04/postfix-dereferencing-is-stable-is-v5-24/&quot;&gt;This feature became stable in Perl v5.24&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl v5.20 offers an experimental form of dereferencing. Instead of the complicated way I&#39;ll explain in the moment, the new &lt;i&gt;postfix&lt;/i&gt; turns a reference into its contents. Since this is a new feature, you need to pull it in with the &lt;code&gt;feature&lt;/code&gt; pragma (although this feature is undocumented in the pragma docs) (&lt;span class=&quot;item&quot;&gt;Item 2. Enable new Perl features when you need them.&lt;/span&gt;) and turn off the &lt;a href=&quot;/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/&quot;&gt;experimental warnings&lt;/a&gt;: 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;
use feature qw(postderef);
no warnings qw(experimental::postderef);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
To turn an array reference into its elements, after the arrow &lt;code&gt;-&amp;gt;&lt;/code&gt; you use &lt;code&gt;@&lt;/code&gt; with a star, &lt;code&gt;*&lt;/code&gt; after it:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @elements = $array_ref-&amp;gt;@*;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This also works with the anonymous array constructor, although you would be wrapping that package just to unwrap it. Sometimes consistent syntax is like that:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @elements = [ qw(cat bird dog) ]-&amp;gt;@*;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In prior versions, you have to use the &lt;a href=&quot;#circumfix&quot;&gt;circumfix notation&lt;/a&gt; to do the same thing:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @elements = @$array_ref;
my @elements = @{ $array_ref };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The new notation is handy for things such as &lt;code&gt;foreach&lt;/code&gt; which needs a list:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;foreach my $element ( $array_ref-&amp;gt;@* ) {
	...
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This feature is more interesting when the reference doesn&#39;t come from a variable and you can&#39;t conveniently use the &lt;a href=&quot;#circumfix&quot;&gt;circumfix notation&lt;/a&gt;. Perhaps the reference is the return value of a subroutine:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub some_sub {
	state $ref = [ qw(cat dog bird) ];
	$ref;
	}

foreach my $element ( some_sub()-&amp;gt;@* ) {
	...
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
This works for all of the reference types, although the subroutine version is a bit weird. I&#39;ll get to the &lt;a href=&quot;#subroutine&quot;&gt;subroutine postfix dereference&lt;/a&gt; later.
&lt;/p&gt;



&lt;p&gt;
If you use a subscripty thing after the postfix sigil, you get a slice, even when you ask for a single element:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $first = some_sub()-&amp;gt;@[0];
my @slice = some_sub()-&amp;gt;@[0,-1];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The single element access is not that interesting because you can already do that without the postfix sigil:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $first = some_sub()-&amp;gt;[0];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Here&#39;s an interesting bit of Perl syntax. What happens when you put two indices in that?
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $first ) = some_sub()-&amp;gt;[0, -1];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Perl doesn&#39;t think that&#39;s a slice so it treats the expression inside the braces in scalar context. The comma in scalar context evaluates the left hand side, discards the result, evaluates the right hand side, and returns the result. It&#39;s the same thing as the behavior in the FAQ &lt;a href=&quot;https://perldoc.perl.org/perlfaq4#What-is-the-difference-between-a-list-and-an-array%3f&quot;&gt;What is the difference between a list and an array?&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
In this code, only &lt;code&gt;$first&lt;/code&gt; gets a value, but it&#39;s from the last subscript, while &lt;code&gt;$second&lt;/code&gt; gets none:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $first, $second ) = some_sub()-&amp;gt;[ 
	do { say &quot;Evaluated!&quot;; 0 }, 1 
	]; 
say &quot;First is $first&quot;;
say &quot;Second is $second&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Perl evaluates the first &quot;subscript&quot;, but doesn&#39;t use it. It uses the last result in the comma chain, even though the assignment looks like it&#39;s in list context.
&lt;/p&gt;



&lt;p&gt;
Wedge that postfix &lt;code&gt;@&lt;/code&gt; in there and both &lt;code&gt;$first&lt;/code&gt; and &lt;code&gt;$second&lt;/code&gt; get values:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $first, $second ) = some_sub()-&amp;gt;@[ 
	do { say &quot;Evaluated!&quot;; 0 }, 1 
	]; 
say &quot;First is $first&quot;;
say &quot;Second is $second&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Don&#39;t make the common mistake of associating the &lt;code&gt;@&lt;/code&gt; with an array. Just like the array and hash slices, Perl figures out the type with the subscripty braces.
&lt;/p&gt;



&lt;p&gt;
A postfix hash slice still has the &lt;code&gt;@&lt;/code&gt; and gives back a list of values:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub get_hashref {
	state $ref = { 
		cat  =&amp;gt; &#39;Buster&#39;,
		dog  =&amp;gt; &#39;Addy&#39;,
		bird =&amp;gt; &#39;Poppy&#39;,
		};
	$ref;
	}

my( $first, $second ) = get_hashref()-&amp;gt;@{ qw(cat dog) }; 
say &quot;First is $first&quot;;
say &quot;Second is $second&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Curiously, in all of this, the subroutine with the postfix dereference is always called in scalar context despite what you are doing with the result or how you are assigning it. A reference is always a scalar, and that&#39;s a single thing.
&lt;/p&gt;

&lt;h2&gt;Scalar and array interpolation&lt;/h2&gt;

&lt;p&gt;
You can interpolate the postfix dereference notation with scalar and array references if you enable the &lt;code&gt;postderef_qq&lt;/code&gt; feature (also undocumented in the pragma docs):
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

use feature qw(postderef postderef_qq);
no warnings qw(experimental::postderef);


my $scalarref = \ &#39;This is the string&#39;;
say &quot;Scalar is &amp;lt; $scalarref-&amp;gt;$* &amp;gt;&quot;;
my $arrayref = [ qw(cat bird dog) ];

say &quot;Array is &amp;lt; $arrayref-&amp;gt;@* &amp;gt;&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Notice that without the &lt;code&gt;postderef&lt;/code&gt; feature, Perl would try to interpolate the &lt;code&gt;$*&lt;/code&gt; (a Perl 4 multi-line feature removed in v5.10).
&lt;/p&gt;



&lt;p&gt;
I&#39;ve only found one useful demonstration of the postfix scalar dereference. &lt;a href=&quot;https://docs.mojolicious.org/Mojo/JSON&quot;&gt;Mojo::JSON&lt;/a&gt; uses references to the literals 0 and 1 to represent JSON&#39;s &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;. The postfix dereference gets you the boolean value:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

use experimental qw(postderef);

use Encode qw(encode);
use Mojo::JSON qw(decode_json);
use Mojo::Util qw(dumper);

my $raw_octets = encode( &#39;UTF-8&#39;,
	&#39;{&quot;name&quot;:&quot;Bender&quot;,&quot;robot&quot;:true}&#39; );

my $hash = decode_json( $raw_octets );

say &quot;$hash-&amp;gt;{name} is a robot? &quot;,
	$hash-&amp;gt;{robot}-&amp;gt;$* ? &#39;Yes&#39; : &#39;No&#39;;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;circumfix&quot;&gt;Circumfix notation&lt;/h2&gt;

&lt;p&gt;
To appreciate the ease of the postfix dereference notation, you should understand the &lt;i&gt;circumfix notation&lt;/i&gt; which earlier versions use. I cover this in &lt;span class=&quot;book-title&quot;&gt;Intermediate Perl&lt;/span&gt;, but here&#39;s a short explanation.
&lt;/p&gt;



&lt;p&gt;
Remember the notation for an array variable. There&#39;s a sigil to give some context, an identifier (the name), and possible subscripts:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SIGIL  IDENTIFIER   SUBSCRIPT
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
In Perl, you can put whitespace between those parts and it still works, including with the new &lt;a href=&quot;/2014/07/perl-5-20-introduces-keyvalue-slices/&quot;&gt;key/value slice syntax&lt;/a&gt;:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;@ IDENTIFIER
$ IDENTIFIER [ 0 ]
@ IDENTIFIER [ @indices ]
% IDENTIFIER [ @indices ]

% IDENTIFIER
$ IDENTIFIER { &#39;cat&#39; }
@ IDENTIFIER { @keys }
% IDENTIFIER { @keys } 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You can replace &lt;code&gt;IDENTIFIER&lt;/code&gt;	with a reference. This is the circumfix notation, called that because there&#39;s stuff &lt;em&gt;around&lt;/em&gt; the thing you&#39;re dereferencing. The general syntax uses braces around the reference, which, like in the previous section, might be something that produces a reference and not a variable:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;@ { REFERENCE }
$ { REFERENCE } [ 0 ]
@ { REFERENCE } [ @indices ]
% { REFERENCE } [ @indices ]

% { REFERENCE }
$ { REFERENCE } { &#39;cat&#39; }
@ { REFERENCE } { @keys }
% { REFERENCE } { @keys } 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
If the &lt;code&gt;REFERENCE&lt;/code&gt; is a simple scalar variable (not a single element access, a subroutine call, or something else), you can omit the braces:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;@ $array_ref
$ $array_ref [ 0 ]
@ $array_ref [ @indices ]
% $array_ref [ @indices ]

% $hash_ref
$ $hash_ref { &#39;cat&#39; }
@ $hash_ref { @keys }
% $hash_ref { @keys } 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Now, suppose you have an array of arrays of arrays of hashes, just to look at something complicated:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $array_ref = [
	[
		[
			{ animal =&amp;gt; &#39;cat&#39;, name =&amp;gt; &#39;Buster&#39; },
		]
		...
	],
	...
	];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
To get the first element of the array reference, you can dereference with the circumfix notation. This gives you the first array reference under the top level reference, leaving off the braces because the top level is in a simple scalar variable:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$array_ref[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
To get the first item from that reference, you have to use the braces since that&#39;s not a simple scalar variable:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;${ $array_ref[0] }[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
To get all the elements from that reference, you add more braces:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;@{ ${ $array_ref[0] }[0] }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
It&#39;s the same with the arrow notation (even implied!) which makes the inside only slightly prettier but still needs the outside braces:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;@{ $array_ref-&amp;gt;[0]-&amp;gt;[0] }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You can leave off the arrows between subscripts:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;@{ $array_ref-&amp;gt;[0][0] }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
It&#39;s no wonder some people think Perl is ugly.
&lt;/p&gt;



&lt;p&gt;
The postfix dereferencing looks nicer because it doesn&#39;t use the braces, but it also keeps everything in order from left to right:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$array_ref-&amp;gt;[0][0]-&amp;gt;@*
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You need that extra arrow at the end; it&#39;s a syntax error otherwise:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$array_ref-&amp;gt;[0][0]@*        # syntax error!
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;subroutine&quot;&gt;Subroutine postfix dereference&lt;/h2&gt;

&lt;p&gt;
The postfix dereference for a subroutine is slightly odd because there are different ways that you can call a subroutine, but the postfix dereference uses one of the uncommon ones.
&lt;/p&gt;



&lt;p&gt;
Start with a named subroutine. With just the &lt;code&gt;&amp;amp;&lt;/code&gt; and no parentheses, the called subroutine gets the arguments in the current version of &lt;code&gt;@_&lt;/code&gt;:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;local @_ = qw(Buster Mimi Ginger);

sub some_sub { &quot;@_&quot; }

say &quot;With &amp;amp;: &quot;, &amp;amp;some_sub;     # With &amp;amp;: Buster Mimi Ginger
say &quot;With &amp;amp;(): &quot;, &amp;amp;some_sub(); # With &amp;amp;():
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The postfix dereference form has the same behavior. It executes the subroutine reference with the current &lt;code&gt;@_&lt;/code&gt;. Dereferencing the sub reference with &lt;code&gt;-&amp;gt;()&lt;/code&gt; is different; it specifies an empty argument list:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;

use feature qw(postderef);
no warnings qw(experimental::postderef);

my $sub = sub { &quot;@_&quot; };

local @_ = qw(Buster Mimi Ginger);
say $sub-&amp;gt;&amp;amp;*;   # Buster Mimi Ginger
say $sub-&amp;gt;();   #
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Things to remember&lt;/h2&gt;
&lt;ul&gt;
 	&lt;li&gt;Perl v5.20 adds a postfix notation to dereference all variable types&lt;/li&gt;
 	&lt;li&gt;Subroutines returning references are called in scalar context&lt;/li&gt;
 	&lt;li&gt;Subroutine references dereferenced with the postfix notation use the current value of &lt;code&gt;@_&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl v5.20 combines multiple my() statements</title>
		<link>https://www.effectiveperlprogramming.com/2014/08/perl-v5-20-combines-multiple-my-statements/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/08/perl-v5-20-combines-multiple-my-statements/</guid>
		<pubDate>Wed, 13 Aug 2014 19:00:14 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl v5.20 continues to clean up and optimize its internals. Now &lt;code&gt;perl&lt;/code&gt; optimizes a series of lexical variable declarations into a single list declaration. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You can write them out as separate statements:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $dog );
my( $cat );
my( $bird );
&lt;/code&gt;&lt;/pre&gt;

v5.20 turns that into a single declaration.

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my( $dog, $cat, $bird );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That doesn&#39;t sound like a big deal, but it saves several steps, and steps translate to time no matter what they do. 
&lt;/p&gt;



&lt;p&gt;
Remember where you&#39;re likely to declare lexical variables—at the start of subroutines. Call the subroutine enough times and those little bits of time add up:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub foo {
	my( $dog );
	my( $cat );
	my( $bird );
	
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With v5.20, either way you write it you get the faster form:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub foo {
	my( $dog, $cat, $bird );	
	...
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If I&#39;m not initializing variables, I initialize with a list anyway, but I tend to declare variables as close to their use as I can get away with, which often means that I don&#39;t list them at the top of the subroutine. Writing short subroutines, however, means I probably have them near the top.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>In v5.20, -F implies -a implies -n</title>
		<link>https://www.effectiveperlprogramming.com/2014/08/in-v5-20-f-implies-a-implies-n/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/08/in-v5-20-f-implies-a-implies-n/</guid>
		<pubDate>Tue, 12 Aug 2014 21:04:32 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl was once known for its one-liners in its sysadmin heydays. People would pass around lists of these one liners, many of which replaced complicated pipelines that glued together various unix utilities to do some impressive system maintenance. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
 	&lt;li&gt;&lt;code&gt;-a&lt;/code&gt; splits the input line on whitespace and puts the result in &lt;code&gt;@F&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code&gt;-n&lt;/code&gt; adds &lt;code&gt;while( &amp;lt;&amp;gt; ) { ... }&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;The &lt;code&gt;-F&lt;/code&gt; specifies what &lt;code&gt;-a&lt;/code&gt; should split on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
You could write a program to read a line, split on whitespace, and print the elements:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;while( &amp;lt;&amp;gt; ) {
	@F = split;
	print &quot;@F\n&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
As a one-liner, that program is much shorter:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -a -ne &#39;print &quot;@F\n&quot;&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
If you want to change the split pattern, you use &lt;code&gt;-F&lt;/code&gt;. For instance, you have a line with fields separated by colons:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;cat:bird:dog:lizard:fox
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You use &lt;code&gt;-F&lt;/code&gt; to set the colon as the split pattern:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -aF: -ne &#39;print &quot;@F\n&quot;&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Prior to v5.20, you had to specify each of those switches, even though they implied each other. The &lt;code&gt;-F&lt;/code&gt; switch only makes sense with &lt;code&gt;-a&lt;/code&gt;, and &lt;code&gt;-a&lt;/code&gt; needs something to put data into &lt;code&gt;$_&lt;/code&gt;, hence &lt;code&gt;-n&lt;/code&gt;.
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.20 -F: -le &#39;print join &quot;=&quot;, @F&#39; input.txt
&lt;/code&gt;&lt;/pre&gt;
(The &lt;code&gt;-l&lt;/code&gt; adds the newline after the &lt;code&gt;print&lt;/code&gt;.)


&lt;p&gt;
If you don&#39;t need the &lt;code&gt;-F&lt;/code&gt;, the &lt;code&gt;-a&lt;/code&gt; still implies the &lt;code&gt;-n&lt;/code&gt;:
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.20 -ale &#39;print join &quot;=&quot;, @F&#39; input.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
You can see what a one-liner does by deparsing it (&lt;a class=&quot;item&quot; href=&quot;/2010/05/use-bdeparse-to-see-what-perl-thinks-the-code-is/&quot;&gt;Use B::Deparse to see what perl thinks the code is&lt;/a&gt;):
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.20 -MO=Deparse -F: -le &#39;print join &quot;=&quot;, @F&#39; input.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The code comments I added myself:
&lt;/p&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;BEGIN { $/ = &quot;\n&quot;; $\ = &quot;\n&quot;; }
LINE: while (defined($_ = &amp;lt;ARGV&gt;)) {     # from -n
    chomp $_;                            # from -n
    our(@F) = split(/:/, $_, 0);         # @F from -a, /:/ from -F
    print join(&#39;=&#39;, @F);                 # argument to -e
}                                        # from -n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
To make this new feature work reliably, you have to ensure that you are using at least v5.20. If someone tries your shortened one-liner with an earlier version, it won&#39;t work. That&#39;s the danger with using the new features in any version. Specifying the version negates the saved typing since it also requires you to declare &lt;code&gt;@F&lt;/code&gt; since declaring a minimum version also &lt;a href=&quot;/2010/08/implicitly-turn-on-strictures-with-perl-5-12/&quot;&gt;turns on &lt;code&gt;strict&lt;/code&gt; since v5.12&lt;/a&gt;.
&lt;/p&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -M5.20.0 -Mvars=@F -lne &#39;print join &quot;=&quot;, @F&#39; input.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Further Reading&lt;/h2&gt;
&lt;ul&gt;
 	&lt;li&gt;&lt;span class=&quot;person&quot;&gt;Randal Schwartz&lt;/span&gt; writes about &lt;code&gt;-F&lt;/code&gt; in his &lt;a href=&quot;https://stonehenge.com/merlyn/UnixReview/col01&quot;&gt;first &lt;span class=&quot;magazine-title&quot;&gt;Unix Review&lt;/span&gt; column&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code&gt;perl&lt;/code&gt;&#39;s run time switches are documented in &lt;a href=&quot;https://perldoc.perl.org/perlrun&quot;&gt;perlrun&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl 5.20 introduces &quot;Key/Value Slices&quot;</title>
		<link>https://www.effectiveperlprogramming.com/2014/07/perl-5-20-introduces-keyvalue-slices/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/07/perl-5-20-introduces-keyvalue-slices/</guid>
		<pubDate>Thu, 03 Jul 2014 18:52:43 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl v5.20 adds the &quot;Key/Value Slice,&quot; which extracts multiple keys and their corresponding values from a container (hash or array). It uses the &lt;code&gt;%&lt;/code&gt;, which is new, legal syntax for a variable name with subscripts after it: 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;  # you don&#39;t need this for the new syntax

my %smaller_hash = %big_hash{ @keys };
my %index_hash   = %big_array[ @indices ];
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
As with the &lt;code&gt;@&lt;/code&gt; sigil, you know the variable type you&#39;re dealing with by the indexing syntax after it. The &lt;code&gt;%&lt;/code&gt; does not signify a hash; it denotes that you are getting the index (key) with the value.
&lt;/p&gt;



&lt;p&gt;
Looking at that example, you might mistakenly think that these slices return hashes. They don&#39;t. They return lists which have an index then a value. That&#39;s pairwise like the list representation of a hash, but it can also repeat keys (which hashes can&#39;t do):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.20;  # you don&#39;t need this for the new syntax

my %big_hash     = qw( cat Buster dog Addy bird Poppy );

my @array = %big_hash{ qw(cat cat) };

say &quot;@array&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The resulting list duplicates the entries for &lt;code&gt;cat&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;cat Buster cat Buster
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This new type of slice returns a list, which is just data (&lt;i&gt;i.e. not a variable&lt;/i&gt;). That means you can&#39;t use the hash or array operators on the result, which would be a neat trick. You can&#39;t take a reference to the entire result, because that&#39;s the same as taking a reference to a list to get a list of references. The result is not an lvalue, so you can&#39;t assign to it or modify it directly.
&lt;/p&gt;



&lt;p&gt;
Previous to v5.20, you can do the same thing with a &lt;code&gt;map&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my @array = map { $_ =&gt; $big_hash{$_} } @keys;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s not that bad, making this new feature less than compelling. Use it if you need v5.20 for something else, but don&#39;t make this feature the one that forces people to upgrade.
&lt;/p&gt;


&lt;h2&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.nntp.perl.org/group/perl.perl5.porters/2013/07/msg204641.html&quot;&gt;&lt;span class=&quot;person&quot;&gt;Ruslan Zakirov&lt;/span&gt;&#39;s perl5-porters summary of the feature&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl 5.20 optimizes return at the end of a subroutine</title>
		<link>https://www.effectiveperlprogramming.com/2014/06/perl-5-20-optimizes-return-at-the-end-of-a-subroutine/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/06/perl-5-20-optimizes-return-at-the-end-of-a-subroutine/</guid>
		<pubDate>Thu, 12 Jun 2014 21:07:38 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Want to save 10 nanoseconds? Perl v5.20 optimizes a &lt;code&gt;return&lt;/code&gt; at the end of a subroutine to use two fewer ops in the optimized version. During compilation, a subroutine like this one: 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub some_sub { ...; return $foo }
&lt;/code&gt;&lt;/pre&gt;

turns into a subroutine like this one, without the &lt;code&gt;return&lt;/code&gt;:

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub some_sub { ...; $foo }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can see the difference in the output from the &lt;a href=&quot;https://metacpan.org/pod/B::Concise&quot;&gt;B::Concise&lt;/a&gt; module (which you can use through the &lt;code&gt;O&lt;/code&gt; frontend). Prior to v5.20, there are five steps to return the first argument:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.18.0 -MO=Concise,baz,-exec -e &#39;sub baz { return $_[0] }&#39;
main::baz:
1  &amp;lt;;&gt; nextstate(main 1 -e:1) v
2  &amp;lt;0&gt; pushmark s
3  &amp;lt;$&gt; aelemfast(*_) s
4  &amp;lt;@&gt; return K
5  &amp;lt;1&gt; leavesub[1 ref] K/REFC,1
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
But, prior to v5.20, if you didn&#39;t use the &lt;code&gt;return&lt;/code&gt; keyword, there are only three steps because the &lt;code&gt;PUSHMARK&lt;/code&gt; isn&#39;t there:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.18.0 -MO=Concise,baz,-exec -e &#39;sub baz { $_[0] }&#39;
main::baz:
1  &amp;lt;;&gt; nextstate(main 1 -e:1) v
2  &amp;lt;$&gt; aelemfast(*_) s
3  &amp;lt;1&gt; leavesub[1 ref] K/REFC,1
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can read about &lt;code&gt;PUSHMARK&lt;/code&gt; in the &lt;a href=&quot;https://perldoc.perl.org/perlcall&quot;&gt;perlcall&lt;/a&gt; documentation. It&#39;s a signal to &lt;code&gt;perl&lt;/code&gt; to remember where the current stack pointer is.
&lt;/p&gt;




&lt;p&gt;
With v5.20, &lt;code&gt;perl&lt;/code&gt; optimizes the &lt;code&gt;return&lt;/code&gt; version to have the same steps:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.20.0 -MO=Concise,baz,-exec -e &#39;sub baz { return $_[0] }&#39;
main::baz:
1  &amp;lt;;&gt; nextstate(main 1 -e:1) v
2  &amp;lt;$&gt; aelemfast(*_) s
3  &amp;lt;1&gt; leavesub[1 ref] K/REFC,1
-e syntax OK

$ perl5.20.0 -MO=Concise,baz,-exec -e &#39;sub baz { $_[0] }&#39;
main::baz:
1  &amp;lt;;&gt; nextstate(main 1 -e:1) v
2  &amp;lt;$&gt; aelemfast(*_) s
3  &amp;lt;1&gt; leavesub[1 ref] K/REFC,1
-e syntax OK
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;
This will be happy news to people who stick to &lt;span class=&quot;book-title&quot;&gt;Perl Best Practices&lt;/span&gt;, which recommends that you always use an explicit &lt;code&gt;return&lt;/code&gt;. &lt;span class=&quot;person&quot; data-fullname=&quot;Damian Conway&quot;&gt;Damian&lt;/span&gt;&#39;s recommendation is to denote intent, but now you don&#39;t suffer if it&#39;s the last statement in the subroutine.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl 5.20 uses its own random number generator</title>
		<link>https://www.effectiveperlprogramming.com/2014/06/perl-5-20-uses-its-own-random-number-generator/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/06/perl-5-20-uses-its-own-random-number-generator/</guid>
		<pubDate>Wed, 11 Jun 2014 17:49:41 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Prior to v5.20, &lt;code&gt;perl&lt;/code&gt; used whatever random number generator the system provided. This meant that the same program could have statistically different results based on the quality of that function. &lt;a href=&quot;https://learn.microsoft.com/en-us/previous-versions/398ax69y(v=vs.140)&quot;&gt;The &lt;code&gt;rand()&lt;/code&gt; for Windows&lt;/a&gt; had a max of 32,768 (15 bits), while POSIX has &lt;a href=&quot;https://pubs.opengroup.org/onlinepubs/007908799/xsh/drand48.html&quot;&gt;&lt;code&gt;drand48&lt;/code&gt;&lt;/a&gt; (48 bits). This sort of numerical un-portability has always been a problem with &lt;code&gt;perl&lt;/code&gt; since it&#39;s relied on the underlying &lt;i&gt;libc&lt;/i&gt; for so much. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;figure class=&quot;text-center&quot;&gt;
&lt;img src=&quot;https://farm9.staticflickr.com/8507/8520562112_0d4ec43d66.jpg&quot; width=&quot;500&quot; height=&quot;375&quot; alt=&quot;A printed page from a random sequence generator listing the numbers 1 through 100 in random order&quot; class=&quot;figure-img img-fluid&quot;&gt;
&lt;figcaption class=&quot;figure-caption&quot;&gt;One hundred random numbers&lt;/figcaption&gt;
&lt;/figure&gt;



&lt;p&gt;
Not any more. It&#39;s all internal to &lt;code&gt;perl&lt;/code&gt; now. With v5.20 and beyond, you&#39;ll get the same pseudorandom number generator that everyone else with v5.20 and later gets.
&lt;/p&gt;



&lt;p&gt;
For the effective programmer though, this doesn&#39;t really matter because you shouldn&#39;t be using the pseudorandom number generator for anything important. We call it &lt;code&gt;rand&lt;/code&gt;, but it&#39;s not really. We should have called it &lt;code&gt;fake_random&lt;/code&gt;, &lt;code&gt;good_enough_random&lt;/code&gt;, or &lt;code&gt;i_wont_install_a_module_so_ill_deal_with_it_random&lt;/code&gt;. The name in Perl comes from the name in &lt;i&gt;libc&lt;/i&gt; (&lt;i&gt;e.g.&lt;/i&gt; &lt;a href=&quot;https://www.gnu.org/software/libc/manual/html_node/Function-Index.html&quot;&gt;the GNU libc function list&lt;/a&gt;), just like many of the oddly named functions such as &lt;code&gt;abs&lt;/code&gt;, &lt;code&gt;chmod&lt;/code&gt;, or &lt;code&gt;getgrent&lt;/code&gt;. From the name, we get sloppy talking about its output as &quot;random numbers&quot; instead of the correct &quot;pseudorandom&quot; number.
&lt;/p&gt;



&lt;p&gt;
&lt;span class=&quot;person&quot;&gt;Sinan Ünür&lt;/span&gt; examines how well Perl&#39;s &lt;code&gt;rand&lt;/code&gt; does with coin flips and concludes it comes up short (&lt;a href=&quot;https://www.nu42.com/2014/05/perl-5200-brings-better-prng-to-windows.html&quot;&gt;Perl 5.20.0 brings a &quot;better&quot; PRNG to Windows&lt;/a&gt;). An older presentation from the Wellington Perl mongers goes through some &lt;a href=&quot;https://wellington.pm.org/archive/200704/randomness/#slide0&quot;&gt;serious math to talk about better pseudorandom numbers&lt;/a&gt;. The documentation for &lt;a href=&quot;https://metacpan.org/pod/Math::Random::Secure&quot;&gt;Math::Random::Secure&lt;/a&gt; has more interesting details. Several other modules provide &lt;code&gt;rand&lt;/code&gt; replacements.
&lt;/p&gt;



&lt;p&gt;
There are ways to get real random numbers. Atmospheric noise, nuclear decay, and other processes are random and their measurement can supply the numbers. The &lt;a href=&quot;https://www.random.org&quot;&gt;random.org&lt;/a&gt; website, for one, can supply these, and the &lt;a href=&quot;https://metacpan.org/pod/Net::Random&quot;&gt;Net::Random&lt;/a&gt; module makes the connection for you.
&lt;/p&gt;



&lt;p&gt;
Even though &lt;code&gt;rand&lt;/code&gt; still isn&#39;t random, at least everyone can use the same thing without any extra work. I like it any time the &lt;code&gt;perl&lt;/code&gt; can bring this stuff inside to make it more portable.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl 5.20 new features</title>
		<link>https://www.effectiveperlprogramming.com/2014/06/perl-5-20-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2014/06/perl-5-20-new-features/</guid>
		<pubDate>Wed, 11 Jun 2014 16:03:21 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-20</category>
		<description>&lt;p&gt;
Perl 5.20 is out and there are some nice syntax changes that make life easier for Perlers, along with some improvements that don&#39;t require any work from you. Some of the features are experimental, so be careful that you don&#39;t create problems by overusing them until they settle down.
&lt;/p&gt;



&lt;p&gt;
You can download the &lt;a href=&quot;https://www.cpan.org/src/README.html&quot;&gt;Perl source from CPAN&lt;/a&gt;. For Windows, &lt;a href=&quot;https://strawberryperl.com&quot;&gt;Strawberry Perl 5.20&lt;/a&gt; is available now. The full details are in the &lt;i&gt;&lt;a href=&quot;//perldoc.perl.org/5.20.0/perldelta&quot;&gt;perldelta&lt;/a&gt;&lt;/i&gt;. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2015/04/use-v5-20-subroutine-signatures/&quot;&gt;Subroutine signatures (experimental)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/04/use-the-prototype-attribute/&quot;&gt;The &lt;code&gt;:prototype&lt;/code&gt; attribute&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/09/use-postfix-dereferencing/&quot;&gt;Postfix dereferencing&lt;/a&gt; (experimental)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/07/perl-5-20-introduces-keyvalue-slices/&quot;&gt;Slices that return the indices too (&quot;Key/Value Hash Slices&quot;, which is going to be confusing to distinguish from &quot;hash slices&quot; when we talk about them)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/09/perl-v5-20-fixes-taint-problems-with-locale/&quot;&gt;Fixes taint problems with locale&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The Standard Library is trimmed (CGI, Module::Build, find2perl, s2p, and a2p are gone)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/06/perl-5-20-optimizes-return-at-the-end-of-a-subroutine/&quot;&gt;A &lt;code&gt;return&lt;/code&gt; at the end of a subroutine is optimized away&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/08/perl-v5-20-combines-multiple-my-statements&quot;&gt;Adjacent &lt;code&gt;my&lt;/code&gt; declarations are combined&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/08/in-v5-20-f-implies-a-implies-n/&quot;&gt;On the command line, &lt;code&gt;-F&lt;/code&gt; implies &lt;code&gt;-a&lt;/code&gt;, and &lt;code&gt;-a&lt;/code&gt; implies &lt;code&gt;-n&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/06/perl-5-20-uses-its-own-random-number-generator/&quot;&gt;&lt;code&gt;rand&lt;/code&gt; has an internal generator so it&#39;s not platform or library dependent&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Declare packages outside of their block</title>
		<link>https://www.effectiveperlprogramming.com/2013/08/declare-packages-outside-of-their-block/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2013/08/declare-packages-outside-of-their-block/</guid>
		<pubDate>Sat, 03 Aug 2013 02:08:08 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-14</category>
		<description>&lt;p&gt;
Perl v5.14 gets a step closer to a saner way to declare classes with its new &lt;code&gt;package NAME BLOCK&lt;/code&gt; syntax that lets you easily group everything that goes in a package.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Previously, to limit a package you had to declare the package inside the scope:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;{
package Some::Class;
...
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That &lt;code&gt;...&lt;/code&gt; is another v5.12 new feature, the &lt;a href=&quot;/2015/08/the-new-yadda-yadda-operator/ &quot;&gt;yadda yadda operator&lt;/a&gt;. It&#39;s a compilable placeholder for statements to come later.
&lt;/p&gt;



&lt;p&gt;
Many people think that the &lt;code&gt;package&lt;/code&gt; statement changes the scope, but it doesn&#39;t (see &lt;a href=&quot;/2010/01/know-what-creates-a-scope/&quot;&gt;Know what creates a scope&lt;/a&gt;). It doesn&#39;t.
&lt;/p&gt;



&lt;p&gt;
Now you can put the &lt;code&gt;package&lt;/code&gt; statement outside the block so everything in the block is in that package:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.14;

package Some::Class {
...
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can also add a version too, like the &lt;a href=&quot;/2013/05/perl-v5-12-adds-the-package-name-version-syntax/&quot;&gt;package NAME VERSION&lt;/a&gt; syntax that v5.12 added:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.14;

package Some::Class 1.23 {
...
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This even works with nested classes:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.14;

package Local::foo {
	say &quot;1. Package is &quot; . __PACKAGE__;
	
	package Local::bar {
		say &quot;2. Package is &quot; . __PACKAGE__;
		}

	say &quot;3. Package is &quot; . __PACKAGE__;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is handy for small private classes that you don&#39;t want to put in a separate file. That also means another part of the code can&#39;t load it explicitly because there&#39;s no file that maps to its package name. That class is not actually hidden; it&#39;s merely obscure.
&lt;/p&gt;



&lt;p&gt;
And, if your editor supports it, you can also fold the block that contains the package. But then you may be able to fold naked blocks as well. 
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Experimental features now warn (reaching back to v5.10)</title>
		<link>https://www.effectiveperlprogramming.com/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/</guid>
		<pubDate>Sat, 08 Jun 2013 22:34:46 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
Perl 5.18 provides a new way to introduce experimental features in a program, augmenting the &lt;a href=&quot;https://perldoc.perl.org/feature&quot;&gt;feature&lt;/a&gt; pragma that v5.10 added. This change marks certain broken v5.10 features as experimental with an eye toward possible removal from the language.
&lt;/p&gt;



&lt;p&gt;
Smart matching in v5.10 led to several broken and conflated features. The &lt;code&gt;given&lt;/code&gt; used a lexical version of &lt;code&gt;$_&lt;/code&gt;, which broke many other common uses of that variable inside the &lt;code&gt;given&lt;/code&gt;, which I explain in &lt;a href=&quot;/2011/05/use-for-instead-of-given/&quot;&gt;Use for() instead of given()&lt;/a&gt; and you can see in &lt;a href=&quot;https://blogs.perl.org/users/komarov/2011/09/givenwhen-and-lexical.html&quot;&gt;given/when and lexical $_ ...&lt;/a&gt;. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Under v5.18, when you use &lt;code&gt;given&lt;/code&gt;, &lt;code&gt;when&lt;/code&gt;, or &lt;code&gt;~~&lt;/code&gt;, you get a warning, even if there is no smart match involved:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# given_warning.pl
use v5.10; # earliest occurance of feature
for( &#39;Buster&#39; ) { 
    when( 1 == 1 ) { say &quot;Hello&quot; } 
    }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
These warnings might cause test suites to fail when people try to install modules on the new &lt;code&gt;perl&lt;/code&gt;, like they do for &lt;a href=&quot;https://www.cpantesters.org/cpan/report/da727304-bfdd-11e2-9f21-918710d45c6b&quot;&gt;Unicode::Tussle&lt;/a&gt;.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.10.1 given_warning.pl
Hello
% perl5.18.0 given_warning.pl
when is experimental at given_warning.pl line 4.
Hello
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Using the &lt;a href=&quot;https://perldoc.perl.org/diagnostics&quot;&gt;diagnostics&lt;/a&gt; pragma shows the sort of warning it is:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.0 -Mdiagnostics given_warning.pl
when is experimental at -e line 1 (#1)
    (S experimental::smartmatch) when depends on smartmatch, which is
    experimental.  Additionally, it has several special cases that may
    not be immediately obvious, and their behavior may change or
    even be removed in any future release of perl.
    See the explanation under &quot;Experimental Details on given and when&quot;
    in perlsyn.
    
Hello
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To get rid of this warning, you do the same thing you do with other warnings. Take the category of the warning and turn it off with &lt;code&gt;no&lt;/code&gt; (&lt;span class=&quot;item&quot;&gt;Item 100: Use lexical warnings to selectively turn on or off complaints&lt;/span&gt;):
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# given_warning.pl
use v5.10; # earliest occurance of feature
no warnings &#39;experimental::smartmatch&#39;;
for( &#39;Buster&#39; ) { 
    when( 1 == 1 ) { say &quot;Hello&quot; } 
    }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The lexical &lt;code&gt;$_&lt;/code&gt; is another broken feature that&#39;s now marked as experimental.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# lexical_.pl
use v5.10;

sub cat { my $_ }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Any use in v5.18 gives a warning:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.0 lexical_.pl
Use of my $_ is experimental at lexcial_.pl line 3.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The category is different:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.0 -Mdiagnostics lexical_.pl
Use of my $_ is experimental at lexcial_.pl line 4 (#1)
    (S experimental::lexical_topic) Lexical $_ is an experimental 
    feature and its behavior may change or even be removed in any 
    future release of perl. See the explanation under &quot;$_&quot; in perlvar.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That takes care of the two retro features. Perl v5.18 introduces two new experimental features, set logic in character classes (for complete &lt;a href=&quot;https://www.unicode.org/reports/tr18/tr18-5.1.html#Basic%20Unicode%20Support&quot;&gt;Unicode Level 1 regular expression compliance&lt;/a&gt;), and lexical subroutines, which I&#39;ll cover in other items.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# regex.pl
use v5.18;

print &quot;Match&quot; if &#39;foo&#39; =~ /(?[ \p{Thai} &amp; \p{Digit} ])/;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Without turning off the warning, &lt;code&gt;perl&lt;/code&gt; knows about the feature and points it out:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.0 regex.pl
The regex_sets feature is experimental in regex; marked by &lt;-- HERE in m/(?[ &lt;-- HERE  \p{Thai} &amp; \p{Digit} ])/ at regex.pl line 4.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In this case, &lt;a href=&quot;https://perldoc.perl.org/diagnostics&quot;&gt;diagnostics&lt;/a&gt; is not any help:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.18.0 -Mdiagnostics regex.pl
The regex_sets feature is experimental in regex; marked by &lt;-- HERE in m/(?[
        &lt;-- HERE  \p{Thai} &amp; \p{Digit} ])/ at regex.pl line 3 (#1)
The regex_sets feature is experimental in regex; marked by &lt;-- HERE in m/(?[ &lt;-- HERE  \p{Thai} &amp; \p{Digit} ])/ at regex.pl line 3.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
For lexical named subroutines, you have to explicitly enable the feature but you then have to explicitly turn off its warnings.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# lexical_sub.pl
use v5.18;
no warnings &#39;experimental::lexical_subs&#39;;
use feature &quot;lexical_subs&quot;;

my sub foo { say &quot;Hello&quot; }
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Handling older perls&lt;/h2&gt;


&lt;p&gt;
In v5.18, that&#39;s all fine and good, but older versions don&#39;t understand those warnings categories and will stop your program.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.10.1 -e &#39;no warnings qw(smartmatch)&#39;
Unknown warnings category &#39;smartmatch&#39; at -e line 1
BEGIN failed--compilation aborted at -e line 1.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Instead of using &lt;a href=&quot;https://perldoc.perl.org/warnings&quot;&gt;warnings&lt;/a&gt;, you can use the non-core (until v5.20) &lt;a href=&quot;https://metacpan.org/pod/experimental&quot;&gt;experimental&lt;/a&gt; module that handles that for you:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use experimental qw(smartmatch);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
For versions without that warning category, nothing happens. For versions with that feature, it turns off the warning. 
&lt;/p&gt;



&lt;p&gt;
However, this is an additional dependency prior to v5.20. You should limit the features you use to the minimum version of Perl you allow.
&lt;/p&gt;


&lt;h2&gt;Summary&lt;/h2&gt;


&lt;p&gt;
This table summarizes the new experimental warnings categories and the features they affect.
&lt;/p&gt;




&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;Category&lt;/th&gt;
	&lt;th&gt;Features&lt;/th&gt;
&lt;/tr&gt;

&lt;tr&gt;
	&lt;td&gt;experimental::smartmatch&lt;/td&gt;
	&lt;td&gt;&lt;code&gt;given&lt;/code&gt;, &lt;code&gt;when&lt;/code&gt;, &lt;code&gt;~~&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	&lt;td&gt;experimental::lexical_topic&lt;/td&gt;
	&lt;td&gt;&lt;code&gt;my $_&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	&lt;td&gt;experimental::regex_sets&lt;/td&gt;
	&lt;td&gt;&lt;code&gt;(?[  ])&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	&lt;td&gt;experimental::lexical_subs&lt;/td&gt;
	&lt;td&gt;&lt;code&gt;my sub NAME {}&lt;/code&gt;, &lt;code&gt;our sub NAME {}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Some v5.10 features now warn under v5.18&lt;/li&gt;
&lt;li&gt;Some new experimental features must be explicitly enabled&lt;/li&gt;
&lt;li&gt;Even explicitly enabled features still warn&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://metacpan.org/pod/experimental&quot;&gt;experimental&lt;/a&gt; module is version safe&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Perl 5.18 new features</title>
		<link>https://www.effectiveperlprogramming.com/2013/06/perl-5-18-new-features/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2013/06/perl-5-18-new-features/</guid>
		<pubDate>Fri, 07 Jun 2013 17:20:53 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
Perl v5.18 is out and there are some major changes that you should know about before you upgrade. Most notably, some features from v5.10 are now marked experimental. If you use those features, you get warnings.
&lt;/p&gt;



&lt;p&gt;
You can download the &lt;a href=&quot;https://www.cpan.org/src/README.html&quot;&gt;Perl source from CPAN&lt;/a&gt;. For Windows, &lt;a href=&quot;https://strawberryperl.com&quot;&gt;Strawberry Perl 5.18&lt;/a&gt; is available now. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2013/06/experimental-features-now-warn-reaching-back-to-v5-10/&quot;&gt;Experimental features now warn (reaching back to v5.10)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2015/02/perl-v5-18-adds-character-class-set-operations/&quot;&gt;Perl regex character classes now have set operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2014/10/use-a-computed-label-with-loop-controllers/&quot;&gt;Use computed labels with loop controllers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2013/06/the-vertical-tab-is-part-of-s-in-perl-5-18/&quot;&gt;&lt;code class=&quot;regex&quot;&gt;\s&lt;/code&gt; now matches the vertical tab&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2016/03/no-more-no_match_vars/&quot;&gt;The new copy-on-write internal string management obviates English&#39;s &lt;code&gt;-no_match_vars&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;given&lt;/code&gt; now aliases &lt;code&gt;$_&lt;/code&gt; like &lt;code&gt;foreach&lt;/code&gt; does&lt;/li&gt;
&lt;li&gt;&lt;code&gt;readline&lt;/code&gt; now reads characters instead of octets&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CPANPLUS&lt;/code&gt; and &lt;code&gt;Archive::Extract&lt;/code&gt; are deprecated and marked for removal in v5.20.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;${^LAST_FH}&lt;/code&gt; refers to the last used filehandle.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>The vertical tab is part of \s in Perl 5.18</title>
		<link>https://www.effectiveperlprogramming.com/2013/06/the-vertical-tab-is-part-of-s-in-perl-5-18/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2013/06/the-vertical-tab-is-part-of-s-in-perl-5-18/</guid>
		<pubDate>Fri, 07 Jun 2013 17:18:52 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-18</category>
		<description>&lt;p&gt;
Up to v5.18, the vertical tab wasn&#39;t part of the &lt;code class=&quot;regex&quot;&gt;\s&lt;/code&gt; character class shortcut for ASCII whitespace. No one really knows why. It was curious trivia that I pointed out in &lt;a href=&quot;/2011/01/know-your-character-classes/&quot;&gt;Know your character classes under different semantics&lt;/a&gt;. Whitespace in ASCII, POSIX, and Unicode represented different sets. Perl whitespace was different from POSIX whitespace by only the exclusion of the vertical tab. Now that little oversight is fixed. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
I had this program to mark which sets matched which characters. I required v5.10 because that&#39;s the first appearance of the &lt;code class=&quot;regex&quot;&gt;\h&lt;/code&gt; and &lt;code class=&quot;regex&quot;&gt;\v&lt;/code&gt; shortcuts for horizontal and vertical whitespace.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use 5.010;

use charnames qw(:full);

print &lt;&lt;&quot;LEGEND&quot;;
s   matches \\s, matches Perl whitespace
h   matches \\h, horizontal whitespace
v   matches \\v, vertical whitespace
p   matches [[:space:]], POSIX whitespace
all characters match Unicode whitespace, \\p{Space}

LEGEND

printf qq(%s %s %s %s  %-7s --&gt; %s\n),
	qw( s h v p  Ordinal  Name );
print &#39;-&#39; x 50, &quot;\n&quot;;

foreach my $ord ( 0 .. 0x10ffff ) {
	next unless chr($ord) =~ /\p{Space}/;
	my( $s, $h, $v, $posix ) =
		map { chr($ord) =~ m/$_/ ? &#39;x&#39; : &#39; &#39; }
			( qr/\s/, qr/\h/, qr/\v/, qr/[[:space:]]/ );
	printf qq(%s %s %s %s  0x%04X  --&gt; %s\n),
		$s, $h, $v, $posix,
		$ord, charnames::viacode($ord);
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Under v5.10, the top of the output showed that &lt;code&gt;\s&lt;/code&gt; did not include the vertical tab, which the UCS names LINE TABULATION.
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.10.1 spaces
s   matches \s, matches Perl whitespace
h   matches \h, horizontal whitespace
v   matches \v, vertical whitespace
p   matches [[:space:]], POSIX whitespace
all characters match Unicode whitespace, \p{Space}

s h v p  Ordinal --&gt; Name
--------------------------------------------------
x x   x  0x0009  --&gt; CHARACTER TABULATION
x   x x  0x000A  --&gt; LINE FEED
    x x  0x000B  --&gt; LINE TABULATION
x   x x  0x000C  --&gt; FORM FEED
x   x x  0x000D  --&gt; CARRIAGE RETURN
x x   x  0x0020  --&gt; SPACE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Run under v5.18, the output changes slightly to have another &lt;code&gt;x&lt;/code&gt; in the third row (line 12).
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.18.0 spaces
s   matches \s, matches Perl whitespace
h   matches \h, horizontal whitespace
v   matches \v, vertical whitespace
p   matches [[:space:]], POSIX whitespace
all characters match Unicode whitespace, \p{Space}

s h v p  Ordinal --&gt; Name
--------------------------------------------------
x x   x  0x0009  --&gt; CHARACTER TABULATION
x   x x  0x000A  --&gt; LINE FEED
x   x x  0x000B  --&gt; LINE TABULATION
x   x x  0x000C  --&gt; FORM FEED
x   x x  0x000D  --&gt; CARRIAGE RETURN
x x   x  0x0020  --&gt; SPACE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
I don&#39;t foresee this breaking anything since the vertical tab seems to be a rare character, although in ETL I liked using it as a separator since I figured no one else would be using it.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Perl v5.12 adds the package NAME VERSION syntax</title>
		<link>https://www.effectiveperlprogramming.com/2013/05/perl-v5-12-adds-the-package-name-version-syntax/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2013/05/perl-v5-12-adds-the-package-name-version-syntax/</guid>
		<pubDate>Fri, 03 May 2013 09:52:22 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-12</category>
		<description>&lt;p&gt;
Perl v5.12 modifies the &lt;code&gt;package&lt;/code&gt; statement to take a version as well as a name. This allows you to implicitly declare the &lt;code&gt;$VERSION&lt;/code&gt; variable:
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package Some::Package 1.23;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is the same as setting &lt;code&gt;$VERSION&lt;/code&gt; explicitly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package Some::Package;

our $VERSION = &#39;1.23&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
or the older, pre-&lt;code&gt;our&lt;/code&gt; way:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package Some::Package;
use vars qw($VERSION);

$VERSION = &#39;1.23&#39;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Remember, the &lt;code&gt;package&lt;/code&gt; statement merely changes the default package. There&#39;s not much more magic than that. If you declare multiple versions, the last one wins even if it&#39;s lower than the ones that came before (and there&#39;s no warning), but everything is still in the same package:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.12;

package Local::foo 1.23;

sub show_version {
	say &quot;Version is &quot; . Local::foo-&gt;VERSION;
	}

__PACKAGE__-&gt;show_version;

package Local::foo 1.24;

__PACKAGE__-&gt;show_version;

package Local::foo 1.22;

__PACKAGE__-&gt;show_version;
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Ignore part of a substitution&#39;s match</title>
		<link>https://www.effectiveperlprogramming.com/2012/10/ignore-part-of-a-substitutions-match/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/10/ignore-part-of-a-substitutions-match/</guid>
		<pubDate>Fri, 26 Oct 2012 08:24:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-10</category>
		<description>&lt;p&gt;
Normally, a substitution replaces everything it matched, but v5.10 adds a feature that allows you to ignore part of the match. The &lt;code&gt;\K&lt;/code&gt; excludes from &lt;code&gt;$&amp;amp;&lt;/code&gt; anything to its left. This feature has already made it into &lt;a href=&quot;https://www.pcre.org&quot;&gt;PCRE&lt;/a&gt;. It doesn&#39;t have an official name, so I&#39;ll call it the &lt;i&gt;match reset operator&lt;/i&gt; because it resets the start of &lt;code&gt;$&amp;amp;&lt;/code&gt;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Start with the match operator. This pattern matches the entire string, being anchored at the absolute beginning and end of the string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
$_ = &#39;Buster and Mimi&#39;;
print &quot;Matched &amp;lt;&amp;#36;&amp;amp;&gt;&quot; if /\ABuster and \K\S+\z/;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output doesn&#39;t show the entire part of the string that actually matched. You only see the part past the &lt;code&gt;\K&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl slash-k.pl
Matched &amp;lt;Mimi&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now apply that to the substitution operator, where the right side replaces &lt;code&gt;$&amp;&lt;/code&gt;. Although this pattern needs to have &lt;code&gt;Buster and &lt;/code&gt; at the front, you don&#39;t want to replace that part. It&#39;s just part of locating the right part of the string:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
$_ = &#39;Buster and Mimi&#39;;
s/\ABuster and \K\S+\z/Ginger/;
print;   # Buster and Ginger
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can also do this with a positive lookbehind assertion since that doesn&#39;t actually match characters, so that subpattern doesn&#39;t show up in &lt;code&gt;$&amp;&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &#39;Buster and Mimi&#39;;
s/(?&amp;lt;=\ABuster and )\S+\z/Ginger/;
print; # Buster and Ginger
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The lookbehind has a limitation though. It can&#39;t be variable width (update: in v5.30 there&#39;s an experimental feature that allows that).
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &#39;Buster and Mimi&#39;;
s/(?&amp;lt;=\A\S+ and )\S+\z/Ginger/;
print; # Buster and Ginger
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This doesn&#39;t work and gives a fatal error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl slash-k.pl
Variable length lookbehind not implemented in
regex m/(?&amp;lt;=\A\S+ and )\S+\z/
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;\K&lt;/code&gt; however, handles the variable width just fine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
$_ = &#39;Buster and Mimi&#39;;
s/\A\S+ and \K\S+\z/Ginger/;
print;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now here&#39;s a more likely use for it. Match the whitespace at the beginning of a line, no matter how much there is, but don&#39;t replace it. That is, don&#39;t match lines that don&#39;t start with whitespace. You might try this, but you have to remember &lt;code&gt;$1&lt;/code&gt; and &lt;code&gt;$2&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &quot;    buster&quot;;
s/\A(\s+)(\S+)\z/$1\u$2/;
print &quot;Line |$_|&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With &lt;code&gt;\K&lt;/code&gt;, both sides are a bit simpler:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
$_ = &quot;    buster&quot;;      # lowercase!
s/\A\s+\K\S+\z/\u&amp;#36;&amp;amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Add &lt;code&gt;/x&lt;/code&gt; to make it even more apparent:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$_ = &quot;    buster&quot;;      # lowercase!
s/
	\A \s+ \K           # ignore this part for &amp;#36;&amp;amp;
	\S+
	\z
/\u&amp;#36;&amp;amp;/;
print &quot;Line |$_|&quot;;      # Line |    Buster|
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Don&#39;t use this in lookarounds&lt;/h2&gt;


&lt;p&gt;
Before v5.32, you could use &lt;code&gt;\K&lt;/code&gt; inside lookaround assertions, but what it does in that case is undefined so you might not get what you expect. Since the lookaround was never part of &lt;code&gt;&amp;#36;&amp;amp;&lt;/code&gt;, what would it reset?
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;s/(?&lt;=\s+\Kaaa)Ginger/.../
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since no one could figure out what should happen in that case, v5.32 disallows it.
&lt;/p&gt;



&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lookbehinds don&#39;t consume characters, but they can&#39;t be variable width&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;\K&lt;/code&gt; resets the start of &lt;code&gt;$&amp;amp;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>The Perl 5.12 yada yada operator</title>
		<link>https://www.effectiveperlprogramming.com/2012/08/the-new-yadda-yadda-operator/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/08/the-new-yadda-yadda-operator/</guid>
		<pubDate>Fri, 03 Aug 2012 09:19:56 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-12</category>
		<description>&lt;p&gt;
Perl v5.12 adds a placeholder operator, &lt;code&gt;...&lt;/code&gt;, called the yada yada operator, after an episode of &lt;a href=&quot;https://www.urbandictionary.com/define.php?term=Yada+yada+yada&quot;&gt;&lt;span class=&quot;tv-show&quot;&gt;Seinfeld&lt;/span&gt;&lt;/a&gt; where the interesting parts of the story are replaced with &quot;yada yada yada&quot;.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;figure class=&quot;text-center&quot;&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/O6kRqnfsBEc&quot; title=&quot;The Seinfeld scene that named the yada yada operator&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;figcaption class=&quot;figure-caption&quot;&gt;The &lt;span class=&quot;tv-show&quot;&gt;Seinfeld&lt;/span&gt; scene that named the operator&lt;/figcaption&gt;
&lt;/figure&gt;




&lt;p&gt;
You can replace whole statements with &lt;code&gt;...&lt;/code&gt; and Perl will compile it. You can check that everything else compiles without having to fill in the bits you have deferred. 
&lt;/p&gt;



&lt;p&gt;
Here&#39;s a simple program that compiles:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# yada.pl
use v5.12;

...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you check the syntax, everything is fine:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.12 -c yada.pl
yada.pl syntax OK
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
At runtime, the &lt;code&gt;...&lt;/code&gt; dies with the message &lt;code&gt;Unimplemented&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.12 yada.pl
Unimplemented at yada.pl line 3.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Before this feature, you might have inserted comments to mark the places you needed to return to:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# yada.pl
use v5.12;

# XXX: do that thing we talked about right here
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Being comments, these do compile, but being comments, they let the program continue past them like it worked despite part of it missing. The &lt;code&gt;...&lt;/code&gt; stops the program to remind you that you aren&#39;t done yet.
&lt;/p&gt;



&lt;p&gt;
The &lt;code&gt;...&lt;/code&gt; doesn&#39;t work as a placeholder for terms or partial statements:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# yada_term.pl
use v5.12;

... + ...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Those don&#39;t compile:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl5.12 -c yada_term.pl
syntax error at yada_term.pl line 3, near &quot;... +&quot;
yada_term.pl had compilation errors.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can make these into statements though by enclosing them in &lt;code&gt;do&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# yada_term.pl
use v5.12;

do{...} + do{...}
&lt;/code&gt;&lt;/pre&gt;</description>
	</item>
	<item>
		<title>Effective Perler discounts during OSCON</title>
		<link>https://www.effectiveperlprogramming.com/2012/07/effective-perler-discounts-during-oscon/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/07/effective-perler-discounts-during-oscon/</guid>
		<pubDate>Fri, 13 Jul 2012 19:15:13 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>promotion</category>
		<description>&lt;p&gt;
I&#39;ll be at OSCON on Tuesday, July 17, but you don&#39;t have to find me to get up to 37% off &lt;span class=&quot;book-title&quot;&gt;Effective Perl Programming&lt;/span&gt;. That&#39;s a slightly lower price than Amazon. To get that discount, you have to buy the book at Pearson&#39;s booth in the exhibition hall. You&#39;ll need to track me down on Tuesday afternoon or evening if you want me to sign your book.
&lt;/p&gt;



&lt;p&gt;
If you can&#39;t make it to OSCON, you can still get 35% off the cover price by ordering &lt;a href=&quot;https://www.informit.com/promotions/promotion.aspx?promo=138425&quot;&gt;directly from the InformIT discount link&lt;/a&gt; or using the OSCON2012 discount code when you check out. Instead of navigating their site, you can &lt;a href=&quot;https://www.informit.com/store/effective-perl-programming-ways-to-write-better-more-9780321496942&quot;&gt;go directly to our book&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
If you&#39;re not sure you want the book, you can look at &lt;a href=&quot;https://ptgmedia.pearsoncmg.com/images/9780321496942/samplepages/0321496949.pdf&quot;&gt;a free sample chapter&lt;/a&gt;, which is also 35% off during OSCON.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Declare your Pod encoding</title>
		<link>https://www.effectiveperlprogramming.com/2012/07/declare-your-pod-encoding/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/07/declare-your-pod-encoding/</guid>
		<pubDate>Sun, 01 Jul 2012 07:20:43 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>documentation</category>
		<description>&lt;p&gt;
&lt;a href=&quot;https://metacpan.org/pod/Pod::Simple&quot;&gt;Pod::Simple&lt;/a&gt; 3.21 changed its behavior when it encountered a non-ASCII character in Pod without an encoding. Instead of handling it quietly, it now gives a warning. That&#39;s not so bad, but &lt;a href=&quot;https://metacpan.org/pod/Test::Pod&quot;&gt;Test::Pod&lt;/a&gt; uses &lt;a href=&quot;https://metacpan.org/pod/Pod::Simple&quot;&gt;Pod::Simple&lt;/a&gt;, and whenever it sees a warning, &lt;code&gt;pod_ok&lt;/code&gt; fails, as it did in my &lt;a href=&quot;https://www.cpantesters.org/cpan/report/e0c5e3c4-c2bf-11e1-aed2-b65908f5d1fc&quot;&gt;Mac::Errors&lt;/a&gt; module: 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#   Failed test &#39;POD test for blib/lib/Mac/Errors.pm&#39;
#   at .../Test/Pod.pm line 182.
# blib/lib/Mac/Errors.pm (2776): Non-ASCII character seen before =encoding in &#39;donÃt&#39;. Assuming ISO8859-1
# Looks like you failed 1 test of 2.
t/pod.t ........... 
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/2 subtests 
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Unfortunately, the Pod tests are the sort that shouldn&#39;t stop an installation, which is why many developers have a separate area for author tests (which I&#39;ll cover in an upcoming Item). Outside of that, you have to fix the Pod.
&lt;/p&gt;



&lt;p&gt;
There are two things here. First, I have a genuine error here. The module is auto generated from other source files and the &quot;donÃt&quot; is a mistake; it should be &quot;don’t&quot; (with a smart quote) or even better, &quot;don&#39;t&quot;. &lt;a href=&quot;https://metacpan.org/pod/Test::Pod&quot;&gt;Test::Pod&lt;/a&gt; didn&#39;t catch this before. So, that&#39;s not bad.
&lt;/p&gt;



&lt;p&gt;
More importantly, telling Perl that the source code is UTF-8 isn&#39;t enough. When you use the &lt;a href=&quot;https://perldoc.perl.org/utf8&quot;&gt;utf8&lt;/a&gt; pragma, the &lt;code&gt;perl&lt;/code&gt; interpreter reads the source as UTF-8:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, a Pod parser ignores all the code. It looks for Pod sections and never sees that pragma, nor does it care. You have to tell the Pod parser which encoding you have if you want to use something outside of ASCII:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;=encoding utf8
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
I hadn&#39;t used that in &lt;a href=&quot;https://metacpan.org/pod/Mac::Errors&quot;&gt;Mac::Errors&lt;/a&gt;, or any of my other modules, although in some of them I had used genuine UTF-8 sequences. Now any person using &lt;a href=&quot;https://metacpan.org/pod/Test::Pod&quot;&gt;Test::Pod&lt;/a&gt; with the latest &lt;a href=&quot;https://metacpan.org/pod/Pod::Simple&quot;&gt;Pod::Simple&lt;/a&gt; won&#39;t be able to install those modules normally. That is, until I fix them.
&lt;/p&gt;



&lt;p&gt;
I could use other encodings, such as ISO-8859-1, as long as I declare the right thing and save the file correctly.
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href=&quot;https://perldoc.perl.org/utf8&quot;&gt;utf8&lt;/a&gt; pragma doesn&#39;t affect the Pod&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://metacpan.org/pod/Pod::Simple&quot;&gt;Pod::Simple&lt;/a&gt; assumes ASCII unless you tell it otherwise&lt;/li&gt;
&lt;li&gt;Declare your Pod encoding with the &lt;code&gt;=encoding&lt;/code&gt; directive&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Hide namespaces from PAUSE</title>
		<link>https://www.effectiveperlprogramming.com/2012/04/hide-namespaces-from-pause/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/04/hide-namespaces-from-pause/</guid>
		<pubDate>Sun, 29 Apr 2012 05:12:44 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>distributions</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
The grep.cpan.me site is retired, so the grep CPAN link goes to a copy on archive.org and the hide from pause search uses grep.metacpan.org. You can grep CPAN with &lt;a href=&quot;https://grep.metacpan.org&quot;&gt;grep.metacpan.org&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
The &lt;a href=&quot;https://pause.perl.org&quot;&gt;Perl Authors Upload Server (PAUSE)&lt;/a&gt; is responsible for analyzing distributions on their way to CPAN. PAUSE indexes the distributions to discover the package names that it contains so it can add them to the data files that many of the CPAN clients use to figure out what to download to install the module that you request. It also compares the package names that it finds to a list of permissions it maintains.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The &lt;a href=&quot;https://github.com/andk/pause/blob/master/cron/mldistwatch&quot;&gt;mldistwatch&lt;/a&gt; program is responsible for this bit. It tries two things to find the packages in the distribution. If it can, it reads the &lt;span class=&quot;filename&quot;&gt;META.yml&lt;/span&gt; or &lt;span class=&quot;filename&quot;&gt;META.json&lt;/span&gt; to get the data. Otherwise, it examines the files directly to look for package declarations, but sometimes these come up with the wrong answers.
&lt;/p&gt;



&lt;p&gt;
First, there&#39;s an easy fix to &lt;code&gt;package&lt;/code&gt; statements in code. After ignoring text in Pod or after &lt;code&gt;__END__&lt;/code&gt; or &lt;code&gt;__DATA__&lt;/code&gt;, PAUSE looks for package statements appearing on a single line:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;# from PAUSE::pmfile::packages_per_pmfile()
           $pline =~ m{
                      (.*)
                      \bpackage\s+
                      ([\w\:\&#39;]+)
                      \s*
                      (?: $ | [\}\;] | ($version::STRICT) )
                    }x&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If your complete &lt;code&gt;package&lt;/code&gt; statement isn&#39;t on a single line, then that won&#39;t match it. Since Perl has insignificant whitespace, including vertical whitespace, you could do this:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package
    hide::this::package;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might even leave yourself (or other developers) a note about the importance of that whitespace now:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;package # hide from pause
    hide::this::package;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Indeed, if you &lt;a href=&quot;https://web.archive.org/web/20120110011903/http://grep.cpan.me/&quot;&gt;grep CPAN&lt;/a&gt;, you&#39;ll find &lt;a href=&quot;https://grep.metacpan.org/search?q=hide+from+pause&quot;&gt;hide from pause&lt;/a&gt; in many distributions.
&lt;/p&gt;



&lt;p&gt;
That&#39;s the easy way, although it&#39;s kludgey and relies on a special case in the PAUSE code. Other indexers might not honor it. There&#39;s a better way for you to explicitly tell an indexer what namespaces you want to advertise. You add them to the &lt;code&gt;provides&lt;/code&gt; section of &lt;span class=&quot;filename&quot;&gt;META.yml&lt;/span&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;provides:
  Cats::Buster:
	file: lib/Cats/Buster.pm
	version: 0.01
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The data comes from the &lt;a href=&quot;https://module-build.sourceforge.net/META-spec-v1.4.html#provides&quot;&gt;META-spec&lt;/a&gt;. &lt;a href=&quot;https://metacpan.org/pod/Module::Build&quot;&gt;Module::Build&lt;/a&gt; will automatically create these entries in &lt;span class=&quot;filename&quot;&gt;META.yml&lt;/span&gt; for you. The indexer can use these to know what&#39;s in the distribution without directly examining module files.
&lt;/p&gt;



&lt;p&gt;
If there are multiple packages declarations, all of them show up in &lt;span class=&quot;filename&quot;&gt;META.yml&lt;/span&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;provides:
  Cats::Buster:
    file: lib/Test/Provides.pm
    version: 0.01
  Cats::Mimi:
    file: lib/Test/Provides.pm
    version: 0,02
  version:
    file: lib/Test/Provides.pm
    version: 0.01
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice that &lt;code&gt;version&lt;/code&gt; shows up in that list. You may have included it in your module to extend or override parts of that core module, but you don&#39;t want people who want the real &lt;code&gt;version&lt;/code&gt; to install your module to get it. You might only declare that package in your module as a temporary workaround and don&#39;t intend it to be a permanent part of the work. They probably wouldn&#39;t be able to do that anyway since PAUSE would recognize that you included a package for which you do not have permissions and would not index it. A site such as CPAN Search might mark your otherwise good distribution as &quot;&lt;span class=&quot;text-red&quot;&gt;UNAUTHORIZED&lt;/span&gt;&quot;. Module::Build doesn&#39;t know to exclude &lt;code&gt;version&lt;/code&gt;, at least not by default.
&lt;/p&gt;



&lt;p&gt;
To hide that package from indexers, you can specify it in &lt;code&gt;no_index&lt;/code&gt;. In &lt;span class=&quot;filename&quot;&gt;Build.PL&lt;/span&gt;, you can use &lt;code&gt;META_ADD&lt;/code&gt; to specify that parts of the META-spec not already supported by other arguments to &lt;code&gt;new&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Module::Build;

my $builder = Module::Build-&gt;new(
	...,
	meta_add =&gt; {
		no_index =&gt; {
			package   =&gt; [ qw( version Local ) ],
			directory =&gt; [ qw( t/inc inc ) ],
			file      =&gt; [ qw( t/lib/test.pm ) ],
			namespace =&gt; [ qw( Local ) ],
			},
		},
);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;directory&lt;/code&gt; and &lt;code&gt;file&lt;/code&gt; keys tell the indexer to ignore those parts of the distribution. The &lt;code&gt;package&lt;/code&gt; tells the indexer to ignore exactly those packages. The curious one is &lt;code&gt;namespace&lt;/code&gt;, which tells the indexer to ignore namespaces &lt;em&gt;under&lt;/em&gt; that namespace.
&lt;/p&gt;



&lt;p&gt;
Likewise, you can do the same in &lt;span class=&quot;filename&quot;&gt;Makefile.PL&lt;/span&gt; with a recent enough version:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use ExtUtils::MakeMaker 6.48;

WriteMakefile(
	...,
	META_ADD =&gt; {
		no_index =&gt; {
			package   =&gt; [ qw( version Local ) ],
			directory =&gt; [ qw( t/inc inc ) ],
			file      =&gt; [ qw( t/lib/test.pm ) ],
			namespace =&gt; [ qw( Local ) ],
			},
		},
	);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Otherwise, it examines the module files to find package statements, but it does it without running the code.
&lt;/p&gt;



&lt;p&gt;
But, what if &lt;code&gt;provides&lt;/code&gt; and &lt;code&gt;no_index&lt;/code&gt; have conflicting instructions? The META-spec doesn&#39;t give any guidance for indexers in those cases. PAUSE filters on &lt;code&gt;no_index&lt;/code&gt; last. This means that PAUSE and other indexers might leave out files you specify in &lt;code&gt;provides&lt;/code&gt; but then exclude in &lt;code&gt;no_index&lt;/code&gt;.
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Spread the package statement over two or more lines to hide it from PAUSE&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;provides&lt;/code&gt; to advertise the namespaces a distribution comprises.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;no_index&lt;/code&gt; to limit what an indexer sees or reports.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Don&#39;t use auto-dereferencing with each or keys</title>
		<link>https://www.effectiveperlprogramming.com/2012/03/dont-use-auto-dereferencing-with-each/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/03/dont-use-auto-dereferencing-with-each/</guid>
		<pubDate>Sat, 31 Mar 2012 09:31:18 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-12</category>
		<description>&lt;div class=&quot;note update&quot;&gt;
&lt;p&gt;
&lt;a href=&quot;/2016/04/lexical-_-and-autoderef-are-gone-in-v5-24&quot;&gt;Perl v5.24 removes this experimental feature&lt;/a&gt;, for the reasons I list, among others.
&lt;/p&gt;
&lt;/div&gt;



&lt;p&gt;
Perl 5.14 added an auto-dereferencing feature to the hash and array operators, and I wrote about those in &lt;a href=&quot;/2010/11/use-array-references-with-the-array-operators/&quot;&gt;Use array references with the array operators&lt;/a&gt;. I&#39;ve never particularly liked that feature, but I don&#39;t have to like everything. Additionally, Perl 5.12 expanded the job of &lt;a href=&quot;https://perldoc.perl.org/functions/keys&quot;&gt;keys&lt;/a&gt; and &lt;a href=&quot;https://perldoc.perl.org/functions/values&quot;&gt;values&lt;/a&gt; to also work on arrays. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
&lt;span class=&quot;person&quot;&gt;chromatic&lt;/span&gt; has explicated a problem with &lt;a href=&quot;https://perldoc.perl.org/functions/each&quot;&gt;each&lt;/a&gt;, which is both an array and hash operator. He details it in &lt;a href=&quot;https://www.modernperlbooks.com/mt/2012/03/inadvertent-inconsistencies-each-in-perl-512.html&quot;&gt;Inadvertent Inconsistencies: each in Perl 5.12&lt;/a&gt; and &lt;a href=&quot;https://www.modernperlbooks.com/mt/2012/03/inadvertent-inconsistencies-each-versus-autoderef.html&quot;&gt;Inadvertent Inconsistencies: each versus Autoderef&lt;/a&gt;. In short, if you use it with a reference, Perl doesn&#39;t know until it actually executes the &lt;a href=&quot;https://perldoc.perl.org/functions/each&quot;&gt;each&lt;/a&gt; if it&#39;s going to use its array or hash behavior (and in some cases, blow up with either). However, as the programmer, I probably know which behavior I want:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;while( my( $index, $value ) = each $ref ) { # I want array behavior
    my $elem = $other_array-&gt;[$index]; 
    } 

while( my( $key, $value ) = each $ref ) { ... } # I want hash behavior
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The problem isn&#39;t when it blows up, which is easy to catch (it blows up). If you get the wrong sort of reference, you&#39;ll get nonsensical indices or keys. If you have an array reference, you&#39;ll get numbers with the first return value. If you have a hash reference, you&#39;ll get strings. If you get strings but treat them as array indices, you&#39;ll likely always get array index 0, unless the key is a number. You might even get an odd index. If the key is &lt;code&gt;123Buster&lt;/code&gt;, you&#39;ll get array index &lt;code&gt;123&lt;/code&gt; due to Perl&#39;s numification. Going the other way, using an array reference when you expected a hash, you&#39;ll have to find keys that are whole numbers.
&lt;/p&gt;



&lt;p&gt;
Effective programs reduce ambiguity in their code, but this new feature increases it. It&#39;s easy to fix; you dereference them yourself. If you have the wrong reference type, you&#39;ll find out right away:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;while( my( $index, $value ) = each @$ref ) { my $elem = $other_array-&gt;[$index]; } # I want array behavior
while( my( $key, $value ) = each %$ref ) { ... } # I want hash behavior
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you really wanted to keep the auto-dereferencing feature, you could check the reference type before you use it, but what&#39;s the point of saving a character with the auto-dereferencing if you have to wrap the whole thing in a guard condition?
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;if( ref $ref eq ref [] ) {
    while( my( $index, $value ) = each @$ref ) { ... }
    }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now &lt;a href=&quot;https://perldoc.perl.org/functions/keys&quot;&gt;keys&lt;/a&gt; has the same problem. You can use that either with an array or a hash, but at some point you&#39;re probably going to have to know what sort of reference you have so you can use the key to dereference it. At that point, you effectively declare what sort of reference it should have been. If you have the wrong sort of reference, your script dies:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $ref = [ ... ];
foreach my $key ( keys $ref ) { 
    my $elem = $ref-&gt;{$key}; # Big error!
    }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This problem is the unintended consequence of letting the other array and hash operators take a scalar variable as an argument and letting the parser automatically add the bits to dereference. &lt;span class=&quot;person&quot;&gt;David Golden&lt;/span&gt; &lt;a href=&quot;https://xdg.me/if-perl-were-smarter-about-references/&quot;&gt;wanted more magic syntax&lt;/a&gt; and the patch wasn&#39;t so tough. To get the nicer syntax in some cases you end up dealing with more special cases. I noted this at the time &lt;span class=&quot;person&quot; data-fullname=&quot;David Golden&quot;&gt;David&lt;/span&gt; proposed it, but his enthusiasm for the interesting parts of the problem steamrolled over the bad parts.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Look up Unicode properties with an inversion map</title>
		<link>https://www.effectiveperlprogramming.com/2012/03/look-up-unicode-properties-with-an-inversion-map/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/03/look-up-unicode-properties-with-an-inversion-map/</guid>
		<pubDate>Mon, 12 Mar 2012 22:52:25 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-16</category>
		<description>&lt;p&gt;
Perl comes with extracts of the Unicode character data, but it hasn&#39;t been easy to look up all of the information Perl knows about a character. Perl v5.15.7 adds a way to create an inverted map based on the property that you want to access.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/Unicode::UCD&quot;&gt;Unicode::UCD&lt;/a&gt; module gives you access to some of the information about a character:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Unicode::UCD &#39;charinfo&#39;;
use charnames qw(:full);
use Data::Dumper;

my $charinfo   = charinfo(
	ord( &quot;\N{SMILING CAT FACE WITH OPEN MOUTH}&quot; )
	);
print Dumper( $charinfo );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output has many of the properties, but not all of them:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$VAR1 = {
		  &#39;digit&#39; =&gt; &#39;&#39;,
		  &#39;bidi&#39; =&gt; &#39;ON&#39;,
		  &#39;category&#39; =&gt; &#39;So&#39;,
		  &#39;code&#39; =&gt; &#39;1F63A&#39;,
		  &#39;script&#39; =&gt; &#39;Common&#39;,
		  &#39;combining&#39; =&gt; 0,
		  &#39;upper&#39; =&gt; &#39;&#39;,
		  &#39;name&#39; =&gt; &#39;SMILING CAT FACE WITH OPEN MOUTH&#39;,
		  &#39;unicode10&#39; =&gt; &#39;&#39;,
		  &#39;decomposition&#39; =&gt; &#39;&#39;,
		  &#39;comment&#39; =&gt; &#39;&#39;,
		  &#39;mirrored&#39; =&gt; &#39;N&#39;,
		  &#39;lower&#39; =&gt; &#39;&#39;,
		  &#39;numeric&#39; =&gt; &#39;&#39;,
		  &#39;decimal&#39; =&gt; &#39;&#39;,
		  &#39;title&#39; =&gt; &#39;&#39;,
		  &#39;block&#39; =&gt; &#39;Emoticons&#39;
		};
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This doesn&#39;t include the Age of the character, that is, when the character was added to Unicode. This might seem like a silly thing to know, but it came in handy typesetting &lt;span class=&quot;book-title&quot;&gt;Programming Perl&lt;/span&gt;. We had problems with some characters but we couldn&#39;t see a pattern until we looked at the age of all the problem characters. Any character added after Unicode 4.0 didn&#39;t typeset correctly. It took some annoying work to get the age by scanning through each age until that property matched:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/Users/brian/bin/perls/perl5.15.7

use v5.10;
use utf8;
use open qw(:std :utf8);

use List::Util qw(first);

my @chars =  ( &#39;a&#39;, &#39;⣽&#39;, &quot;\N{SMILING CAT FACE WITH OPEN MOUTH}&quot; );

my @ages = qw( 1.1 2.1 2.0 3.0 3.1 3.2 4.0 4.1 5.0 5.1 5.2 6.0 );

foreach my $char ( @chars ) {
	my $age = first { $char =~ /\p{Age=$_}/ } @ages;
	say &quot;$char -&gt; Age: $age&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It works, but it&#39;s an unsatisfying kludge:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;a -&gt; Age: 1.1
⣽ -&gt; Age: 3.0
😺 -&gt; Age: 6.0
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now, &lt;a href=&quot;https://metacpan.org/pod/Unicode::UCD&quot;&gt;Unicode::UCD&lt;/a&gt; has a &lt;code&gt;prop_invmap&lt;/code&gt; to create an index based on a property you choose and a &lt;code&gt;search_invlist&lt;/code&gt; to return the offset in the map:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;#!/Users/brian/bin/perls/perl5.15.7

use 5.15.7;
use utf8;
use open qw(:std :utf8);

use charnames qw(:full);
use List::Util qw(first);
use Unicode::UCD;

my @chars =  ( &#39;a&#39;, &#39;⣽&#39;, &quot;\N{SMILING CAT FACE WITH OPEN MOUTH}&quot; );

foreach my $char ( @chars ) {
	my $age = age_of_char( $char );
	say &quot;$char Age: $age&quot;;
	}

sub age_of_char {
	# create the inverted list, once
	# can only initialize as scalar
	state $inv = _make_age_inverted_list();

	my( $char ) = @_;

	my $i = Unicode::UCD::search_invlist($inv-&gt;[0], ord $char);
	return $inv-&gt;[1][$i];
	}

# create the inverted list, once
sub _make_age_inverted_list {
	state( $list, $map, $format, $default, $init );
	unless( $init++ ) {
		($list, $map, $format, $default) = Unicode::UCD::prop_invmap(&quot;Age&quot;);
		$format eq &quot;s&quot; || die &quot;wrong format $format&quot;;
		}
	return [ $list, $map ];
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That looks like a lot of work, but most of it happens once to set up the inversion map.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Fold cases properly</title>
		<link>https://www.effectiveperlprogramming.com/2012/02/fold-cases-properly/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/02/fold-cases-properly/</guid>
		<pubDate>Wed, 29 Feb 2012 22:04:51 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-16</category>
		<description>&lt;p&gt;
You might think that you know how to compare strings regardless of case, and you&#39;re probably wrong. After you read this Item, you&#39;ll be able to do it correctly and without doing any more work than you were doing before. Perl handles all the details for you. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
If you grew up in the ASCII world, case insensitivity is a difference of literally one bit, so changing case is setting or unsetting a bit in the octet that represents that character.
&lt;/p&gt;



&lt;p&gt;
If you&#39;ve read the Perl FAQ, you may have seen this quip:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
&quot;Perl&quot; is the name of the language. Only the &quot;P&quot; is capitalized. The name of the interpreter (the program which runs the Perl script) is &quot;perl&quot; with a lowercase &quot;p&quot;.
&lt;/blockquote&gt;


&lt;p&gt;
When &lt;span class=&quot;person&quot;&gt;Larry Wall&lt;/span&gt; was asked what the difference was between &quot;Perl&quot; and &quot;perl&quot;, he said &quot;One bit&quot;. It&#39;s literally a difference of flipping one bit in the ASCII representation. That&#39;s as complicated as ASCII case folding gets.
&lt;/p&gt;



&lt;p&gt;
The capital letter &lt;i&gt;P&lt;/i&gt; has the ordinal value 0b1010000. The small letter &lt;i&gt;p&lt;/i&gt;, which shows up later in the ASCII sequence, has the ordinal value 0b1110000. This makes it extremely easy to write routines to change between upper and lower cases:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

say &quot;  U L&quot;;
say &quot;-----&quot;;

foreach my $char ( qw(p P a b c A B C) ) {
	my $lower = chr( ord($char) | 0b0100000 );
	my $upper = chr( ord($char) &amp; 0b1011111 );
	
	say &quot;$char $upper $lower&quot;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows what you&#39;d expect for the upper and lower cases:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;  U L
-----
p P p
P P p
a A a
b B b
c C c
A A a
B B b
C C c
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Since bit flipping is easy to do, it&#39;s very easy for even primitive computers to quickly change case (assuming that you&#39;re not so primitive as to not have two cases). But, this only works if you restrict the output to the ASCII letters. If you want to handle non-letters, you have to do a bit more work to ensure that you don&#39;t shift them into other characters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

say &quot;  U L&quot;;
say &quot;-----&quot;;

foreach my $char ( qw(p P a b c A B C # !) ) {
	my $upper = uppercase( $char );
	my $lower = lowercase( $char );

	say &quot;$char $upper $lower&quot;;
	}
 
 sub lowercase {
 	my $_ = shift;
  	my $ord = ord();

 	return $_ unless $ord &gt;= 0x41 and $ord &lt;= 0x5A;
	return chr( $ord ^ 0b100000 );
	}

 sub uppercase {
 	my $_ = shift;	
 	my $ord = ord();
 	
 	return $_ unless $ord &gt;= 0x61 and $ord &lt;= 0x7A;
	return chr( $ord ^ 0b100000 );
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the non-letters stay the same character:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;  U L
-----
p P p
P P p
a A a
b B b
c C c
A A a
B B b
C C c
# # #
! ! !
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This almost works for Latin-* encodings too. When you move out of the ASCII sequence into Unicode, you don&#39;t have this luxury, and it&#39;s not merely a representational issue. 
&lt;/p&gt;



&lt;p&gt;
If you were infected with ASCII early, you&#39;ve grown up thinking that you can go back and forth between upper and lower cases and always get the same result. Outside of ASCII, that&#39;s not necessarily true. Consider the word &quot;Reichwaldstraße&quot;, a common street name in Germany. The &quot;straße&quot; has the special character ß (U+00DF LATIN SMALL LETTER SHARP S), which is a ligature of a long s, the fancy &lt;i&gt;ſ&lt;/i&gt; (U+017F LATIN SMALL LETTER LONG S) that you may have seen in historical documents, and the familiar short &lt;i&gt;s&lt;/i&gt;. Put them together, &lt;i&gt;ſs&lt;/i&gt;, and move them close enough and you can see how you would end up with &lt;i&gt;ß&lt;/i&gt; once you connect the hanging portion of the long &lt;i&gt;s&lt;/i&gt; with the top of the short &lt;i&gt;s&lt;/i&gt;. The UCS has an uppercase version (U+1E9E LATIN CAPITAL LETTER SHARP S), although no one uses it aside from saying that no one uses it. U+1E9E lowercases to U+00DF, but U+00DF has no single character uppercase version; it&#39;s the two characters &lt;i&gt;SS&lt;/i&gt;. The lowercase of &lt;i&gt;SS&lt;/i&gt;, however, is &lt;i&gt;ss&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;

my $string = &quot;Reichwaldstraße&quot;;

my $upper = uc( $string );
my $lower = lc( $upper  );

print &lt;&lt;&quot;HERE&quot;;
Started with: $string
Upper:        $upper
Lower:        $lower
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows that you don&#39;t get back to the original:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Started with: Reichwaldstraße
Upper:        REICHWALDSTRASSE
Lower:        reichwaldstrasse
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
There&#39;s another &lt;i&gt;s&lt;/i&gt; that causes problems: the Greek sigma, which comes in two lowercase forms. One appears in the middle of words and the other appears at the end, as in &lt;i&gt;Ὀδυσσεύς&lt;/i&gt; (Odysseus), where &lt;i&gt;σ&lt;/i&gt; and &lt;i&gt;ς&lt;/i&gt; represent the same thing, just in different forms mandated by their position:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;

my $char = &quot;Ὀδυσσεύς&quot;;

my $upper = uc( $char );
my $lower = lc( $upper );

print &lt;&lt;&quot;HERE&quot;;
Started with: $char
Upper:        $upper
Lower:        $lower
HERE
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Again, the lowercase version at the end is different than what you started with:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Started with: Ὀδυσσεύς
Upper:        ὈΔΥΣΣΕΎΣ
Lower:        ὀδυσσεύσ
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This means that you can&#39;t merely use &lt;a href=&quot;https://perldoc.perl.org/functions/lc&quot;&gt;lc&lt;/a&gt; to normalize text for case insensitive comparison. These won&#39;t compare correctly:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;lc( &quot;Reichwaldstraße&quot; ) eq lc( &quot;REICHWALDSTRASSE&quot; );  # Nope!
lc( &#39;Ὀδυσσεύς&#39; ) eq lc( &#39;ὈΔΥΣΣΕΎΣ&#39; );                         # Nope!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might object that these are different strings and that they shouldn&#39;t be the same, but where did these strings start? Perhaps that REICHWALDSTRASSE was not originally all uppercase, but changed by some stupid filters between you and the original information (and with a name like mine, I know about stupid casing filters). That&#39;s part of the ASCII infection.
&lt;/p&gt;



&lt;p&gt;
So, &lt;a href=&quot;https://perldoc.perl.org/functions/lc&quot;&gt;lc&lt;/a&gt; is the wrong way. Sadly, we do this incorrectly in &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt;, when we show this subroutine we want to &lt;a href=&quot;https://perldoc.perl.org/functions/sort&quot;&gt;sort&lt;/a&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub case_insensitive { &quot;\L$a&quot; cmp &quot;\L$b&quot; }
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The Unicode specification solves this with its &lt;i&gt;case folding&lt;/i&gt; rules. In short, it folds characters with different case forms into a common form. There&#39;s not a rule for this; they do it by exhaustion, specifying the common form for each fold. The common form is defined in the Unicode Character Database, which the Perl developers have digested into the files you find in the &lt;span class=&quot;filename&quot;&gt;unicore/&lt;/span&gt; directory in your Perl library. Here are a few lines from &lt;span class=&quot;filename&quot;&gt;unicore/CaseFolding.txt&lt;/span&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0050; C; 0070; # LATIN CAPITAL LETTER P
0051; C; 0071; # LATIN CAPITAL LETTER Q
0052; C; 0072; # LATIN CAPITAL LETTER R
00DF; F; 0073 0073; # LATIN SMALL LETTER SHARP S
03A3; C; 03C3; # GREEK CAPITAL LETTER SIGMA
03C2; C; 03C3; # GREEK SMALL LETTER FINAL SIGMA
FB00; F; 0066 0066; # LATIN SMALL LIGATURE FF
FB01; F; 0066 0069; # LATIN SMALL LIGATURE FI
FB02; F; 0066 006C; # LATIN SMALL LIGATURE FL
FB03; F; 0066 0066 0069; # LATIN SMALL LIGATURE FFI
FB04; F; 0066 0066 006C; # LATIN SMALL LIGATURE FFL
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The first column is the code number of the original character, the second is the type of folding (explained in the data file and coming up later), and the third column are the code numbers that form the common, folded (&quot;equivalent&quot;) version. Essentially, it&#39;s a big hash. Notice that some of the folded versions are multiple characters. You&#39;re not going to get that with bit fiddling.
&lt;/p&gt;



&lt;p&gt;
Case folding takes the character in the first column and turns them into the characters in the third column, then takes the result and does it again until there are no more folds possible. It keeps doing that until there is nothing to replace. Characters that don&#39;t have an entry in this file fold into themselves. You case fold to compare strings, not to normalize strings for storage or other uses. Case folding makes case insensitive comparisons very fast, but it also loses information that you can&#39;t recover. You can read the exact rules in Section 5.18, &quot;Case mapping&quot;, of the &lt;a href=&quot;https://unicode.org/standard/standard.html&quot;&gt;Unicode Standard&lt;/a&gt;.
&lt;/p&gt;



&lt;p&gt;
To see how that works, try that with &lt;i&gt;Reichwaldstraße&lt;/i&gt; and &lt;i&gt;Ὀδυσσεύς&lt;/i&gt;. All characters except two stay the same, and two use the mapping from &lt;span class=&quot;filename&quot;&gt;unicore/CaseFolding.txt&lt;/span&gt;:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Reichwaldstraße → &lt;b&gt;r&lt;/b&gt;eichwaldstra&lt;b&gt;ss&lt;/b&gt;e&lt;/li&gt;
&lt;li&gt;REICHWALDSTRASSE → reichwaldstrasse&lt;/li&gt;
&lt;li&gt;Ὀδυσσεύς → ὀδυσσεύ&lt;b&gt;σ&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;ὈΔΥΣΣΕΎΣ → ὀδυσσεύσ&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
To implement these operations, Perl v5.16 adds the &lt;code&gt;fc&lt;/code&gt; built-in function. Instead of &lt;a href=&quot;https://perldoc.perl.org/functions/lc&quot;&gt;lc&lt;/a&gt;, use that:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;
fc( &quot;Reichwaldstraße&quot; ) eq fc( &quot;REICHWALDSTRASSE&quot; );  # Yep!
fc( &#39;ὈΔΥΣΣΕΎΣ&#39; ) eq fc( &#39;Ὀδυσσεύς&#39; );                         # Yep!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you don&#39;t have v5.16, you can use the &lt;code&gt;fc&lt;/code&gt; from the &lt;a href=&quot;https://metacpan.org/pod/Unicode::CaseFold&quot;&gt;Unicode::CaseFold&lt;/a&gt; module on CPAN.
&lt;/p&gt;



&lt;p&gt;
If you wanted to do this inside a double-quoted string, you can use the &lt;code&gt;\F&lt;/code&gt; case shift operator (but be aware of the things we noted in &lt;a href=&quot;/2012/01/understand-the-order-of-operations-in-double-quoted-contexts/&quot;&gt;Understand the order of operations in double quoted contexts&lt;/a&gt;). Our &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt; example could change to:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;sub case_insensitive { &quot;\F$a&quot; cmp &quot;\F$b&quot; }
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;More complicated folds&lt;/h2&gt;


&lt;p&gt;
Looking back at the extract of &lt;span class=&quot;filename&quot;&gt;unicore/CaseFolding.txt&lt;/span&gt;, you might remember that I skipped over the second column, the mapping status. Those letters stand for different folding rules:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;C: common case folding&lt;/li&gt;
&lt;li&gt;F: full case folding (strings may grow in length)&lt;/li&gt;
&lt;li&gt;S: simple case folding (map to single characters)&lt;/li&gt;
&lt;li&gt;T: special case for uppercase I and dotted uppercase I&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
The &quot;T&quot; status stands in for folds that the general rules can&#39;t handle, mostly some characters from Turkish and similar languages.
&lt;/p&gt;



&lt;p&gt;
So far, Perl&#39;s &lt;code&gt;fc&lt;/code&gt; only handles the &quot;F&quot; status for full case folding. It doesn&#39;t handle the special folding you&#39;ll find in &lt;span class=&quot;filename&quot;&gt;unicore/SpecialCasing.txt&lt;/span&gt; that has the oddball situations, such as multiple source characters folding onto other multiple characters. If you want to handle those, you&#39;re on your own, although the &lt;a href=&quot;https://metacpan.org/pod/Unicode::Casing&quot;&gt;Unicode::Casing&lt;/a&gt; module on CPAN might help.
&lt;/p&gt;



&lt;p&gt;
Many of the folding rules depend on the source language, so you&#39;ll probably want to pay special attention if you are using that language or completely ignore them if you are not.
&lt;/p&gt;



&lt;p&gt;
Besides that, the Universal Character Set gives people much more of a chance to mess up. Suppose that you want to write &quot;β-carotene&quot;, that thing you get from carrots. That first character is β (U+03B2 GREEK SMALL LETTER BETA). Some people might think it looks like ß (U+00DF LATIN SMALL LETTER SHARP S), and that&#39;s good enough for them. No amount of case folding is going to let you know that someone used an incorrect character. But, this is also one of the benefits of Unicode: characters know what they are.
&lt;/p&gt;


&lt;h2&gt;Another correct way&lt;/h2&gt;


&lt;p&gt;
There&#39;s another correct way to check strings regardless of case. You can use the &lt;code&gt;/i&lt;/code&gt; flag on the match operator. The Unicode-aware Perl regex engine handles the rest:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;
use v5.18;

use Set::CrossProduct;

my $string = &quot;Reichwaldstraße&quot;;

my $upper = uc( $string );
my $lower = lc( $upper  );

my $sets = Set::CrossProduct-&gt;new(
	[
	[ $string, $upper, $lower ],
	[ $string, $upper, $lower ],
	]
	);

foreach my $tuple ( $sets-&gt;combinations ) {
	my( $l, $r ) = @$tuple;
	next if $l eq $r;

	say &quot;lc($r) eq lc($l)  ? &quot;, lc($r) eq lc($l) ? &quot;matched&quot; : &quot;failed&quot;;
	say &quot;fc($r) eq fc($l)  ? &quot;, fc($r) eq fc($l) ? &quot;matched&quot; : &quot;failed&quot;;
	say &quot;$r =~ m/$l/i      ? &quot;, $l =~ m/$r/i ? &quot;matched&quot; : &quot;failed&quot;;
	
	say;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In the output, you can see that &lt;a href=&quot;https://perldoc.perl.org/functions/lc&quot;&gt;lc&lt;/a&gt; sometimes fails, but that the &lt;code&gt;fc&lt;/code&gt; and &lt;code&gt;m//i&lt;/code&gt; always work:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;lc(REICHWALDSTRASSE) eq lc(Reichwaldstraße)  ? failed
fc(REICHWALDSTRASSE) eq fc(Reichwaldstraße)  ? matched
REICHWALDSTRASSE =~ m/Reichwaldstraße/i      ? matched

lc(reichwaldstrasse) eq lc(Reichwaldstraße)  ? failed
fc(reichwaldstrasse) eq fc(Reichwaldstraße)  ? matched
reichwaldstrasse =~ m/Reichwaldstraße/i      ? matched

lc(Reichwaldstraße) eq lc(REICHWALDSTRASSE)  ? failed
fc(Reichwaldstraße) eq fc(REICHWALDSTRASSE)  ? matched
Reichwaldstraße =~ m/REICHWALDSTRASSE/i      ? matched

lc(reichwaldstrasse) eq lc(REICHWALDSTRASSE)  ? matched
fc(reichwaldstrasse) eq fc(REICHWALDSTRASSE)  ? matched
reichwaldstrasse =~ m/REICHWALDSTRASSE/i      ? matched

lc(Reichwaldstraße) eq lc(reichwaldstrasse)  ? failed
fc(Reichwaldstraße) eq fc(reichwaldstrasse)  ? matched
Reichwaldstraße =~ m/reichwaldstrasse/i      ? matched

lc(REICHWALDSTRASSE) eq lc(reichwaldstrasse)  ? matched
fc(REICHWALDSTRASSE) eq fc(reichwaldstrasse)  ? matched
REICHWALDSTRASSE =~ m/reichwaldstrasse/i      ? matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The match operator isn&#39;t useful for &lt;a href=&quot;https://perldoc.perl.org/functions/sort&quot;&gt;sort&lt;/a&gt; though, since you can only tell if the strings are the same. 
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Case-folding is more complicated than merely lowercasing.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;fc&lt;/code&gt; does proper case folding according to the Unicode standard.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;\F&lt;/code&gt; case fold operator does full case folding in double-quoted contexts.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Use __SUB__ to get a reference to the current subroutine</title>
		<link>https://www.effectiveperlprogramming.com/2012/02/use-__sub__-to-get-a-reference-to-the-current-subroutine/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/02/use-__sub__-to-get-a-reference-to-the-current-subroutine/</guid>
		<pubDate>Sun, 19 Feb 2012 13:53:47 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-16</category>
		<description>&lt;p&gt;
What if you want to write a recursive subroutine but you don&#39;t know the name of the current subroutine? Since Perl is a dynamic language and code references are first class objects, you might not know the name of the code reference, if it even has a name. Perl 5.16 introduces &lt;code&gt;__SUB__&lt;/code&gt; as a special sequence to return a reference to the current subroutine. You could almost do the same thing without the new feature, but each of those has drawbacks you might want to avoid. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
Although &lt;code&gt;__SUB__&lt;/code&gt; looks like &lt;code&gt;__FILE__&lt;/code&gt;, &lt;code&gt;__LINE__&lt;/code&gt;, and &lt;code&gt;__PACKAGE__&lt;/code&gt;, each of which is compile-time directives, the &lt;code&gt;__SUB__&lt;/code&gt; happens at run time so you can use it with subroutines you define later.
&lt;/p&gt;



&lt;p&gt;
First, consider how you&#39;d try to do this without the &lt;code&gt;__SUB__&lt;/code&gt; feature. You could declare a variable to hold a subroutine reference then in a later statement define the subroutine. Since you&#39;ve already declared the variable, you can use it in the definition. Perl won&#39;t de-reference it until you actually run the subroutine, so it doesn&#39;t matter that it&#39;s not a reference yet:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $sub;

$sub = sub {
	state $count = 10;
	say $count;
	return if --$count &lt; 0;
	$sub-&gt;();
	};
	
$sub-&gt;();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Your output is a countdown:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;10
9
8
7
6
5
4
3
2
1
0
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
To do that, there are two requirements: the code reference must be stored in a variable, and the variable must already be defined. That&#39;s not always convenient. Not only that, your anonymous subroutine contains a reference to itself, so you&#39;d either have to play games with weak references or just let the reference live forever. Neither of those is attractive.
&lt;/p&gt;



&lt;p&gt;
&lt;span class=&quot;person&quot;&gt;Rafaël Garcia-Suarez&lt;/span&gt; solved these problems by creating &lt;a href=&quot;https://metacpan.org/pod/Sub::Current&quot;&gt;Sub::Current&lt;/a&gt; to give you a &lt;code&gt;ROUTINE&lt;/code&gt; function that returns a reference to the current subroutine, even if it is a named subroutine:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Sub::Current;

sub countdown {
	state $count = 10;
	say $count;
	return if --$count &lt; 0;
	ROUTINE-&gt;();
	};
	
countdown();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might want to define these code references as a single statement, even if you don&#39;t need to. This is useful for inline subroutines where you want to define the code reference in the parameter list:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Sub::Current;

sub run { $_[0]-&gt;() };

run( sub {
		state $count = 10;
		say $count;
		return if --$count &lt; 0;
		ROUTINE-&gt;();
		}
	);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You may want to define the subroutine in one statement as a return value:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Sub::Current;

sub factory {
	my $start = shift;
	sub {
		state $count = $start;
		say $count;
		return if --$count &lt; 0;
		ROUTINE-&gt;();
		}
	};

factory(4)-&gt;();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Using this module has the disadvantage of a CPAN dependency, although a very light one because it&#39;s self contained. There&#39;s another module, &lt;a href=&quot;https://metacpan.org/pod/Devel::Caller&quot;&gt;Devel::Caller&lt;/a&gt;, from &lt;span class=&quot;person&quot;&gt;Richard Clamp&lt;/span&gt; that can get a code reference from any level in the call stack, including the current level:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Devel::Caller qw(caller_cv);

sub factory {
	my $start = shift;
	sub {
		state $count = $start;
		say $count;
		return if --$count &lt; 0;
		caller_cv(0)-&gt;();
		}
	};

factory(7)-&gt;();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl 5.16 lets you do the same thing without the CPAN module:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.15.6;  # until v5.16 is released

sub factory {
	my $start = shift;
	sub {
		state $count = $start;
		say $count;
		return if --$count &lt; 0;
		__SUB__-&gt;();
		}
	};
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
As with many new features added since Perl v5.10, you can enable &lt;code&gt;__SUB__&lt;/code&gt; with a &lt;code&gt;use &lt;i&gt;VERSION&lt;/i&gt;&lt;/code&gt; statement, as you see in the previous example, or with the &lt;code&gt;feature&lt;/code&gt; pragma and the &lt;code&gt;current_sub&lt;/code&gt; import:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use feature qw(say state current_sub);

sub factory {
	my $start = shift;
	sub {
		state $count = $start;
		say $count;
		return if --$count &lt; 0;
		__SUB__-&gt;();
		}
	};

factory(7)-&gt;();	
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Perl v5.16 provides the &lt;code&gt;__SUB__&lt;/code&gt; directive to return a reference to the currently running subroutine&lt;/li&gt;
&lt;li&gt;Import this new feature by requiring the Perl version or through the &lt;code&gt;feature&lt;/code&gt; pragma&lt;/li&gt;
&lt;li&gt;Prior to Perl v5.16, you can do the same thing with &lt;a href=&quot;https://metacpan.org/pod/Sub::Current&quot;&gt;Sub::Current&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Understand the order of operations in double quoted contexts</title>
		<link>https://www.effectiveperlprogramming.com/2012/01/understand-the-order-of-operations-in-double-quoted-contexts/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/01/understand-the-order-of-operations-in-double-quoted-contexts/</guid>
		<pubDate>Tue, 31 Jan 2012 05:14:04 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>the-basics-of-perl</category>
		<description>&lt;p&gt;
Perl&#39;s powerful string manipulation tools include case-shifting operators that change the parts of a double-quoted string. There are many other things that happen in a double-quoted string too, so you need to know where these operators fit in with each other. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
A double-quoted string has three features:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Variable interpolation&lt;/li&gt;
&lt;li&gt;Escaped and logical characters&lt;/li&gt;
&lt;li&gt;Case shift operators&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
You might have missed this because the documentation doesn&#39;t emphasize it. There is a single sentence in &lt;a href=&quot;https://perldoc.perl.org/perlop#Quote-and-Quote-like-Operators&quot;&gt;perlop&lt;/a&gt;, but in relation to the regular expression operators and the &lt;code&gt;\Q&lt;/code&gt;:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
For double-quoted strings, the quoting from \Q is applied after interpolation and escapes are processed.
&lt;/blockquote&gt;


&lt;p&gt;
If you don&#39;t pay attention to the order of these operations, you&#39;ll get results that you might not expect. The problem is that the order of operations isn&#39;t the same in all double-quoted contexts.
&lt;/p&gt;



&lt;p&gt;
In strings, the order of operations is the same as listed earlier:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Variable interpolation&lt;/li&gt;
&lt;li&gt;Escaped and logical characters&lt;/li&gt;
&lt;li&gt;Case shift operators&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Variable interpolation&lt;/h2&gt;


&lt;p&gt;
You already know about variable interpolation. This is one of Perl&#39;s greatest features, and the one I miss the most when I have to use a different language:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $cat = &#39;Roscoe&#39;;
my $string = &quot;Buster $cat Mimi&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In a double quoted context, Perl substitutes the value of &lt;code&gt;$cat&lt;/code&gt;. You end up with &lt;code&gt;Buster Roscoe Mimi&lt;/code&gt;.
&lt;/p&gt;


&lt;h2&gt;Case-shift operators&lt;/h2&gt;


&lt;p&gt;
The case-shift operators change parts of a double-quoted string. Although we call them &quot;case shift&quot;, not all of them change the case.
&lt;/p&gt;


&lt;table class=&quot;mx-auto my-4&quot;&gt;
&lt;tr&gt;&lt;th&gt;Operator&lt;/th&gt;&lt;th&gt;Effect&lt;/th&gt;&lt;th&gt;Function equivalent&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;\U&lt;/td&gt;&lt;td&gt;Uppercase everything following&lt;/td&gt;&lt;td&gt;uc&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\u&lt;/td&gt;&lt;td&gt;Uppercase the next character&lt;/td&gt;&lt;td&gt;ucfirst&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\L&lt;/td&gt;&lt;td&gt;Lowercase everything following&lt;/td&gt;&lt;td&gt;lc&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\l&lt;/td&gt;&lt;td&gt;Lowercase the next character&lt;/td&gt;&lt;td&gt;lcfirst&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\F&lt;/td&gt;&lt;td&gt;(v5.16) Lowercase everything following&lt;/td&gt;&lt;td&gt;fc&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\Q&lt;/td&gt;&lt;td&gt;Quote metacharacters&lt;/td&gt;&lt;td&gt;quotemeta&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;\E&lt;/td&gt;&lt;td&gt;Stop whatever you were doing&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;


&lt;p&gt;
The &lt;code&gt;\F&lt;/code&gt; and &lt;code&gt;fc&lt;/code&gt; are new for the yet unreleased Perl v5.16. Those will show up in a different Item. Notice there&#39;s no &lt;code&gt;\f&lt;/code&gt; for a &lt;code&gt;fcfirst&lt;/code&gt;. That double-quoted sequence already means &quot;form feed&quot;, the instruction to printers to stop the current page and start a new page.
&lt;/p&gt;



&lt;p&gt;
Look at some examples using these in a double-quoted string:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -e &#39;print &quot;\ubuster\n&quot;&#39;
Buster
% perl -e &#39;print &quot;\LBUSTER\n&quot;&#39;
buster
% perl -e &#39;print &quot;\Ubuster\n&quot;&#39;
BUSTER
% perl -e &#39;print &quot;\Ubus\Eter\n&quot;&#39;
BUSter
% perl -e &#39;print &quot;\LBUST\EER\n&quot;&#39;
bustER
% perl -e &#39;print &quot;\QP*rl\n&quot;&#39;
P\*rl\
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That last one is a bit odd. It looks like it ends with a &lt;code&gt;\&lt;/code&gt;. It doesn&#39;t really end like that because there&#39;s a newline that &lt;code&gt;\Q&lt;/code&gt; quoted:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -e &#39;print &quot;\QP*rl\n&quot;&#39; | hexdump -C
00000000  50 5c 2a 72 6c 5c 0a                 |P\*rl\.|
00000007
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl handled the &quot;\n&quot; before it handled the &lt;code&gt;\Q&lt;/code&gt;, but the meta-character quoter thinks the newline is a special character so it escapes it. An escaped newline is just a newline, though.
&lt;/p&gt;



&lt;p&gt;
Now, combine these with variable interpolation. Perl handles the variables first then does the case shifting:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use 5.14.1;

my $cat = &#39;Buster&#39;;

say &quot;Roscoe $cat Mimi&quot;;
say &quot;Roscoe \U$cat Mimi&quot;;
say &quot;Roscoe \U$cat\E Mimi&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The results are probably not surprising. The first line is just interpolation, the second line uppercases everything from &lt;code&gt;\U&lt;/code&gt; to the end, and the third line uppercases only the parts between the &lt;code&gt;\U&lt;/code&gt; and the &lt;code&gt;\E&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Roscoe Buster Mimi
Roscoe BUSTER MIMI
Roscoe BUSTER Mimi
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If the case shift happens after interpolation, you might think that you could interpolate a case shift:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use 5.14.1;

my $cat = &#39;\UBuster&#39;; # no case shift in a single quote!

say &quot;Roscoe $cat Mimi&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That doesn&#39;t work though. The intended case shift operator shows up as literal characters because Perl doesn&#39;t do double processing:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Roscoe \UBuster Mimi
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
A &lt;code&gt;\U&lt;/code&gt; inside the string doesn&#39;t bother the escaped characters because Perl has already processed those: 
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use 5.14.1;

my $cat = &#39;Buster&#39;;

say &quot;Roscoe \U$cat\a\n Mimi&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &quot;\n&quot; is still a newline and the &quot;\a&quot; is still the bell, and everything after the &lt;code&gt;\U&lt;/code&gt; is uppercased (if it has an uppercase equivalent).
&lt;/p&gt;



&lt;p&gt;
That seems simple enough. It&#39;s variable interpolation followed by character escapes followed by case shifting. But this is Perl, so it can&#39;t be that easy.
&lt;/p&gt;


&lt;h2&gt;Regular expression double quoting&lt;/h2&gt;


&lt;p&gt;
The regular expression operators (&lt;code&gt;qr&lt;/code&gt;, &lt;code&gt;m//&lt;/code&gt;, and &lt;code&gt;s///&lt;/code&gt;) handle the double quote operations differently. From &lt;a href=&quot;https://perldoc.perl.org/perlop#Quote-and-Quote-like-Operators&quot;&gt;perlop&lt;/a&gt;:
&lt;/p&gt;


&lt;blockquote class=&quot;blockquote border-start border-4 ps-3&quot;&gt;
For the pattern of regex operators (qr//, m// and s///), the quoting from \Q is applied after interpolation is processed, but before escapes are processed.
&lt;/blockquote&gt;


&lt;p&gt;
Now the order of operations is:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Variable interpolation&lt;/li&gt;
&lt;li&gt;Case shift operators&lt;/li&gt;
&lt;li&gt;Escaped and logical characters&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
You can see this when you print the stringified forms of the patterns:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -le &#39;print qr/\Q\n/&#39;
(?-xism:\\n)
% perl -le &#39;print qr/\U\n/&#39;
(?-xism:\N)
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You probably expect all of these to match, but not all of them do:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -le &#39;print &quot;\n&quot; =~ qr/\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
% perl -le &#39;print &quot;\n&quot; =~ qr/\Q\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl -le &#39;print &quot;\n&quot; =~ qr/\U\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl -le &#39;print &quot;\n&quot; =~ qr/\l\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
% perl -le &#39;print &quot;\n&quot; =~ qr/\L\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The last two times are curious. The &lt;code&gt;\l&lt;/code&gt; and &lt;code&gt;\L&lt;/code&gt; leave the &lt;i&gt;n&lt;/i&gt; as a lowercase &lt;i&gt;n&lt;/i&gt; so in the last step, the &lt;code&gt;\n&lt;/code&gt; is still a newline. Those two tests still match.
&lt;/p&gt;



&lt;p&gt;
This means that you can construct a string and a pattern with the same sequence of characters, but they might not match:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -le &#39;print &quot;\Q\n&quot; =~ qr/\Q\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl -le &#39;print &quot;\U\n&quot; =~ qr/\U\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl -le &#39;print &quot;\L\n&quot; =~ qr/\L\n/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s even worse. What does the &lt;code&gt;\N&lt;/code&gt; mean? It depends on the Perl version:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.10.1 -le &#39;print &quot;\n&quot; =~ qr/\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Missing braces on \N{} in regex; marked by &lt;-- HERE in m/\N &lt;-- HERE / at -e line 1.
% perl5.12.1 -le &#39;print &quot;\n&quot; =~ qr/\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl5.14.1 -le &#39;print &quot;\n&quot; =~ qr/\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Perl v5.12 added &lt;code&gt;\N&lt;/code&gt; as &quot;not a newline&quot; to replace the &lt;code&gt;.&lt;/code&gt; no matter which default regex switches you have. That&#39;s why Perl v5.10 thinks you have an incomplete &lt;code&gt;\N{CHARNAME}&lt;/code&gt;. The others match a newline because the case shift happens in the middle of the process:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl5.10.1 -le &#39;print &quot;\n&quot; =~ qr/\L\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
% perl5.14.1 -le &#39;print &quot;\n&quot; =~ qr/\L\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
% perl5.8.9 -le &#39;print &quot;\n&quot; =~ qr/\L\N/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Missing braces on \N{} at -e line 1, near &quot;\L&quot;
Execution of -e aborted due to compilation errors.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With the &lt;code&gt;\N{CHARNAME}&lt;/code&gt; syntax, you can match characters by their name in the Universal Character Set. Here you match an uppercase &lt;i&gt;A&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;A&quot; =~ qr/\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you put a &lt;code&gt;\L&lt;/code&gt; in front of that, you might think it would match the lowercase version of the named letter. There&#39;s no such luck because the &lt;code&gt;\L&lt;/code&gt; affects the pattern before the &lt;code&gt;\N{CHARNAME}&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;A&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
% perl -Mcharnames=:full -le &#39;print &quot;a&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;\N&lt;/code&gt; turns into a newline and the braces are now for a quantifier with a non-number in it:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print qr/\L\N{LATIN CAPITAL LETTER A}/&#39;
(?-xism:\n{u+41})
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You might think that this would match anything since that should probably turn into &lt;code&gt;\n{0}&lt;/code&gt; just like the values in the array index turn into integers. The &lt;a href=&quot;https://perldoc.perl.org/perlre&quot;&gt;perlre&lt;/a&gt; section on &quot;Quantifiers&quot; doesn&#39;t say what should happen, but if it&#39;s not a number, the braces become literals. Here&#39;s a simple demonstration that those braces are literals:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -le &#39;print &quot;\n{a}&quot; =~ qr/\n{a}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Here&#39;s the pattern you created before, and that you want to match now:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print qr/\L\N{LATIN CAPITAL LETTER A}/&#39;
(?^u:\n{u+41})
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It doesn&#39;t match a lowercase &lt;i&gt;a&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;$ perl -Mcharnames=:full -le &#39;print &quot;a&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It doesn&#39;t match a newline either. The pattern is &lt;code&gt;\n{u+41}&lt;/code&gt;, and that&#39;s not a quantifier. There are some characters after the &lt;code&gt;\n&lt;/code&gt;, so the target string doesn&#39;t have enough characters to match:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;\n&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Using the regular expression text doesn&#39;t work either, which you might miss on the first pass:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;\n{u+41}&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
No
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Of course! That &lt;code&gt;+&lt;/code&gt; is a quantifier, so it isn&#39;t a literal character that should show up in the string. So this works:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;\n{u41}&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This works too because you can have one or more of &lt;i&gt;u&lt;/i&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;\n{uuuuu41}&quot; =~ qr/\L\N{LATIN CAPITAL LETTER A}/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you don&#39;t want the &lt;code&gt;\L&lt;/code&gt; to extend into the character name sequence, you can use the &lt;code&gt;\E&lt;/code&gt; to limit its effect:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;% perl -Mcharnames=:full -le &#39;print &quot;bar&quot; =~ qr/\LB\E\N{LATIN SMALL LETTER A}r/ ? &quot;Yes&quot; : &quot;No&quot;&#39;
Yes
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The double quote string constructor handles variable interpolation, special characters, and case shift operators in that order.&lt;/li&gt;
&lt;li&gt;The regular expression operators handle variable interpolation, case shift operators, and special characters in that order.&lt;/li&gt;
&lt;li&gt;Double-quoted interpolation in a match operator happens before regular expression compilation.&lt;/li&gt;
&lt;li&gt;The min-max quantifier is only a quantifier if you give it numbers. Otherwise, it&#39;s literal characters.&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>The Effective Perler in 2012 and beyond</title>
		<link>https://www.effectiveperlprogramming.com/2012/01/the-effective-perler-in-2012-and-beyond/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2012/01/the-effective-perler-in-2012-and-beyond/</guid>
		<pubDate>Sun, 01 Jan 2012 11:11:40 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>administrative-note</category>
		<description>&lt;p&gt;
Two years ago, &lt;span class=&quot;person&quot;&gt;Josh McAdams&lt;/span&gt; and I started &lt;a href=&quot;/&quot;&gt;&lt;span class=&quot;site&quot;&gt;The Effective Perler&lt;/span&gt;&lt;/a&gt; as an extension of the second edition of &lt;span class=&quot;book-title&quot;&gt;Effective Perl Programming&lt;/span&gt;. Since then, roughly once a week, we added one meaty Item a week. Last month, we published our 100th Item. With the 120 Items in the book, that&#39;s a lot of items.
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
I have a new plan for 2012 onward. It&#39;s much harder to find topics now and it takes much longer to research and write them. I&#39;ve exhausted all of the advice I have and all of the easy topics. When I think I have a good idea, I now know to search everything else I&#39;ve already written. More than a couple of times I thought I had the next week&#39;s idea, but it was already in the book or on the website.
&lt;/p&gt;



&lt;p&gt;
The search for content has another problem: I don&#39;t want to add Items for anything that&#39;s already been written—not just by me, but by anyone. I don&#39;t want to repeat content unless I have a different take on it and I can illuminate something new.
&lt;/p&gt;



&lt;p&gt;
I&#39;m not going to do weekly big Items anymore. I&#39;ll try one a month, I think. I&#39;ll see how it goes.
&lt;/p&gt;



&lt;p&gt;
There&#39;s still room for shorter content, such as the short demonstrations of new features, and ideas that don&#39;t have 1,000 words in them. There are also many interesting, although esoteric, features that I would probably never recommend for production code.
&lt;/p&gt;



&lt;p&gt;
This doesn&#39;t mean that I&#39;m going to write less, though. If I do less for &lt;span class=&quot;website&quot;&gt;The Effective Perler&lt;/span&gt;, I can do more somewhere else. There&#39;s another edition of &lt;span class=&quot;book-title&quot;&gt;Intermediate Perl&lt;/span&gt; that needs attention, the &lt;a href=&quot;https://www.learning-perl.com&quot;&gt;Learning Perl&lt;/a&gt; website, or a few other things.
&lt;/p&gt;



&lt;p&gt;
Many people have asked for Items about specific modules, but that&#39;s not really the idea of  &lt;span class=&quot;website&quot;&gt;The Effective Perler&lt;/span&gt;. We want to teach people about core Perl and thinking in Perl. Modules are essentially all the same—they give you an interface and you do what the interface tells you to do. For the most part, they are just subroutines or method calls. There&#39;s not much interesting there. You already know how to do that. The much more interesting advice is researching modules, but we put that in the book already.
&lt;/p&gt;</description>
	</item>
	<item>
		<title>Define grammars in regular expressions</title>
		<link>https://www.effectiveperlprogramming.com/2011/12/define-grammars-in-regular-expressions/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2011/12/define-grammars-in-regular-expressions/</guid>
		<pubDate>Sun, 18 Dec 2011 22:30:20 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-10</category>
		<description>&lt;p&gt;
[&lt;i&gt;This is the 100th Item we&#39;ve shared with you in the two years this blog has been around. We deserve a holiday and we&#39;re taking it, so read us next year! Happy Holidays.&lt;/i&gt;]
&lt;/p&gt;



&lt;p&gt;
Perl 5.10 added rudimentary grammar support in its regular expressions. You could define many subpatterns directly in your pattern, use them to define larger subpatterns, and, finally, when you have everything in place, let Perl do the work. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
There are other ways, some more powerful, that let you do the same thing. This Item is not about those, however, but you can read about &lt;a href=&quot;https://metacpan.org/pod/Regexp::Grammars&quot;&gt;Regexp::Grammars&lt;/a&gt; or &lt;a href=&quot;https://metacpan.org/pod/Parse::RecDescent&quot;&gt;Parse::RecDescent&lt;/a&gt; on your own. Also, you&#39;re not going to get much of a recommendation of which one you should use for your task. We don&#39;t know your situation.
&lt;/p&gt;



&lt;p&gt;
To understand this new syntax, you have to study it from the ground up. It&#39;s not simple, and the terse documentation in &lt;a href=&quot;https://perldoc.perl.org/perlre&quot;&gt;perlre&lt;/a&gt; doesn&#39;t do much to help. 
&lt;/p&gt;


&lt;h2&gt;Referencing a subpattern&lt;/h2&gt;


&lt;p&gt;
The first part you need is the ability to call a named part of the pattern (to label a subpattern, see &lt;span class=&quot;item&quot;&gt;Item 31. Use named captures to label matches&lt;/span&gt;). To re-match a labeled subpattern, you use: 
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;(?&amp;NAME)
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can use that syntax to rerun a subpattern later:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?&amp;lt;cat&gt;Buster|Mimi)
	\s+
	(?&amp;cat)
	/x;

foreach ( &#39;Buster Mimi&#39;, &#39;Mimi Buster&#39;, &#39;Buster&#39;, &#39;Buster Buster&#39; ) {
	say &quot;$_ &quot;, m/$pattern/p ? &quot;matched&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The labeled subpattern has an alternation where either cat name can match. When you reference it again, you re-run the alternation and you can match either cat name again:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster Mimi matched
Mimi Buster matched
Buster nope!
Buster Buster matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is not the same thing as matching the same text a labeled capture group already matched. That&#39;s the &lt;code&gt;\k&amp;lt;NAME&gt;&lt;/code&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;\k&amp;lt;NAME&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This pattern is a different beast. Whichever cat name matches first also has to match second:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?&amp;lt;cat&gt;Buster|Mimi)
	\s+
	\k&amp;lt;cat&gt;
	/x;

foreach ( &#39;Buster Mimi&#39;, &#39;Mimi Buster&#39;, &#39;Buster&#39;, &#39;Buster Buster&#39; ) {
	say &quot;$_ &quot;, m/$pattern/p ? &quot;matched&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now only one of the strings matches because only one string repeats a cat&#39;s name:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster Mimi nope!
Mimi Buster nope!
Buster nope!
Buster Buster matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Although you won&#39;t see it here, the &lt;code&gt;(?&amp;NAME)&lt;/code&gt; syntax is the trick to matching a recursive pattern since the reference can appear inside the pattern it references.
&lt;/p&gt;


&lt;h2&gt;Conditional match&lt;/h2&gt;


&lt;p&gt;
The second building block you need starts with a conditional submatch:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;(?(condition)yes-pattern|no-pattern)
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That &lt;i&gt;condition&lt;/i&gt; can be many things, most of which won&#39;t appear in this Item. Although you see the &lt;code&gt;|&lt;/code&gt; character, but this isn&#39;t an alternation. It&#39;s like an alternation because the &lt;code&gt;|&lt;/code&gt; separates distinct subpatterns, but unlike an alternation because this will only ever try one of the subpatterns and you only get two subpatterns.
&lt;/p&gt;



&lt;p&gt;
The simplest condition is just an ordinal number, which is true only if that capture group matched. Here&#39;s a pattern that has two capture groups:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?:           # parens for grouping
		(B)     # $1
		|         # alternation
		(M)     # $2
	)     
	(?(1)uster|imi) # conditional match
	/x;

foreach ( qw(Mimi Buster Muster Bimi Roscoe) ) {
	say &quot;$_ &quot;, m/$pattern/p ? &quot;matched ${^MATCH}&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In this pattern, if the &lt;code&gt;(B)&lt;/code&gt; matches, the conditional uses &lt;code&gt;uster&lt;/code&gt; from the conditional. Otherwise, it uses &lt;code&gt;imi&lt;/code&gt;. However, the only thing that can match besides a &lt;code&gt;(B)&lt;/code&gt; is the other part of the alternation, the &lt;code&gt;(M)&lt;/code&gt;. The output shows that only &lt;code&gt;Mimi&lt;/code&gt; or &lt;code&gt;Buster&lt;/code&gt; matches:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Mimi matched Mimi
Buster matched Buster
Muster nope!
Bimi nope!
Roscoe nope!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You get the same results if you use &lt;code&gt;(2)&lt;/code&gt; as the condition and re-arrange the order of the patterns:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;my $pattern = qr/
	(?:(B)|(M))
	(?(2)imi|uster)
	/x;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Putting it together&lt;/h2&gt;


&lt;p&gt;
The condition can also be the literal &lt;code&gt;(DEFINE)&lt;/code&gt;. In that case, Perl only allows a yes-branch. And, as its condition implies, it merely &lt;em&gt;defines&lt;/em&gt; the patterns and does not execute them. 
&lt;/p&gt;



&lt;p&gt;
This means that you can create and label the subpatterns that you need, but not actually assert that any of them match the string. The definitions are just there. This pattern defines and labels three subpatterns then uses none of them:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?(DEFINE)
		(?&amp;lt;cat&gt;Buster)
		(?&amp;lt;dog&gt;Addie)
		(?&amp;lt;bird&gt;Poppy)
	)
	Mimi
	/x;

foreach ( &#39;Buster Mimi&#39;, &#39;Roscoe&#39;, &#39;Buster&#39;, &#39;Mimi&#39; ) {
	say &quot;$_ &quot;, m/$pattern/ ? &quot;matched&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s as if the &lt;code&gt;DEFINE&lt;/code&gt; bit is not even there:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster Mimi matched
Roscoe nope!
Buster nope!
Mimi matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Outside the &lt;code&gt;(DEFINE)&lt;/code&gt;, you can reference any of the subpatterns that you created:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?(DEFINE)
		(?&amp;lt;cat&gt;Buster)
		(?&amp;lt;dog&gt;Addie)
		(?&amp;lt;bird&gt;Poppy)
	)
	(?&amp;cat)
	/x;

foreach ( &#39;Buster Mimi&#39;, &#39;Roscoe&#39;, &#39;Buster&#39;, &#39;Mimi&#39; ) {
	say &quot;$_ &quot;, m/$pattern/ ? &quot;matched&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now &lt;code&gt;Buster&lt;/code&gt; matches because you reference that defined  subpattern:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Buster Mimi matched
Roscoe nope!
Buster matched
Mimi nope!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now it&#39;s time for the grammar. Inside the &lt;code&gt;(DEFINE)&lt;/code&gt;, you can reference subpatterns you haven&#39;t defined yet, and your subpatterns can get arbitrarily complex:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $pattern = qr/
	(?(DEFINE)
		(?&amp;lt;male&gt; Buster | Roscoe )
		(?&amp;lt;female&gt; Mimi | Juliet )
		(?&amp;lt;cat&gt; (?&amp;male) | (?&amp;female) )
		(?&amp;lt;dog&gt;Addie)
		(?&amp;lt;bird&gt;Poppy)
	)
	(?&amp;cat)
	/x;

foreach ( &#39;Addie&#39;, &#39;Roscoe&#39;, &#39;Buster&#39;, &#39;Mimi&#39; ) {
	say &quot;$_ &quot;, m/$pattern/ ? &quot;matched&quot; : &#39;nope!&#39;;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Even though the cat names are in two different subpatterns, the &lt;code&gt;cat&lt;/code&gt; subpattern unifies them so all the cat names match:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Addie nope!
Roscoe matched
Buster matched
Mimi matched
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You should now be able to understand this regular expression from &lt;span class=&quot;person&quot;&gt;Tom Christiansen&lt;/span&gt; (appearing on &lt;a href=&quot;https://stackoverflow.com/a/4843579/2766176&quot;&gt;Stackoverflow&lt;/a&gt;). You might have to pick it apart, but you know how all the parts fit together to match the Internet Message Format defined in &lt;a href=&quot;https://www.rfc-editor.org/rfc/rfc5322&quot;&gt;RFC 5322&lt;/a&gt;:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;$rfc5322 = qr{

   (?(DEFINE)

     (?&amp;lt;address&gt;         (?&amp;mailbox) | (?&amp;group))
     (?&amp;lt;mailbox&gt;         (?&amp;name_addr) | (?&amp;addr_spec))
     (?&amp;lt;name_addr&gt;       (?&amp;display_name)? (?&amp;angle_addr))
     (?&amp;lt;angle_addr&gt;      (?&amp;CFWS)? &amp;lt; (?&amp;addr_spec) &gt; (?&amp;CFWS)?)
     (?&amp;lt;group&gt;           (?&amp;display_name) : (?:(?&amp;mailbox_list) | (?&amp;CFWS))? ; (?&amp;CFWS)?)
     (?&amp;lt;display_name&gt;    (?&amp;phrase))
     (?&amp;lt;mailbox_list&gt;    (?&amp;mailbox) (?: , (?&amp;mailbox))*)

     (?&amp;lt;addr_spec&gt;       (?&amp;local_part) \@ (?&amp;domain))
     (?&amp;lt;local_part&gt;      (?&amp;dot_atom) | (?&amp;amp;quoted_string))
     (?&amp;lt;domain&gt;          (?&amp;dot_atom) | (?&amp;domain_literal))
     (?&amp;lt;domain_literal&gt;  (?&amp;CFWS)? \[ (?: (?&amp;FWS)? (?&amp;dcontent))* (?&amp;FWS)?
                                   \] (?&amp;CFWS)?)
     (?&amp;lt;dcontent&gt;        (?&amp;dtext) | (?&amp;amp;quoted_pair))
     (?&amp;lt;dtext&gt;           (?&amp;NO_WS_CTL) | [\x21-\x5a\x5e-\x7e])

     (?&amp;lt;atext&gt;           (?&amp;ALPHA) | (?&amp;DIGIT) | [!#\$%&amp;&#39;*+-/=?^_`{|}~])
     (?&amp;lt;atom&gt;            (?&amp;CFWS)? (?&amp;atext)+ (?&amp;CFWS)?)
     (?&amp;lt;dot_atom&gt;        (?&amp;CFWS)? (?&amp;dot_atom_text) (?&amp;CFWS)?)
     (?&amp;lt;dot_atom_text&gt;   (?&amp;atext)+ (?: \. (?&amp;atext)+)*)

     (?&amp;lt;text&gt;            [\x01-\x09\x0b\x0c\x0e-\x7f])
     (?&amp;lt;quoted_pair&gt;     \\ (?&amp;text))

     (?&amp;lt;qtext&gt;           (?&amp;NO_WS_CTL) | [\x21\x23-\x5b\x5d-\x7e])
     (?&amp;lt;qcontent&gt;        (?&amp;qtext) | (?&amp;amp;quoted_pair))
     (?&amp;lt;quoted_string&gt;   (?&amp;CFWS)? (?&amp;DQUOTE) (?:(?&amp;FWS)? (?&amp;qcontent))*
                          (?&amp;FWS)? (?&amp;DQUOTE) (?&amp;CFWS)?)

     (?&amp;lt;word&gt;            (?&amp;atom) | (?&amp;amp;quoted_string))
     (?&amp;lt;phrase&gt;          (?&amp;word)+)

     # Folding white space
     (?&amp;lt;FWS&gt;             (?: (?&amp;WSP)* (?&amp;CRLF))? (?&amp;WSP)+)
     (?&amp;lt;ctext&gt;           (?&amp;NO_WS_CTL) | [\x21-\x27\x2a-\x5b\x5d-\x7e])
     (?&amp;lt;ccontent&gt;        (?&amp;ctext) | (?&amp;amp;quoted_pair) | (?&amp;comment))
     (?&amp;lt;comment&gt;         \( (?: (?&amp;FWS)? (?&amp;ccontent))* (?&amp;FWS)? \) )
     (?&amp;lt;CFWS&gt;            (?: (?&amp;FWS)? (?&amp;comment))*
                         (?: (?:(?&amp;FWS)? (?&amp;comment)) | (?&amp;FWS)))

     # No whitespace control
     (?&amp;lt;NO_WS_CTL&gt;       [\x01-\x08\x0b\x0c\x0e-\x1f\x7f])

     (?&amp;lt;ALPHA&gt;           [A-Za-z])
     (?&amp;lt;DIGIT&gt;           [0-9])
     (?&amp;lt;CRLF&gt;            \x0d \x0a)
     (?&amp;lt;DQUOTE&gt;          &quot;)
     (?&amp;lt;WSP&gt;             [\x20\x09])
   )

   (?&amp;address)

}x;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If that&#39;s not clever enough for you, try &lt;a href=&quot;https://stackoverflow.com/a/4286326/2766176&quot;&gt;&lt;span class=&quot;person&quot; data-fullname=&quot;Tom Christiansen&quot;&gt;Tom&lt;/span&gt;&#39;s use of &lt;code&gt;(DEFINE)&lt;/code&gt; to properly parse HTML&lt;/a&gt;.
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can reference a named subpattern with &lt;code&gt;(?&amp;NAME)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You can choose a subpattern with a condition &lt;code&gt;(?(condition)yes-pattern|no-pattern)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You can define and label subpatterns for later use with &lt;code&gt;(DEFINE)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Loose match Unicode character names</title>
		<link>https://www.effectiveperlprogramming.com/2011/12/loose-match-unicode-character-names/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2011/12/loose-match-unicode-character-names/</guid>
		<pubDate>Wed, 14 Dec 2011 10:43:51 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>5-16</category>
		<description>&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/charnames&quot;&gt;charnames&lt;/a&gt; module can now handle loose name matching, as outlined in &lt;a href=&quot;https://www.unicode.org/reports/tr44/#Matching_Rules&quot;&gt;Unicode Standard Annex #44&lt;/a&gt;. This accounts for the various ways people are abusing things.
&lt;/p&gt;



&lt;p&gt;
Consider the character 😻, (U+1F63B SMILING CAT FACE WITH HEART-SHAPED EYES). If you want to interpolate that into a string, you have to use the exact name:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;
use open qw(:std :utf8);

say &quot;\N{SMILING CAT FACE WITH HEART-SHAPED EYES}&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Starting with v5.16, the &lt;code&gt;\N{}&lt;/code&gt; in a double-quoted string automatically imports &lt;code&gt;:long&lt;/code&gt; and &lt;code&gt;:short&lt;/code&gt;. There&#39;s another one that you can import yourself, but it&#39;s a bit costly.
&lt;/p&gt;



&lt;p&gt;
Some people don&#39;t like all uppercase strings, so they might want to type it out as title or lowercase:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;
use open qw(:std :utf8);

say &quot;\N{Smiling Cat Face With Heart-Shaped Eyes}&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That doesn&#39;t work and you get an error:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Unknown charname &#39;Smiling Cat Face With Heart-Shaped Eyes&#39;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Import &lt;code&gt;:loose&lt;/code&gt; from &lt;a href=&quot;https://metacpan.org/pod/charnames&quot;&gt;charnames&lt;/a&gt; and it will work:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;
use open qw(:std :utf8);
use charnames qw(:loose);

say &quot;\N{Smiling Cat Face With Heart-Shaped Eyes}&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The loose naming rules involve three things, which makes the loose matching slow:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Ignore case folding&lt;/li&gt;
&lt;li&gt;Ignore whitespace&lt;/li&gt;
&lt;li&gt;Ignore &quot;medial&quot; hyphens (letters on either side)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
So all of these work, even the one with consecutive hyphens:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.16;
use open qw(:std :utf8);
use charnames qw(:loose);

say &quot;\N{Smiling Cat Face With Heart Shaped Eyes}&quot;;
say &quot;\N{SmilingCatFaceWithHeartShapedEyes}&quot;;
say &quot;\N{Smiling-Cat-Face-With-Heart-Shaped-Eyes}&quot;;
say &quot;\N{Smiling----Cat-Face-----With-Heart-----Shaped-Eyes}&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Some problematic names&lt;/h2&gt;


&lt;p&gt;
This doesn&#39;t work out well for some names, and Perl developer &lt;span class=&quot;person&quot;&gt;Karl Williamson&lt;/span&gt; made &lt;a href=&quot;https://unicode.org/L2/L2010/10151-loose-name-match.pdf&quot;&gt;some comments about this to the Unicode Consortium in 2010&lt;/a&gt;. There are some names that have hyphens next to whitespace (so, not medial hyphens), but if you ignore whitespace first, then the hyphen isn&#39;t next to whitespace.
&lt;/p&gt;



&lt;p&gt;
Not only that, removing the hyphen can turn a character&#39;s name into that of a completely different character:
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;U+0F68 TIBETAN LETTER A&lt;/li&gt;
&lt;li&gt;U+0F60 TIBETAN LETTER -A&lt;/li&gt;
&lt;li&gt;U+0FB8 TIBETAN SUBJOINED LETTER A&lt;/li&gt;
&lt;li&gt;U+0FB0 TIBETAN SUBJOINED LETTER -A&lt;/li&gt;
&lt;li&gt;U+116C HANGUL JUNGSEONG OE&lt;/li&gt;
&lt;li&gt;U+1180 HANGUL JUNGSEONG O-E&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Create your own dualvars</title>
		<link>https://www.effectiveperlprogramming.com/2011/12/create-your-own-dualvars/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2011/12/create-your-own-dualvars/</guid>
		<pubDate>Sun, 11 Dec 2011 13:36:37 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>item</category>
		<description>&lt;p&gt;
Perl&#39;s basic data type is the scalar, which takes its name from the mathematical term for &quot;single item&quot;. However, the scalar is really two things. You probably know that a scalar can be either a number or a string, or a number that looks the same as its string, or a string that can be a number. What you probably don&#39;t know is that a scalar can be two separate and unrelated values at the same time, making it a &lt;i&gt;dualvar&lt;/i&gt;. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
You&#39;re already using a dualvar without knowing it. The &lt;code&gt;$!&lt;/code&gt; variable, which holds the value of the last system error, is most often used in its string form:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open my $fh, &#39;&gt;&#39;, $filename or die &quot;Error: $!&quot;;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If something goes wrong with that &lt;code&gt;open&lt;/code&gt;, you&#39;ll get an error such as these:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;No such file or directory
Permission denied
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Both of those error messages have numbers associated with them. You can output their numeric value:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;open my $fh, &#39;&gt;&#39;, $filename or die $! + 0;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the errors are numbers, which correspond to the &lt;code&gt;errno&lt;/code&gt; value for the system call:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;2
13
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
These numbers are keys in the &lt;code&gt;%!&lt;/code&gt;, and the value for that key is true if that was the last system error.
&lt;/p&gt;



&lt;p&gt;
Perl, on its own, doesn&#39;t give you a way to create this sort of variable yourself. When you assign a new value to a scalar, whether string or number, Perl clears the previous values it has. When you use a number as a string, Perl converts it to a string, and the same the other way around.
&lt;/p&gt;



&lt;p&gt;
You can watch this with &lt;a href=&quot;https://metacpan.org/pod/Devel::Peek&quot;&gt;Devel::Peek&lt;/a&gt;. Here&#39;s a program that sets a string value:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;

my $value = &#39;abc&#39;;

Dump( $value );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
In the scalar record, the &lt;code&gt;POK&lt;/code&gt; flag is set, indicating the variable has a string form, and the &lt;code&gt;PV&lt;/code&gt; slot has a value (see &lt;a href=&quot;https://perldoc.perl.org/perlguts&quot;&gt;perlguts&lt;/a&gt; for more details):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PV(0x100801070) at 0x100827810
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x100202870 &quot;abc&quot;\0
  CUR = 3
  LEN = 16
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you set a numeric value, the flags are different:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Devel::Peek;

my $value = 137;

Dump( $value );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now there&#39;s an &lt;code&gt;IOK&lt;/code&gt; flag, and one of the numeric slots, in this case the &lt;code&gt;IV&lt;/code&gt;, is set:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = IV(0x100827800) at 0x100827810
  REFCNT = 1
  FLAGS = (PADMY,IOK,pIOK)
  IV = 137
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
However, if you take the numeric value and use it in a string context, Perl also creates a string version. Now the scalar has both the &lt;code&gt;IOK&lt;/code&gt; and &lt;code&gt;POK&lt;/code&gt; flags, and both the &lt;code&gt;IV&lt;/code&gt; and &lt;code&gt;PV&lt;/code&gt; slots have values:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;137
SV = PVIV(0x100809208) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
  IV = 137
  PV = 0x100202870 &quot;137&quot;\0
  CUR = 3
  LEN = 16
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you change the variable, some of those flags disappear, even though the values don&#39;t necessarily disappear:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Devel::Peek;

my $value = 137;

$value = &#39;Buster&#39;;

Dump( $value );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the scalar&#39;s value should be just &lt;code&gt;Buster&lt;/code&gt;, but the &lt;code&gt;IV&lt;/code&gt; slot still has the old &lt;code&gt;137&lt;/code&gt;. However, the &lt;code&gt;IOK&lt;/code&gt; flag is gone:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PVIV(0x100809208) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  IV = 137
  PV = 0x100202870 &quot;Buster&quot;\0
  CUR = 6
  LEN = 16
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When you set the new value, and Perl hadn&#39;t used it in a numeric context yet, there was no need to go through the work to translate the string value to the number value. Instead, it just unset the flag that denotes it&#39;s okay to use the value as a number. You have to use it as a number to set that flag again:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Devel::Peek;

my $value = 137;

$value = &#39;Buster&#39;;

say $value + 0;

Dump( $value );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now Perl converts it to a number and sets the numeric flags again:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;0
SV = PVNV(0x100801e30) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,POK,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x100202870 &quot;Buster&quot;\0
  CUR = 6
  LEN = 16
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
That&#39;s how it works if you go through the Perl interface, but if you hand it a scalar through the XS interface, you can set whatever flags and values that you like. That&#39;s exactly what &lt;a href=&quot;https://metacpan.org/pod/Scalar::Util&quot;&gt;Scalar::Util&lt;/a&gt;&#39;s &lt;code&gt;dualvar&lt;/code&gt; does for you:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;
use Devel::Peek;
use Scalar::Util qw(dualvar);

my $value = dualvar 137, &#39;Buster&#39;;

Dump( $value );

say &quot;$value&quot;;
say $value + 0;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now you have a scalar which has unrelated numeric and string values, &lt;em&gt;and&lt;/em&gt; the flags for both values are set:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;SV = PVNV(0x100802010) at 0x1008277c8
  REFCNT = 1
  FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
  IV = 137
  NV = 0
  PV = 0x100202870 &quot;Buster&quot;\0
  CUR = 6
  LEN = 16
Buster
137
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Scalars can have both numeric and string values at the same time&lt;/li&gt;
&lt;li&gt;Those two values can be unrelated&lt;/li&gt;
&lt;li&gt;You can create your own dualvar with &lt;a href=&quot;https://metacpan.org/pod/Scalar::Util&quot;&gt;Scalar::Util&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Make disposable web servers for testing</title>
		<link>https://www.effectiveperlprogramming.com/2011/12/make-disposable-web-servers-for-testing/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2011/12/make-disposable-web-servers-for-testing/</guid>
		<pubDate>Sun, 04 Dec 2011 07:42:50 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>item</category>
		<description>&lt;p&gt;
If your project depends on an interaction with a web server, especially a remote one, you have some challenges with testing that portion. Even if you can get it working for you, when you distribute your code, someone else might not be able to reach your server for testing. Instead of relying on an external server, you can use a local server that you write especially for your test suite. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
This problem has a couple of tricky parts. To run a test server from your own test suite, your server needs to bind to a port that&#39;s not already in use. When it has a port, it needs to communicate that to your test script.
&lt;/p&gt;



&lt;p&gt;
Another problem, which you won&#39;t consider for this Item, involves the configurability of your program so you can change the hostname and port during the test. This Item assumes you&#39;ve taken care of that bit.
&lt;/p&gt;



&lt;p&gt;
The &lt;a href=&quot;https://metacpan.org/pod/Test::Fake::HTTPD&quot;&gt;Test::Fake::HTTPD&lt;/a&gt; module can create a web server directly from your test script. This example creates a web server that returns the same JSON response for every request:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Test::More;
use Test::Fake::HTTPD;

use Mojo::UserAgent;

my $httpd = run_http_server {
	my $request = shift;

	return [ 
		200, 
		[ &#39;Content-Type&#39; =&gt; &#39;application/json&#39; ], 
		[ &#39;{ &quot;cat&quot;: &quot;Buster&quot; }&#39; ] 
		];
	};

ok( defined $httpd, &#39;Got a web server&#39; );

diag( sprintf &quot;You can connect to your server at %s.\n&quot;, $httpd-&gt;host_port );

my $response = Mojo::UserAgent-&gt;new-&gt;get(
	$httpd-&gt;endpoint
	)-&gt;res;

diag( $response-&gt;to_string );
is( $response-&gt;json-&gt;{cat}, &#39;Buster&#39;, &#39;Cat is Buster&#39; );

done_testing();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The test output shows a message telling you the host and port, as well as the response:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;ok 1 - Got a web server
# You can connect to your server at 127.0.0.1:50602.
# HTTP/1.1 200 OK
# Content-Type: application/json
# Date: Wed, 07 Dec 2011 08:44:40 GMT
# Content-Length: 19
# Server: libwww-perl-daemon/6.00
# 
# { &quot;cat&quot;: &quot;Buster&quot; }
ok 2 - Cat is Buster
1..2
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
When the test script ends (or the web server variable goes out of scope), so there&#39;s nothing for you to clean up. It&#39;s a disposable web server.
&lt;/p&gt;



&lt;p&gt;
In your web server, you can do anything that you like. It doesn&#39;t have to implement everything, or even close to everything, that the production server does. It just has to return responses that you can use in your tests. That means that you get to control not only the success, but also the failures.
&lt;/p&gt;



&lt;p&gt;
That &lt;code&gt;Server&lt;/code&gt; line gives you a hint about what created the &lt;code&gt;$request&lt;/code&gt; object—it&#39;s &lt;a href=&quot;https://metacpan.org/pod/LWP&quot;&gt;LWP&lt;/a&gt; behind the scenes, so it&#39;s &lt;a href=&quot;https://metacpan.org/pod/HTTP::Request&quot;&gt;HTTP::Request&lt;/a&gt;. You can get the requested path with the &lt;code&gt;uri&lt;/code&gt; method and decide what to do:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use strict;
use warnings;

use Test::More;
use Test::Fake::HTTPD;

use Mojo::UserAgent;
use URI;

my $httpd = run_http_server {
	my $request = shift;

	my $uri = $request-&gt;uri;

	return do {
		if( $uri-&gt;path eq &#39;/&#39; ) {
			[ 
				200, 
				[ &#39;Content-Type&#39; =&gt; &#39;text/plain&#39; ], 
				[ &quot;Ask about our cats!&quot; ], 
			]
			}
		elsif( $uri-&gt;path eq &#39;/cats&#39; ) {
			[ 
				200, 
				[ &#39;Content-Type&#39; =&gt; &#39;application/json&#39; ], 
				[ &#39;{ &quot;cat&quot;: &quot;Buster&quot; }&#39; ], 
			]
			}
		elsif( $uri-&gt;path eq &#39;/dogs&#39; ) {
			[ 
				408, 
				[ &#39;Content-Type&#39; =&gt; &#39;text/plain&#39; ], 
				[ &quot;We don&#39;t walk dogs&quot; ], 
			]
			}
		else {
			[ 
				404, 
				[ &#39;Content-Type&#39; =&gt; &#39;text/plain&#39; ], 
				[ &quot;Not Found&quot; ], 
			]
			}
		}		
	};

ok( defined $httpd, &#39;Got a web server&#39; );
diag( sprintf &quot;You can connect to your server at %s.\n&quot;, $httpd-&gt;host_port );

my $uri = URI-&gt;new( $httpd-&gt;endpoint );
isa_ok( $uri, &#39;URI&#39; );


subtest &#39;/&#39; =&gt; sub {
	plan tests =&gt; 3;
	
	my $this = $uri-&gt;clone;
	isa_ok( $this, &#39;URI&#39; );
	$this-&gt;path( &#39;/&#39; );
	
	my $response = Mojo::UserAgent-&gt;new-&gt;get( $this )-&gt;res;
	
	is( $response-&gt;headers-&gt;content_type, 
		&#39;text/plain&#39;, &#39;Top level is plain text&#39; );
	like( $response-&gt;body , qr/cats/, &#39;Top level has cats&#39; );
	};
	
subtest &#39;/cats&#39; =&gt; sub {
	plan tests =&gt; 2;
	
	my $this = $uri-&gt;clone;
	isa_ok( $this, &#39;URI&#39; );
	$this-&gt;path( &#39;/cats&#39; );
	
	my $response = Mojo::UserAgent-&gt;new-&gt;get( $this )-&gt;res;
	
	is( $response-&gt;headers-&gt;content_type, 
		&#39;application/json&#39;, &#39;/cats returns JSON&#39; );
	};

subtest &#39;/not_there&#39; =&gt; sub {
	plan tests =&gt; 2;
	
	my $this = $uri-&gt;clone;
	isa_ok( $this, &#39;URI&#39; );
	$this-&gt;path( &#39;/not_there&#39; );
	
	my $response = Mojo::UserAgent-&gt;new-&gt;get( $this )-&gt;res;

	is( $response-&gt;code, 
		&#39;404&#39;, &#39;/not_there returns 404&#39; );
	};

done_testing();
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
With subtests organized around accesses to paths, the TAP isn&#39;t so hard to read (although you probably won&#39;t have to look at it  yourself):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;ok 1 - Got a web server
# You can connect to your server at 127.0.0.1:50498.
ok 2 - The object isa URI
    1..3
    ok 1 - The object isa URI
    ok 2 - Top level is plain text
    ok 3 - Top level has cats
ok 3 - /
    1..2
    ok 1 - The object isa URI
    ok 2 - /cats returns JSON
ok 4 - /cats
    1..2
    ok 1 - The object isa URI
    ok 2 - /not_there returns 404
ok 5 - /not_there
1..5
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you need to use the same test webserver in more than one test script, you move it into its own file.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use strict;
use warnings;

use Test::More;

use Mojo::UserAgent;
use URI;

require &#39;server.pl&#39;;
my $httpd = get_http_server();

ok( defined $httpd, &#39;Got a web server&#39; );
diag( sprintf &quot;You can connect to your server at %s.\n&quot;, $httpd-&gt;host_port );

my $uri = URI-&gt;new( $httpd-&gt;endpoint );
isa_ok( $uri, &#39;URI&#39; );

...
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The &lt;span class=&quot;filename&quot;&gt;server.pl&lt;/span&gt; file wraps the call to &lt;code&gt;run_http_server&lt;/code&gt;. But, if you are going to do that, you might as well skip the convenience method and set up your own object. You can change the timeout value, for instance:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Test::Fake::HTTPD;

sub get_http_server {
	my $httpd = Test::Fake::HTTPD-&gt;new(
		timeout =&gt; 30,
		);

	$httpd-&gt;run( sub {
		my $request = shift;
		...;		
		} );
		
	$httpd;
	}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Different test scripts each get their own test web server. If you&#39;re running several test scripts in parallel, you&#39;ll start several servers at the same time, which might not be that kind to your system or to the other people using it. If you don&#39;t like that, you could set up a single server at the start of your test run, share it with all tests, and shut down everything at the end, although you won&#39;t see that in this Item.
&lt;/p&gt;


&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Test web interactions locally&lt;/li&gt;
&lt;li&gt;Use &lt;a href=&quot;https://metacpan.org/pod/Test::Fake::HTTPD&quot;&gt;Test::Fake::HTTPD&lt;/a&gt; to create cheap, disposable web servers&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
	<item>
		<title>Know split&#39;s special cases</title>
		<link>https://www.effectiveperlprogramming.com/2011/11/know-splits-special-cases/</link>
		<guid isPermaLink="true">https://www.effectiveperlprogramming.com/2011/11/know-splits-special-cases/</guid>
		<pubDate>Sun, 27 Nov 2011 00:09:00 GMT</pubDate>
		<dc:creator>brian d foy</dc:creator>
		<category>idiomatic-perl</category>
		<description>&lt;p&gt;
Perl&#39;s &lt;a href=&quot;https://perldoc.perl.org/functions/split&quot;&gt;split&lt;/a&gt; has some special cases and some perhaps surprising cases. The empty pattern, zero width match, the special argument &lt;code&gt;&#39; &#39;&lt;/code&gt;, and the &lt;code&gt;/^/&lt;/code&gt; act differently than you might expect from the general rule. 
&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2&gt;The empty pattern, //&lt;/h2&gt;


&lt;p&gt;
The empty pattern is a special case that&#39;s designed to give you a list of characters. This pattern specifically has nothing in it and is different than a pattern that matches an empty string (that&#39;s next). For this, &lt;a href=&quot;https://perldoc.perl.org/functions/split&quot;&gt;split&lt;/a&gt; returns a list of characters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;

use Data::Printer;
my @characters = split //, &#39;Büster&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The output shows a list of characters:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;B&quot;,
    [1] &quot;ü&quot;,
    [2] &quot;s&quot;,
    [3] &quot;t&quot;,
    [4] &quot;e&quot;,
    [5] &quot;r&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This is specifically characters, not grapheme clusters. Depending on the normalization of your source code or input, you can get different results:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use utf8;

use Data::Printer;
use Unicode::Normalize qw(NFD);

my @characters = split //, NFD( &#39;Büster&#39; );

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now the grapheme cluster &lt;i&gt;ü&lt;/i&gt; is actually two characters, the &lt;i&gt;u&lt;/i&gt; (U+0075 LATIN SMALL LETTER U) and the &lt;i&gt;¨&lt;/i&gt; (U+0308 COMBINING DIAERESIS), instead of the single &lt;i&gt;ü&lt;/i&gt; (U+00FC LATIN SMALL LETTER U WITH DIAERESIS):
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;B&quot;,
    [1] &quot;u&quot;,
    [2] &quot;¨&quot;,
    [3] &quot;s&quot;,
    [4] &quot;t&quot;,
    [5] &quot;e&quot;,
    [6] &quot;r&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You can review grapheme clusters in &lt;a href=&quot;/2011/06/treat-unicode-strings-as-grapheme-clusters/&quot;&gt;Treat Unicode strings as grapheme clusters&lt;/a&gt;.
&lt;/p&gt;


&lt;h2&gt;Matching the empty string&lt;/h2&gt;


&lt;p&gt;
Successfully matching no characters isn&#39;t really a special case, but people are sometimes surprised about it because it seems special. Your &lt;a href=&quot;https://perldoc.perl.org/functions/split&quot;&gt;split&lt;/a&gt; pattern might match an empty string. This is different from the empty pattern because you actually have a pattern, even if it might match zero characters. This is also distinct from a pattern that doesn&#39;t match:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use v5.10;

my $_ = &#39;Mimi&#39;;

say &quot;Matched empty pattern&quot; if //;
say &quot;Matched optional whitespace&quot; if /\s*/;
say &quot;Matched zero width assertion&quot; if /(?=\w+)/;
say &quot;How did Buster match?&quot; if /Buster/;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The first three of these patterns match successfully but match zero characters, while the fourth fails:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;Matched empty pattern
Matched optional whitespace
Matched zero width assertion
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
It&#39;s easy to construct a pattern that will match zero characters even though it matches successfully. The &lt;code&gt;?&lt;/code&gt; (zero or one) and &lt;code&gt;*&lt;/code&gt; (zero or more) quantifiers do that quite nicely. Zero width assertions, such as the boundaries and lookarounds, do that too. If the pattern can match zero characters successfully, Perl splits into characters:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;
my @characters = split /\s*/, &#39;Buster&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;

&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;B&quot;,
    [1] &quot;u&quot;,
    [2] &quot;s&quot;,
    [3] &quot;t&quot;,
    [4] &quot;e&quot;,
    [5] &quot;r&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The pattern doesn&#39;t have to match zero characters for all separators.
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;
my @characters = split /\s*/, &#39;Buster and Mimi&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Notice that there are no spaces in &lt;code&gt;@characters&lt;/code&gt;, since &lt;a href=&quot;https://perldoc.perl.org/functions/split&quot;&gt;split&lt;/a&gt; matched those as separator characters:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0]  &quot;B&quot;,
    [1]  &quot;u&quot;,
    [2]  &quot;s&quot;,
    [3]  &quot;t&quot;,
    [4]  &quot;e&quot;,
    [5]  &quot;r&quot;,
    [6]  &quot;a&quot;,
    [7]  &quot;n&quot;,
    [8]  &quot;d&quot;,
    [9]  &quot;M&quot;,
    [10] &quot;i&quot;,
    [11] &quot;m&quot;,
    [12] &quot;i&quot;
]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;The single space, &#39; &#39;&lt;/h2&gt;


&lt;p&gt;
The single space in quotes, single or double, is a special case. It splits on whitespace, but unlike the pattern that is a single space, the one in quotes discards empty leading fields:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;
my @characters = split &#39; &#39;, &#39;  Buster and Mimi&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You get just the non-whitespace with no empty fields:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;Buster&quot;,
    [1] &quot;and&quot;,
    [2] &quot;Mimi&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This behavior comes from &lt;span class=&quot;binary&quot;&gt;awk&lt;/span&gt;:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;#!/usr/bin/awk -f
BEGIN {
    string=&quot;  Buster Mimi Roscoe&quot;;
    search=&quot; &quot;;
    n=split(string,array,&quot; &quot;);
    print(&quot;[&quot;);
    for (i=1;i&lt;=n;i++) {
        printf(&quot;    [%d] \&quot;%s\&quot;\n&quot;,i,array[i]);
    }
    print(&quot;]&quot;);
    exit;
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
You end up with almost the same output, although the indices are one greater:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [1] &quot;Buster&quot;
    [2] &quot;Mimi&quot;
    [3] &quot;Roscoe&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Back in Perl, if you tried that with the normal match operator delimiters, you get a different result:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;
my @characters = split / /, &#39;  Buster and Mimi&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This time you kept the empty leading fields:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;&quot;,
    [1] &quot;&quot;,
    [2] &quot;Buster&quot;,
    [3] &quot;and&quot;,
    [4] &quot;Mimi&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
If you include the &lt;code&gt;m&lt;/code&gt; in front of the quotes though, you lose the special magic:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;
my @characters = split m&#39; &#39;, &#39;  Buster and Mimi&#39;;

p( @characters );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
The empty leading fields are back:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;&quot;,
    [1] &quot;&quot;,
    [2] &quot;Buster&quot;,
    [3] &quot;and&quot;,
    [4] &quot;Mimi&quot;
]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Splitting lines&lt;/h2&gt;


&lt;p&gt;
The special pattern of just the beginning-of-line anchor, even without the &lt;code&gt;/m&lt;/code&gt; flag, breaks a multi-line string into lines:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;use Data::Printer;

my $string = &lt;&lt;&#39;HERE&#39;;
Line one
Line two
Line three
HERE
my @lines = split /^/, $string;

p( @lines );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Even without the &lt;code&gt;/m&lt;/code&gt; you get separate lines:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;Line one
&quot;,
    [1] &quot;Line two
&quot;,
    [2] &quot;Line three
&quot;
]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
This only works if the pattern is exactly &lt;code&gt;/^/&lt;/code&gt;. If you put anything else in the pattern, you don&#39;t get the special behavior, even if it&#39;s a zero width match:
&lt;/p&gt;


&lt;pre class=&quot;perl-code-listing&quot;&gt;&lt;code class=&quot;language-perl&quot;&gt;...; # same as before

my @lines = split /^(?=Line)/, $string;  # Oops

p( @lines );
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
Now there&#39;s only one field:
&lt;/p&gt;


&lt;pre class=&quot;plain-code-listing&quot;&gt;&lt;code class=&quot;language-plain&quot;&gt;[
    [0] &quot;Line one
Line two
Line three
&quot;
]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Things to remember&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The empty pattern &lt;code&gt;//&lt;/code&gt; splits on characters, but not grapheme clusters&lt;/li&gt;
&lt;li&gt;A zero width successful match splits on characters too&lt;/li&gt;
&lt;li&gt;The single space in quotes splits on whitespace and discards leading empty fields&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;^&lt;/code&gt; anchor by itself splits into lines, even without the &lt;code&gt;/m&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
	</item>
</channel>
</rss>
