# Tips for golfing in Perl?

What general tips do you have for golfing in Perl? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Perl (e.g. "remove comments" is not an answer). Please post one tip per answer.

## TMTOWTDI

That's the most important Perl golfing tip you need to know. Whenever you're looking at some too-long sequence of characters you absolutely have to have in order to accomplish your task, ask yourself if there isn't some other way to get the same effect using a different feature. There usually is. Here are just a handful:

• ~~ enforces a scalar context and is 4 chars shorter than scalar.

• y///c is one char shorter than length when getting the length of $_. • Need to iterate over the chars in $_? Replace split// with /./gs. (Or use /./g if you also want to skip newlines.) This works with other variables: replace split//,$x with $x=~/./gs.

• Every Perl builtin returns something. print returns 1, for example, to indicate successful I/O. If you need to initialize $_ to a true value, for example, $_=print$foo allows you to kill two birds with one stone. • Almost every statement in Perl can be written as an expression, allowing it to be to used in a wider variety of contexts. Multiple statements can be become multiple expressions chained together with commas. Tests can be done with short-circuiting operators ?: && ||, and also with and and or, which do the same thing but with precedence lower than all other operators (including assignment). Loops can be done via map or grep. Even keywords like next, last and return can be used in an expression context, even though they don't return! Keeping these kinds of transformations in mind give you opportunities to replace code blocks with expressions that can be stuffed into a wider variety of contexts. • $_=print"" is shorter than $_=print$foo. – ASCIIThenANSI Dec 18 '15 at 18:23
• The idea is that you already need to print $foo. Otherwise, $_=1 is much shorter than $_=print"" and has the same effect. – breadbox Feb 19 '16 at 23:59 • For the third do you mean iterating over the chars in $x? Otherwise you could just do /./gs and /./g. – redbmk Apr 14 '16 at 10:44

Abuse Perl's special variables!

• As noted in a previous answer $/ and $" are initialized by default to "\n" and " ", respectively.

• $, and $\ are both set to undef by default, and are 3 chars shorter.

• Setting $\ to a value will cause it to be appended to every print. For example: perl -ple '$\="=".hex.$/' is a handy hex-to-decimal converter. • If you're not reading files from the command-line, you can use the -i command-line switch as an extra channel for inputting a string. Its value will be stored in $^I.

• $= forces whatever is assigned to it to be an integer. Try running perl -ple '$_=$==$_' and giving it various inupts. Likewise, $- forces its value to be a non-negative integer (i.e. a leading dash is treated as a non-numeric character). • You can use $. as a boolean flag that is automatically reset to a true (nonzero) value on every iteration of a while(<>) loop.

# -n and unmatched curly brackets

It is well known that the command line switch -n can be used to execute the script once for every line.

perl --help says:

  -n                assume "while (<>) { ... }" loop around program


What it doesn't say explicitly is that Perl doesn't just assume a loop around the program; it literally wraps while (<>) { ... } around it.

This way, the following commands are equivalent to each other:

 perl -e 'while(<>){code}morecode'
perl -ne 'code;END{morecode}'
perl -ne 'code}{morecode'


# -p and unmatched curly brackets

Similarly to the above, the -p switch wraps while (<>) { ... ; print } around the program.

By using unmatched curly brackets, perl -p 'code}{morecode' will only print once after executing code for all lines of input, followed by morecode.

Since $_ is undefined when morecode;print is executed, the output record separator $\ can be abused to print the actual output.

For example

perl -pe '$\+=$_}{'


reads one number per line from STDIN and prints their sum.

