Hauke Lubenow > Acme-Pythonic-Functions > Acme::Pythonic::Functions

Download:
Acme-Pythonic-Functions-0.38.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.38   Source  

NAME ^

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

VERSION ^

Version 0.38

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

    pront oslistdir(".");
    pront;
    pront "System-related:";
    pront 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.

print-Replacement-Function

pront

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, because i want one element per line then. You can have that with something like 'say for @a;', but i wrote my own little print-replacement instead. It is called pront(). I think that's a funny name, a bit like chomp(), being derived from chop(). In Python, there isn't a function called pront() though.

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.

File-related-Functions

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.

System-related-Functions

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-2012, 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

syntax highlighting: