The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

read_dir($path, $pattern)

Returns a list of all files in a director matching $pattern

Parameters

  • $path Path to directory

  • $pattern Regex pattern (and make sure to escape with `qr` -> e.g qr/.*\.conf$/)

Returns

A list of matching files, possibly empty

read_file($path [, $path_is_fh = undef])

Reads a file given by a $path into a string. Applies a shared lock on the file while reading. $path can also reference an open filehandle for external control over locks and cursor. If this is the case, set $path_is_fh to True.

Parameters

  • $path Path to file

  • C[$path_is_fh = undef]> Set to True if $path is an open filehandle (at least for reading).

Raises

Exception if the file is somehow inaccessible or it was unable to acquire the lock

Returns

File contents as string

write_file($path, $content [, $path_is_fh = undef])

Writes $content to $file while having an exclusive lock. $path can also reference an open filehandle for external control over locks and cursor. If this is the case, set $path_is_fh to True.

Parameters

  • $path Path to file

  • $content File content

  • [$path_is_fh = undef] Set to True if $path is an open filehandle (write!)

Raises

Exception if the file is somehow inaccessible or it was unable to acquire the lock

Returns

None

get_mtime($path)

Tries to extract mtime from a file. If supported by the system in milliseconds resolution.

Parameters

  • $path Path to file

Returns

mtime of the file. If something went wrong, "0";

split_and_trim($line, $separator)

Utility method to split and trim a string separated by $separator.

Parameters

  • $line Input string (e.g 'This = That ')

  • $separator String separator (e.v '=')

Returns

Two strings. With example values given in the parameters this would be 'This' and 'That'.