• I'm assuming you could accomplish this with #!perl -n on the first line, right? – ASCIIThenANSI May 2 '15 at 16:56
• @ASCIIThenANSI: Yes, that is correct. (Sorry for the late answer.) – Dennis Jun 14 '15 at 15:22
• Giving credit where credit is due, I think I saw the combination of these three tricks (unmatched curly brackets, -p and $\​) for the first time in one of @primo's Perl answers. To read his answers is a good Perl tip on its own. – Dennis Jun 14 '15 at 15:24 • Throwing a }for(...){ in between the braces is often quite handy as well, e.g. codegolf.stackexchange.com/a/25632 – primo Oct 2 '15 at 9:53 • One thing I found useful in conjunction with -n is the ||= default value assignment operator. Gets around not being able to assign a value before the loop. – deltaray Dec 17 '16 at 5:55 Use $_ to eliminate scalar references. It is the special variable that is used as a default by most functions, and just leaving out parameters is a shortcut to reference this variable.

By changing $n to $_, you can change $n=<>;chop$n;print$n to $_=<>;chop;print

Here, the print function prints the contents of $_ by default, and chop also works on $_.

• Is $_=<>; required, doesn't <>; read the line into $_ automatically? – sundar - Reinstate Monica Aug 20 '13 at 18:55
• No, I don't think so. I compared the programs $_=<>;print and <>;print. The first one repeats back to me what I type, while the other one doesn't. – PhiNotPi Aug 21 '13 at 21:22 • Oh, thanks, turns out that happens only in cases like print while(<>). Not sure if it's a special case or there's some coherent logic behind it, neither <>'s part in perlop nor while part of perlsyn seem to mention this behaviour. – sundar - Reinstate Monica Aug 21 '13 at 22:36 • @sundar while(<>) is a special case, documented in perlsyn, I/O Operators: 'If and only if the input symbol is the only thing inside the conditional of a "while" statement (even if disguised as a "for(;;)" loop), the value is automatically assigned to the global variable$_, destroying whatever was there previously." – kernigh Nov 9 '14 at 0:22

Use Perl's special variables where-ever you can, eg:

• Use $" instead of " " • Use $/ instead of "\n"

They have the added benefit of being a guaranteed one-character long identifier, with help from the lexer. This makes it possible to glue it to the keyword following it, as in: print$.for@_ The list of all the special variables is available here: Special Variables Don't use qw. This is waste of two characters that could be used in better way. For example, don't write the following. @i=qw(unique value);  Instead use barewords. @i=(unique,value);  Or if you cannot use barewords, use glob syntax. @i=<unique value>;  glob syntax can also be used for interesting effects. @i=<item{1,2,3}>;  Use statement modifiers instead of compound statements. Compound statements tend to require parentheses for the argument and braces for the block, whereas statement modifiers need neither. Compare: • $a++,$b++while$n-- vs while($n--){$a++;$b++} • chop$,if$c vs if($c){chop$,} Note that the last example ties with $c&&chop$,, but starts really shining for most multi-statement operations. Basically anything that loses operator precedence to &&. Don't use strict. (don't quote me on this, PCG.SE context kinda matters) And, more importantly, don't code as if under strict. The usual suspects: • don't my-declare variables if you can avoid it. The only variables that really need my are those you want lexically scoped. That's barely any of them when golfing, where you don't need scope protection and tend to fully control recursion. • don't quote one-word strings: (example). Do ensure you don't have a function with the same name, though. • print hello won't work. It actually means print hello$_ (print $_ to filehandle hello). – Konrad Borowski Oct 22 '12 at 12:25 • @GlitchMr thanks! (and now I'm bummed, because my point is still valid, just not with print, and now I can't find a nice and short example) – J B Oct 22 '12 at 18:10 • @JB here's a good example: codegolf.stackexchange.com/a/8746/3348 – ardnew Oct 22 '12 at 19:45 I'm sure some of these have formal names and I'm just not aware of them. • If you have a while loop (or a for loop you can make into a while loop) you can define the "while" after the command: print$n++ while ($n < 10) • If you need to read everything from STDIN into a string: $var = join('',<>)
• As CeilingSpy pointed out, using $/ instead of \n is faster in some situations: print ('X'*10) . "\n"; is longer than print ('X'*10) .$/;
• Perl's say function is shorter than print, but you'll have to run the code with -E instead of -e
• Use ranges like a..z or even aa..zz. If needed as a string, use join.
• Incrementing strings: $z = 'z'; print ++$z; will display aa

