Loading...
Preparing your coding drills
Comprehensive reference for 31 Perl methods across 6 categories.
push
Appends one or more elements to the end of an array. Returns the new number of elements in the array.
pop
Removes and returns the last element of an array. If the array is empty, returns undef.
shift
Removes and returns the first element of an array, shortening the array by one and shifting all indices down by one.
unshift
Prepends one or more elements to the beginning of an array. Returns the new number of elements in the array.
splice
Removes elements from an array and optionally replaces them with new elements. Returns the removed elements.
sort
Sorts a list and returns the sorted list. By default sorts lexicographically. A custom comparison block using $a and $b can be provided.
grep
Filters a list by evaluating a block or expression for each element. Returns the list of elements for which the expression is true.
map
Applies a block or expression to each element of a list and returns the list of results. Each element is aliased as $_.
chomp
Removes the trailing newline (or the value of $/) from a string. Modifies the variable in place and returns the number of characters removed.
chop
Removes and returns the last character of a string regardless of what it is. Modifies the variable in place.
split
Splits a string into a list of substrings based on a delimiter pattern. Returns the list of substrings.
join
Joins the elements of a list into a single string, separated by the value of EXPR.
substr
Extracts a substring from a string. Can also replace part of the string when a REPLACEMENT is given.
index
Returns the position of the first occurrence of SUBSTR in STR at or after POSITION. Returns -1 if not found.
uc
Returns an uppercased version of EXPR. If EXPR is omitted, uses $_.
lc
Returns a lowercased version of EXPR. If EXPR is omitted, uses $_.
keys
Returns a list of all the keys in a hash. The order is unpredictable but consistent with values() for the same unmodified hash.
values
Returns a list of all the values in a hash. The order corresponds to the order returned by keys() for the same unmodified hash.
exists
Checks whether a key exists in a hash. Returns true if the key is present, even if its value is undef.
delete
Removes a key-value pair from a hash and returns the deleted value.
print
Prints a string or a list of strings to a filehandle (STDOUT by default). Returns true on success, false on failure.
say
Like print, but automatically appends a newline at the end. Requires "use feature 'say'" or "use v5.10".
open
Opens a file and associates it with a filehandle. Supports reading, writing, appending, and piping. Returns true on success.
m//
Matches a string against a regular expression pattern. Returns true if the pattern matches. The m can be omitted when using // delimiters.
s///
Searches for a pattern and replaces it with the replacement string. Modifies the string in place and returns the number of substitutions made.
tr///
Transliterates characters in a string. Each character in SEARCHLIST is replaced with the corresponding character in REPLACEMENTLIST. Returns the number of characters replaced or deleted.
defined
Returns true if the expression has a value other than undef. Used to check whether a variable has been assigned a value.
ref
Returns a string indicating the type of reference, or an empty string if the argument is not a reference. Used for runtime type checking.
die
Throws an exception. If not caught by eval, prints the message to STDERR and exits the program with a non-zero status. If LIST is a reference, it can be caught and inspected as a structured exception.
scalar
Forces EXPR to be evaluated in scalar context. Most commonly used to get the length of an array.
warn
Prints a warning message to STDERR. Unlike die, it does not throw an exception or exit the program.