Re: Python vs Perl (an example)

Discussion in 'Python' started by Nick Monyatovsky, Apr 1, 2004.


  1. >Hello,
    >
    >I recently wrote a Perl version of pyAlbum.py [1] -- a
    >Python script to create an image album from a given
    >directory -- plAlbum.pl [2].
    >
    >It made me realize how easy-to-use Python is.
    >
    >[1]
    >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/271246
    >[2]
    >http://www.premshree.resource-locator.com/cgi-bin/source.pl?file=../perl/plAlbum.pl
    >
    >-Premshree Pillai
    >
    >=====
    >-Premshree
    >[http://www.qiksearch.com/]
    >



    I cannot help, but make a couple of observations:

    1. Although you point about Python being easy has some merit,
    I would not bring up an awkwadly written Perl script to back up
    that argument.

    2. There are many things which are very easy to do in Perl, and
    are very cumbersome in Python. Regular expressions is a
    vivid example. Saying very generically that Python is easier
    than Perl is just an invitation for flame wars.

    3. Lastly, Perl is not aspiring to or has ever claimed to
    be easy. That is not its strength and attraction.
    (Although, on a simple example like this, a more conventional
    Perl script (see below) is pretty comparable).

    If you'd like to demonstrate that Python is easier than Perl, you'll
    need to find a better case.

    ----------------- pyAlbum.pl -------------------------

    =head

    pyAlbum.pl

    Perl version of the pyAlbum.py script
    that creates an album
    of images from a given directory

    (c) 2004 Premshree Pillai (27/02/04)
    http://www.qiksearch.com/

    =cut

    use Tk::Image;

    my @files;
    my $count = 0;
    my $total = 0;


    sub getDir
    {
    print "Enter the directory to read images from (rel/abs path): ";
    $dirName = <STDIN>; chomp $dirName;
    -d $dirName or warn "Directory does not exist!" and getDir();
    }


    sub retPrevFile
    {
    my $index = shift;
    return "" unless $index;
    return sprintf '&laquo; <a href="%s.htm">Previous</a>',
    $files[$index - 1];
    }


    sub retNextFile
    {
    my $index = shift;
    return "" if $index == $#files;
    return sprintf '<a href="%s.htm">Next</a> &raquo;', $files[$index
    + 1];
    }

    sub retPipe
    {
    my $index = shift;
    return " | " if $index and $index < $#files;
    return "";
    }

    sub getSlideName
    {
    print "Enter base name for album: ";
    $slideName = <STDIN>; chomp $slideName;
    -d "$dirName/$slideName" and warn "Directory $slideName exists!"
    and getSlidename();
    mkdir "$dirName/$slideName" or die;
    }

    my $format = q[
    <html>
    <head>
    <title>%s</title>
    <style type="text/css">
    body {font-family:Trebuchet MS, Arial, Verdana;
    font-size:10pt; font-weight:bold}
    h4 {color:#CCCCCC}
    a {font-family:Trebuchet MS, Arial, Verdana;
    font-size:10pt; font-weight:bold;
    text-decoration:none}
    a:hover {font-family:Trebuchet MS, Arial, Verdana;
    font-size:10pt; font-weight:bold;
    text-decoration:underline}
    img {border:#000000 solid 1px}
    </style>
    </head>
    <body>
    <center><h2>%s</h2></center>
    <center><h4>%s (%s/%s)</h4></center>
    <center><a href="../%s"><img src="../%s"></a></center>
    <center>%s%s%s</center>
    </body>
    </html>
    ];


    getDir();
    getSlideName();

    print "Enter a title for the album: ";
    $title = <STDIN>; chomp $title;

    print "Enter image scaling factor (e.g., 0.5, <enter> for default) ";
    $scale = <STDIN> || '1.0'; chomp $scale;

    chdir $dirName;
    @files = <*.*>;
    $total = scalar(@files);
    chdir $slideName;

    foreach $file (@files)
    {
    open FILE , ">$file.htm";
    printf FILE $format, $title, $title, $file, $count + 1, $total,
    $file, $file,
    retPrevFile($count), retPipe($count), retNextFile($count);
    close FILE;
    $count++;

    warn "File $file.htm created.\n"
    }

    print "\n", "Album created!\n";
     
    Nick Monyatovsky, Apr 1, 2004
    #1
    1. Advertising

  2. Nick Monyatovsky

    Ville Vainio Guest

    >>>>> "Nick" == Nick Monyatovsky <mon-at-cox-net> writes:

    Nick> 2. There are many things which are very easy to do in Perl, and
    Nick> are very cumbersome in Python. Regular expressions is a
    Nick> vivid example. Saying very generically that Python is easier
    Nick> than Perl is just an invitation for flame wars.

    Saying very generically that Python regexps are more cumbersome than
    Perl one is just an invitation for flame wars. I have heard that
    statement quite often, yet nobody ever bothers to substantiate it.

    Hint - you can do

    s = re.sub
    m = re.match

    if the function names are too long for you. And you don't need to
    re.compile the regexps beforehand, just pass them as strings to the
    functions. And backslash escaping behaviour can be averted by r"raw
    strings".

    The actual regexp syntax is the same as w/ Perl. I guess that's why
    they are often called 'perl-style' regexps.

    Nick> If you'd like to demonstrate that Python is easier than
    Nick> Perl, you'll need to find a better case.

    This is c.l.py - I don't think there are many who would claim the
    opposite. Actually, I think Python and Perl are so far apart in the
    ease of use/complexity/elegance, it's not even funny.

    I'll give some comments on the script anyway.

    Nick> my @files;
    Nick> my $count = 0;
    Nick> my $total = 0;

    In Python there's no need for 'my' - every assignment makes the
    variable local by default. This is a Good Thing, and I doubt you would
    contest that.

    Nick> sub retPrevFile
    Nick> {
    Nick> my $index = shift;

    In Py, you just list the arguments in signature.

    Nick> mkdir "$dirName/$slideName" or die;

    This idiom is not necessary in Py. You just don't handle errors, the
    program terminates w/ traceback automatically.

    Nick> open FILE , ">$file.htm";
    Nick> printf FILE $format, $title, $title, $file, $count + 1, $total,

    Here's a clear inconsistency (FILE doesn't have $, others do). Things
    like this make a language hard to understand. In Py you just have
    objects.

    --
    Ville Vainio http://tinyurl.com/2prnb
     
    Ville Vainio, Apr 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,830
  2. Shawn Milo
    Replies:
    20
    Views:
    608
    Bruno Desthuilliers
    Mar 4, 2007
  3. kj
    Replies:
    4
    Views:
    302
    Tim Roberts
    Jan 12, 2008
  4. Sam Roberts
    Replies:
    15
    Views:
    308
    Sam Roberts
    Feb 7, 2005
  5. Laura Raffa
    Replies:
    11
    Views:
    219
    BearItAll
    Mar 3, 2005
Loading...

Share This Page