That's all I can think of right now. I may add some more later.

• What is print ('X'*10) . $/; supposed to do? For me it prints 0 and no newline. For one thing, the parentheses become a function-style call argument to print, which binds tighter than .. And did you mean x instead of * or something? – aschepler Jul 30 '17 at 0:45 • No parentheses needed in postfix while, join'',<>; also works without them. – choroba Oct 21 '17 at 23:36 # Use non-word characters as variable names Using $% instead of $a can allow you to place the variable name right next to an if, for or while construct as in: @r=(1,2,3,4,5);$%=4; print$_*$%for@r

Many can be used, but check the docs and @BreadBox's answer for which ones have magic effects!

# Use map when you can't use statement modifiers

If you can't use statement modfiers as per @JB's answer, map might save a byte:

for(@c){} vs. map{}@c;

and is useful if you want to do nested iterations as you can put postfix for loops inside the map.

# Use all the magic Regular Expression variables

Perl has magic variables for 'text before match' and 'text after match' so it is possible to split to groups of two with potentially fewer characters:

($x,$y)=split/,/,$_; ($x,$y)=/(.+),(.+)/; /,/; #$x=$,$y=$' # Note: you will need to save the variables if you'll be using more regex matches!  This might also work well as a replacement for substr: $s=substr$_,1; /./;#$s=$'$s=substr$_,4; /.{4}/;#$s=$'  If you need the contents of the match, $& can be used, eg:

# assume input like '10 PRINT "Hello, World!"'
($n,$c,$x)=split/ /,$_;
/ .+ /; # $n=$, $c=$&, $x=$'


# Replace subs with long names with a shorter name

If you call say print four or more times in your code (this obviously varies with the length of the routine you're calling), replace it with a shorter sub name:

sub p{print@_}p;p;p;p


vs.

print;print;print;print


# Replace conditional incrementors/decrementors

If you have code like:

$i--if$i>0


you can use:

$i-=$i>0


# Convert to integer

If you aren't assigning to a variable and so can't use breadbox's tip, you can use the expression 0|:

rand 25 # random float eg. 19.3560355885212

int rand 25 # random int

0|rand 25 # random int

rand 25|0 # random int

~~rand 25 # random int


It's worth noting however than you don't need to use an integer to access an array index:

@letters = A..Z;
$letters[rand 26]; # random letter  redo adds loop behavior to a block without for or while. {redo} is an infinite loop. Don't parenthesize function calls. Perl lets you call a known (core or predeclared) function using the NAME LIST syntax. This lets you drop the & sigil (if you were still using it) as well as the parentheses. For example: $v=join'',<>

Full documentation

Try to use the value of an assignment expression, like so:

# 14 characters
$n=pop;$o=$n&1 # 13 characters, saves 1$o=($n=pop)&1  This works because $n is 2 characters in Perl. You may change $n to () at no cost, and save 1 semicolon by moving the assignment into the parentheses. You can run multiple different statements within nested ternary logic. Suppose you have a big if-elsif statement. This could be any logic and any number of statements. if($_ < 1 ) {
$a=1;$b=2;
$c=3; say$a.$b.$c;
} elsif($_ < 2 ) {$a=3;
$b=2;$c=1;
say $a.$b.$c; } elsif($_ < 3) {
$a=2;$b=2;
$c=2; say$a.$b.$c;
}


You can use (cmd1, cmd2, cmd3) inside the ternary operator to run all of the commands.

$_ < 1 ? ($a=1,$b=2,$c=3,say$a.$b.$c):$_ < 2 ?
($a=3,$b=2,$c=1,say$a.$b.$c):
$_ < 3 ? ($a=2,$b=2,$c=2,say$a.$b.$c): 0; #put the else here if we have one  Here's a dummy example: perl -nE'$_<1?($a=1,$b=2,$c=3,say$a.$b.$c):$_<2?($a=3,$b=2,$c=1,say$a.$b.$c):$_<3?($a=2,$b=2,$c=2,say$a.$b.$c):0;' <(echo 2)


