How to convert uppercase to lowercase with perl -pe?
Question
How to convert uppercase to lowercase with perl -pe?
When running Perl one-liners in a sandbox environment using perl - (which reads code from STDIN), you cannot use command-line flags like -pe or -e directly. The error happens because perl - expects Perl code on standard input, not command-line switches or scripts.
To convert uppercase letters to lowercase with transliteration (tr/A-Z/a-z/) inside a sandbox where you can’t pass command-line options, you need to write a small Perl script that mimics the behavior of perl -pe. The -p flag means “read each line from input, apply code to $_, then print $_ automatically.” You can duplicate this with a while(<>) loop and calling print explicitly.
Working Perl Example for Sandbox Execution
use strict;
use warnings;
# Simulate 'perl -pe "tr/A-Z/a-z/"' behavior
while (<>) {
tr/A-Z/a-z/; # transliterate uppercase to lowercase in $_
print; # print the modified line
}
__DATA__
Hello WORLD!
Perl IS Fun.
123 ABC xyz
This script reads lines from the DATA filehandle (literally the lines after __DATA__), converts uppercase characters to lowercase in place using tr/A-Z/a-z/, then prints each transformed line.
Key Perl Concepts
$_is the default variable for input line and string manipulation.while (<>)reads from@ARGVfiles orSTDINif none given, assigning each line to$_.tr/A-Z/a-z/transliterates characters in place: here converting uppercase ASCII letters to lowercase.printoutputs the modified line since there is no-pimplicit printing.
Common Pitfalls
- Using
perl -pesyntax withperl -(code on STDIN) causes syntax errors. tr/A-Z/a-z/only handles ASCII letters; uselcfor full Unicode lowercase.- Carefully quote command-line one-liners to avoid shell or Perl parsing errors (not applicable here since code is inline).
Summary
If you want to run transliteration inside a sandbox without command-line flags, write a short Perl script with the common while (<>) loop, apply tr/A-Z/a-z/ to $_, and print. This approach works cleanly under perl - or embedded environments.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 8ms
hello world!
perl is fun.
123 abc xyz
(empty)