ruby.onl / culture

The Perl Refugee's Phrasebook

2026-02-23

You already know Perl. You already think in $_ and =~ and while (<>). Good news: Ruby speaks about 80% of the same language. This is your translation guide for the parts that differ, so you can stop Googling and start shipping.

Part 1: Variable Translation Table

Most of Perl's special variables survived the jump to Ruby with their names intact. A few got minor cosmetic surgery.
Perl Ruby Notes
$_ $_ Same! Works identically with -n/-p
@F $F Array of split fields with -a
$. $. Current line number
$/ $/ Input record separator
$\ $\ Output record separator
$& $& Last regex match
$1-$9 $1-$9 Capture groups, identical behavior
<> ARGF.each or -n ARGF is more powerful
say puts Adds newline automatically
print print Same behavior
chomp chomp or chomp! Bang version modifies in place
chop chop or chop! Same as Perl
die abort or raise abort prints to STDERR and exits
warn warn or STDERR.puts Same behavior
exit exit Same behavior

Part 2: Operator Translation

Perl Ruby Notes
s~~~ gsub or sub Methods, not operators
m~~ =~ with %r~~ Similar syntax
//= ||= Conditional assign if nil/false
.. (flip-flop) .. (flip-flop) Identical behavior
. (concat) + or << << mutates in place
x (repeat) * "ab" * 3 gives "ababab"
eq, ne, lt, gt ==, !=, <, > Ruby uses same ops for strings and numbers
=~ =~ Same!
!~ !~ Same!
qw() %w~~ Word arrays
q() %q~~ Single-quoted string
qq() %Q~~ Double-quoted string
qx() backticks or %x~~ Command execution
qr() %r~~ Regex literal

Part 3: Quoting Translation

Perl Ruby Interpolation?
q~text~ %q~text~ No
qq~text~ %Q~text~ Yes
qw(a b c) %w~a b c~ No
qx(cmd) %x~cmd~ Yes
m~pattern~ %r~pattern~ Yes
<<END <<~END Yes (squiggly strips indentation)

Part 4: The Behavioral Gotchas

These will bite you if you're not ready.

Strings are immutable by default. Perl's $str =~ s/old/new/g; modifies $str in place. Ruby's str.gsub(%r~old~, "new") returns a NEW string. Use str.gsub!(%r~old~, "new") to modify in place. Forget the bang and nothing happens. You will do this at least five times before it sticks.

No sigils for variable types. Perl uses $scalar, @array, %hash. Ruby uses plain variable names for everything. Context determines behavior.

Method calls use dots. Perl: length($str) or $obj->method(). Ruby: str.length or obj.method. Dots everywhere.

Truthiness is different. Perl: 0, "", undef, and "0" are false. Ruby: Only nil and false are false. 0, "", and "0" are ALL TRUE. This will absolutely wreck your conditional logic if you're not paying attention.

Arrays and hashes are objects. Perl: push @arr, $val and $hash{$key}. Ruby: arr.push(val) or arr << val and hash[:key] or hash["key"].

Part 5: Common One-Liner Translations

# Print lines matching pattern # Perl: perl -ne 'print if /ERROR/i' # Ruby: ruby -ne 'puts $_ if /ERROR/i' # Print second CSV field # Perl: perl -F, -ane 'print $F[1], "\n"' # Ruby: ruby -F, -ane 'puts $F[1]' # In-place substitution with backup # Perl: perl -i.bak -pe 's/old/new/g' # Ruby: ruby -i.bak -pe '$_.gsub!(/old/, "new")' # Count lines matching pattern # Perl: perl -ne '$n++ if /error/; END { print "$n\n" }' # Ruby: ruby -ne 'BEGIN{$n=0}; $n+=1 if /error/; END{puts $n}' # Print line numbers # Perl: perl -ne 'print "$.: $_"' # Ruby: ruby -ne 'print($., ": ", $_)'
The patterns are so close you could accidentally write one in the other's syntax and it might still work.

Created By: Wildcard Wizard. Copyright 2026