The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Acme::Pythonic::Functions - Python-like functions for Perl

VERSION

Version 0.39

SYNOPSIS

The following script "example.pl" shows the usage of the functions. A ready-to-run version of it can be found in the "examples"-directory in the module's tar-ball:

    use Acme::Pythonic::Functions;
    
    pyprint "Strings:";
    
    $a = "Hello";
    
    if (endswith($a, "ello")) {
        pyprint '$a ends with "ello".';
    }
    
    if (isin($a, "ll", "s")) {
        pyprint '"ll" is in $a.';
    }
    
    $a = "2345";
    
    if (isdigit($a)) {
        pyprint '$a is a digit.';
    }
    
    $a = "    Line    ";
    
    pyprint lstrip($a);
    $a = replace($a, "Line", "Another line");
    pyprint $a;
    pyprint rstrip($a);
    
    $a = "Hello";
    
    if (startswith($a, "He")) {
        pyprint '$a starts with "He".';
    }
    
    pyprint len($a, "s");
    
    pyprint;
    pyprint "Lists:";
    
    @a = ("a", "b", "c");
    $b = "d";
    
    @a = append(@a, $b);
    
    pyprint @a;
    
    @a = ("a", "b", "c");
    @b = (1, 2, 3);
    
    @a = extend(@a, @b);
    
    pyprint @a;
    
    if (isin(@a, "c", "l")) {
        pyprint '"c" is in @a.';
    }
    
    @a = insert(@a, 1, "a2");
    
    pyprint @a;
    
    pyprint len(@a, "l");
    
    @a = remove(@a, "a2");
    
    pyprint @a;
    
    pyprint;
    pyprint "Hashes:";
    
    %a = ("a" => 1, "b" => 2, "c" => 3);
    
    if (has_key(%a, "c")) {
        pyprint '%a has a key "c".';
    }
    
    if (isin(%a, "c", "h")) {
        pyprint '%a has a key "c".';
    }
    
    pyprint;
    pyprint "File-related:";
    
    if (isdir("/home/user")) {
        pyprint "Is directory.";
    }
    
    if (isfile("/home/user/myfile")) {
        pyprint "Is file.";
    }
    
    @a = ("a\n", "b\n", "c\n");
    
    if (isfile("test12345.txt")) {
    
        pyprint 'File "test12345.txt" already exists. Nothing done.';
    } else {
    
        writefile("test12345.txt", @a);
        @c = readfile("test12345.txt");
    
        for $i (@c) {
            $i = rstrip($i);
            print $i . " " ;
        }
        pyprint;
    }

    pyprint oslistdir(".");
    pyprint;
    pyprint "System-related:";
    pyprint osname();
    

In the "examples"-directory mentioned above, there's also a a Pythonic-Perl-version of this script called "perlpyex.pl" and a corresponding Python-script called "pyex.py" for comparison.

DESCRIPTION

The programming-language "Python" offers some basic string-, list- and other functions, that can be used quite intuatively. Perl often uses regular-expressions or special variables for these tasks. Although Perl's functions are in general more flexible and powerful, they are slightly more difficult to use and a bit harder to read for human beings. This module tries to mimic some of Python's functions in Perl. So maybe Python-programmers switching to Perl or programming-beginners could feel a bit more comfortable with them.

pyprint

Python adds a (system-dependent) newline-character by default to strings to be printed. This is rather convenient and can be found in the say()-function of Perl 5.10 and above too. I wasn't happy with the way, say() prints lists though. You can have that with something like 'say for @a;', but I like the way, Python prints lists better. So I wrote my own little print-replacement called pyprint(). In Python, there isn't a function called pyprint(). If you have to print more complex data-structures, use Data::Dumper.

String-Functions

endswith($foo, $bar)

Tests whether $foo ends with $bar (return-value: 1 or 0).

isdigit($foo)

Tests whether $foo contains just digits (return-value: 1 or 0).

isin($foo, $bar, "s")

See below.

lstrip($foo)

Returns $foo stripped from whitespace characters on the leftern side.

lstrip2($foo, $bar)

Returns $foo stripped from $bar on the leftern side. Just returns $foo, if $foo doesn't start with $bar. Not part of Python, but quite useful.

replace($foo, $old, $new [, $count])

