-
Notifications
You must be signed in to change notification settings - Fork 7
Perl for people who don't know it
We don't write much Perl at RadiaSoft but it still does crop up once in a while. Usually in contexts for manipulating text (ex fixing up CMakeLists.txt). This document is to help folks who don't know Perl learn enough of it to read our code that uses Perl and hopefully write some of their own.
Documentation
$_
is the default variable. There are commands that use the default variable when no args are supplied (ex print)
-
-e
execute the following command as the program -
-p
run the program over every line of the file stored in$_
and print the$_
-
-i
"inplace" means write the output of the command to the input file
# prints the line number and line for each line in file.txt
perl -p -e '$_ = "$.: $_"' file.txt
# Replace foo with bar in file.txt
perl -pi -e 's/foo/bar/' file.txt
Documentation
.
is string concatentation
# concatenate foo and bar
perl -e 'print("foo" . "bar")'
perl -e '$_ = "foo"; $_ .= "bar"; print($_)'
Documentation
q
is the single quote operator, that is, q{hello}
and 'hello'
are semantically equivalent. In embedding Perl inside Bash, you often run into quoting hell so the single quote operator works nicely for this when there are embedded quotes inside data values inside the Perl program that is embedded in Bash. Single quotes are semantically different than double quotes in Perl. Single quoted strings are not interpolated. Double quoted strings are.
# Preserves the value without worrying about embedded quotes in the value
perl -e 'print q{"hello world"}'
# Quotes are lost
perl -e 'print "hello world"'
codes_cmake_fix_lib_dir() {
# otherwise uses ~/.local/lib64
perl -pi -e '/include\(GNUInstallDirs/ && ($_ .= q{
set(CMAKE_INSTALL_LIBDIR "lib" CACHE PATH "Library installation directory." FORCE)
GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_LIBDIR CMAKE_INSTALL_LIBDIR)
})' CMakeLists.txt
}
import sys
for p in sys.argv[1:]:
d = ""
with open(p) as f:
for l in p.readlines():
if "include(GNUInstallDirs" in l:
l += """
set(CMAKE_INSTALL_LIBDIR "lib" CACHE PATH "Library installation directory." FORCE)
GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_LIBDIR CMAKE_INSTALL_LIBDIR)
"""
d += l
with open(p, "w") as f:
f.write(d)