Skip to main content

Perl as a Python package

Project description

https://travis-ci.org/radiac/python-perl.svg?branch=master https://coveralls.io/repos/radiac/python-perl/badge.svg?branch=master&service=github

Haven’t you always dreamed of having the power of Perl at your fingertips when writing Python?

Well, this package is proof that dreams can come true:

>>> import perl
>>> value = "Hello there"
>>> if value =~ /^hello (.+?)$/i:
...     print("Found greeting:", $1)
...
Found greeting: there
>>> value =~ s/there/world/
>>> print(value)
Hello world

Note: This is very silly and probably shouldn’t go anywhere near production code.

Installation

This requires Python 3.7 or later.

Usage

The module needs to be loaded before Python tries to read code which uses these enhancements. There are therefore four different ways to use this module:

  1. Pass it to Python on the command line:

    python3.7 -m perl myscript.py
    
  2. Set it on your script’s shebang:

    #!/usr/bin/python3.7 -mperl
    
  3. Import it before importing any of your code which uses its syntax - usually in your __init__.py:

    import perl
    

    Note

    You only need to import it once in your project.

    However, because Python needs to read the whole file before it can run the import, you cannot use perl’s functionality in the same file where you import perl.

  4. Use it on the Python interactive shell (REPL):

    $ python3.7
    >>> import perl
    

    or:

    $ python3.7 -m perl
    

Features

Regular expression matching

Syntax:

val =~ /pattern/flags
# or
val =~ m/pattern/flags

where pattern uses Python’s regex syntax, and flags is a subset of the characters AILMSXG, which map Python’s single character flags, plus g which mimics the global flag from Perl.

When run without the global flag, the re.Match object is returned; any matched groups will be available as numbered dollar variables, eg $1, and named groups will be available on $name.

When run with the global flag, the list of re.Match objects will be returned. No dollar variables will be set.

Examples:

# Case insensitive match
value =~ /^foo (.+?) bar$/i
print(f"Matched {$1}")

# Use in a condition
if value =~ /^foo (.+?) bar$/i:
    return $1

# Use as a global
matches = value =~ /foo (.+?) bar/gi;

Regular expression replacement

Syntax:

val =~ s/pattern/replacement/flags

where pattern uses Python’s regex syntax, and flags is a subset of the characters AILMSXG, which map Python’s single character flags, plus g which mimics the global flag from Perl to replace all occurrences of the match.

Examples:

# Case insensitive global replacement
value =~ s/foo/bar/gi

Dollar variables

Syntax:

$name
$number

Dollar variables act like regular variables - they can be set and used as normal. They are primarily intended for use with regular expressions - each regex will remove all previous dollar variables, to avoid confusion as to whether they matched or not.

Contributing

During development, install in a virtual environment:

mkdir python-perl
cd python-perl
git clone <path-to-repo> repo
virtualenv --python=python3.7 venv
. venv/bin/activate
cd repo
pip install -r requirements.txt

To run tests:

cd path/to/repo
. ../venv/bin/activate
pytest

To run the example, use one of the following:

$ ./example.py
$ python3.7 -m perl example.py
$ python3.7 example_importer.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for perl, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size perl-1.0.0.tar.gz (11.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page