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.
Project site: http://radiac.net/projects/python-perl/
Source code: https://github.com/radiac/python-perl
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:
Pass it to Python on the command line:
python3.7 -m perl myscript.py
Set it on your script’s shebang:
#!/usr/bin/python3.7 -mperl
Import it before importing any of your code which uses its syntax - usually in your __init__.py:
import perl
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
Metadata
Release files for perl 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| perl-1.0.0.tar.gz | 11.2 kB | Details |
Release files / perl-1.0.0.tar.gz
| Download URL | perl-1.0.0.tar.gz |
|---|---|
| Size | 11.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d17aa3de50e535dd95842246f4b0d0d7713d787171c8464aa63b5c82df3a601e
|
|
BLAKE2b-256 checksum How to use checksums |
cb2fd2724ee752dc50c6eb4273ab5c486ce43737866072f15bd089e449003fd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1
|