Returns a copy of $foo with all occurrences of substring $old replaced by $new. If the optional argument $count is given, only the first $count occurrences are replaced.

rstrip($foo)

Returns $foo stripped from whitespace characters on the right side.

rstrip2($foo, $bar)

Returns $foo stripped from $bar on the right side. Just returns $foo, if $foo doesn't end with $bar. rstrip2() is not a Python-builtin, although it is quite useful. The special case

$foo = rstrip2($foo, "\n");

is similar to

chomp($foo);

(although it makes me feel good, every time I chomp() something).

startswith($foo, $bar)

Tests whether $foo starts with $bar (return-value: 1 or 0).

strip($foo)

Returns $foo stripped from whitespace characters on both sides.

List-Functions

append(@foo, $bar)

Returns a copy of list @foo with string $bar appended. (Perl: push()).

extend(@foo, @bar)

Returns a copy of list @foo extended by list @bar. That would be just (@foo, @bar) in Perl.

insert(@foo, $nr, $bar)

Returns a copy of list @foo, with $bar inserted at position $nr.

isin(@foo, $bar, "l")

See below.

remove(@foo, $bar)

Returns a copy of list @foo with the first occurrence of element $bar removed. If $bar is not an element of @foo, just returns @foo.

Hash-Functions

has_key(%foo, $bar)

Tests whether hash %foo has a key $bar (return-value: 1 or 0). isin() can be used alternatively.

isin(%foo, $bar, "h")

See below.

Functions for several datatypes

isin([$foo, @foo, %foo], $bar, ["s", "l", "h"])

Tests whether $bar is a "member" of foo. Depending on the last argument given ("s" for string, "l" for list, "h" for hash"), foo can either be a string, a list or a hash.

In mode "s", it is tested, whether $bar is a substring of string $foo. In mode "l", it is tested, whether $bar is an element of list @foo. In mode "h", it is tested, whether $bar is a key of hash %foo.

The return-value is 1 or 0. This mimics a special syntax of Python:

if "ell" in "Hello": print "Is in."

if "b" in ["a", "b", "c"]: print "Is in."

if "c" in {"a" : 1, "b" : 2, "c" : 3}: print "Is in."

len([$foo, @foo, %foo], ["s", "l", "h"])

Returns the number of characters or elements of foo. Depending on the last argument given ("s" for string, "l" for list, "h" for hash"), foo can either be a string, a list or a hash.

In mode "s", the number of characters of string $foo is returned. In mode "l", the number of elements of list @foo is returned. In mode "h", the number of keys of hash %foo is returned.

isdir($foo)

Tests whether $foo is a directory. Python: os.path.isdir(), Perl: -d.

isfile($foo)

Tests whether $foo is a plain file. Python: os.path.isfile(), Perl: -f. For more detailed file-testing use the other file-testing-operators described in "perldoc perlfunc", the stat()-function or the "File::stat"-module.

listdir($foo), oslistdir($foo)

Returns a list of the filenames in the directory $foo. Directory-entries "." and ".." are left out. Python: os.listdir().

readfile($foo)

Returns the contents of the text-file named $foo as a list. Only use on smaller files, as this can take a lot of memory. For processing larger files, use the "Tie::File"-module instead. readfile() is not a Python-builtin, although Python has a function readlines() to read multiple lines of text from a file-handle into a list at once.

writefile($foo, @bar)

Writes @bar to a file named $foo. Not a Python-builtin.

osname()

Tells the name of the operating-system, similar to "os.name" in Python.

AUTHOR

Hauke Lubenow, <hlubenow2@gmx.net>

COPYRIGHT AND LICENSE

Acme::Pythonic::Functions is Copyright (C) 2009-2017, Hauke Lubenow.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.14.2. For more details, see the full text of the licenses in the directory LICENSES. The full text of the licenses can also be found in the documents "perldoc perlgpl" and "perldoc perlartistic" of the official Perl 5.14.2-distribution. In case of any contradictions, these 'perldoc'-texts are decisive.

THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. FOR MORE DETAILS, SEE THE FULL TEXTS OF THE LICENSES IN THE DIRECTORY LICENSES AND IN THE 'PERLDOC'-TEXTS MENTIONED ABOVE.

SEE ALSO

Acme::Pythonic