📘 Prefix operator ++ in Perl 6

++ is a prefix operator of increment. First, an increment is done, and then a new value is returned.

my $x = 41;
say ++$x; # 42

The increment operation is not limited to working only with numbers. It can also handle strings.

my $a = 'a';
say ++$a; # b

A practical example is to increment filenames containing numbers. The file extension will survive, and only the numerical part will be incremented.

my $f = "file001.txt"; 

++$f;
say $f; # file002.txt 

++$f;
say $f; # file003.txt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s