# Use select(undef,undef,undef,$timeout) instead of Time::HiRes (Taken from https://stackoverflow.com/a/896928/4739548) Many challenges require you to sleep with greater precision than integers. select()'s timeout argument can do just that. select($u,$u,$u,0.1)


is much more efficient than:

import Time::HiRes qw(sleep);sleep(0.1)


The former is only 20 bytes, whereas the latter takes up 39. However, the former requires you aren't using $u and have never defined it. If you're going to use it a lot importing Time::HiRes pays off, but if you only need it once, using select($u,$u,$u,0.1) saves 19 bytes, which is definitely an improvement in most cases.

Unless the challenge specifies otherwise, you don't need trailing newlines.
Our 'challenge' says 'output a random number from 0 to 9 to STDOUT'. We can take this code (28 bytes):

$s=int(rand(10));print"$s\n"


And shorten it to this (25 bytes):

$s=int(rand(10));print$s


by simply printing the variable. This last one only applies to this challenge specifically (19 bytes):

print int(rand(10))


but that only works when you don't have to do anything to the variable in between assignment and printing.

• The last one's already listed here. – Sp3000 May 2 '15 at 18:58
• @Sp3000 Thank you, I've updated my answer. – ASCIIThenANSI May 2 '15 at 20:22

# Use globs like string literals

Occasionally (often when dealing with or challenges) you benefit greatly from the ability to nest string literals. Normally, you'd do this with q(…). However, depending on what characters you need inside the string, you may be able to save a byte and use <…>, the glob operator. (Note that what's inside the angle brackets can't look like a filehandle, and can't look like it's meant to be expanded into a list of filenames, which means that quite a lot of characters won't work correctly.)

# If you need an alternating boolean

Instead of doing something like $-++%2, use $|--.

Try it online!

# If you need a variable that is falsy on first call and truthy thereafter

Instead of something like !!$x++, use $|++.

Try it online!

# Useful escape sequences

See perlrebackslash, but you can potentially save a lot of bytes using these effectively.

In matches:

[A-Za-z]        \pl
[^A-Za-z]       \Pl
(?<=\w)         \B


In replacements (or double quoted strings):

\utext          Text            (e.g s/ (.)/uc$1/e vs. s/ (.)/\u$1/)
\Utext          TEXT
\lTEXT          tEXT
\LTEXT          text


# Don't reassign $_ in nested for/maps If you aren't using m// or s/// in the body of the loop, you can make the most of $' in a nested loop by performing an empty match (//) and using $' instead of assigning $_ to another variable:

$i=$_,say map{qw(0 * +)[max(abs,abs$i)%3]}@;for @;=- --$_..$_  vs. //,say map{qw(0 * +)[max(abs,abs$')%3]}@;for@;=- --$_..$_


# Use hash keys for random ordering.

If a set of items needs to be in a random order, or you need a (pseudo)random subset of times, storing them as hash keys will allow them to be ordered randomly without a call to rand. The order will remain consistent within a program run as long as no insertions or deletions are made to the hash.

# Hash keys can be defined quickly

If the information to be stored/randomized is already in an array, the hash can quickly be created with:

 @hash{@array}++;


The values of the hash are not particularly useful here, but then the array elements can be retrieved in a random order with:

@random_ordered_array = keys %hash;


## Use expression regexe.

Decent illustration of this is following code shaping the input into sine wave:

s/./print" "x(sin($i+=.5)*5+5).$&/eg;


As you can see, it's pretty compact way of iterating over characters in standard input. You could use other regex to change the way how things are matched.

# Use sub signatures

If you're submitting a function, using -Mfeature+signatures might save you 4 bytes vs. using @_:

sub{($a,$b)=@_;...}

sub($a,$b){...}