Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Sunday, 15 June 2014

Strings and numbers: Scala can be easier than Ruby or Python thanks to type inference

When I was working on some python code I realized, gluing different parameters together is actually more complicated in Python (or Ruby) than in Scala or even in Java!

Someone would expect statically typed languages should be "by default" more complicated than flexible, dynamic Python or Ruby. Actually it may not be correct.

Scala has a really nice feature called types inference. This is something that could fill so natural and saves your time so often, you would wonder later why other languages are so resistant to pick it up. Let's see examples.

I would like to make string and int concatenation, like a code-number of a flight or road, e.g. aaa123
"aaa" + 123
foo + bar

Scala (sbt console):

scala> val foo = "aaa"
foo: String = aaa
scala> val bar = 123
bar: Int = 123
scala> foo + bar
res0: String = aaa123
Simple!

Python (ipython):
In [1]: foo = "aaa"
In [2]: bar = 123
In [3]: foo + bar
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in ()
----> 1 foo + bar
TypeError: cannot concatenate 'str' and 'int' objects

In [4]: foo + str(bar)
Out[4]: 'aaa123'
Ruby (irb):


And how about if we want to add another number value:

scala> val baz = 3
baz: Int = 3
scala> foo + bar + baz
res1: String = aaa1233
scala> baz + bar + foo
res2: String = 126aaa
but perhaps I need aaa126?

I can just use parentheses to show the proper execution flow so type inference will not change the context of second operator:

scala> foo + (bar + baz)
res4: String = aaa126
To do the same in Python you would need explicit conversions:
In [8]: str(baz + bar) + foo
Out[8]: '126aaa'
and similar conversion thing in Ruby:
>> (bar + baz).to_s + foo
=> "126aaa"
This could feel a little bit complicated, especially when you see than even in this monstrous, boiler-plate driven Java you can also write just like this:

String foo = "aaa";
int bar = 123;
System.out.println(foo + bar); // aaa123

int baz = 3;

System.out.println(foo + bar + baz); // aaa1233
System.out.println(bar + baz + foo); // 126aaa
System.out.println(foo + (bar + baz)); // aaa126

How about Perl, the old school Swiss Army knife for data processing? Perl (and in similar fashion Visual Basic), gives you a choice - what kind of "addition" you want to do, a math (+) or a string concatenation (. for Perl or & if this is VB):

perl -le ' $foo="aaa"; $bar=123; $baz=3; print $bar + $baz.$foo '
126aaa

perl -le ' $foo="aaa"; $bar=123; $baz=3; print $foo.($bar + $baz) '
aaa126

Just remember Perl is trying conversion really hard - be sure this is what you really want, like here:

perl -le ' $gbp = "100gbp"; $eur = "100eur"; print($gbp + $eur) '
200

Tuesday, 30 October 2012

Want to do maths? You are 120x better off with Scala than Perl

To be honest, I did expect some difference in performance between statically typed Scala (JVM-based language) and dynamic Perl... but maybe not that big. Anyway, check it out.

Here is a very simple way to compute prime numbers:

==== PrimeNumbers.scala
object PrimeNumbers {
   def isPrime(n: Int): Boolean = (2 until n) forall (d => n % d != 0)
 
   def main(args: Array[String]): Unit = {
    (8999999 until 8999999+200).map(x => if (isPrime(x)) print(x+" ") )
   }
}

==== prime_numbers.pl
sub isPrime {
    my $n = shift;
    grep( $n % $_ == 0, (2..($n-1))) ? 0 : 1
}

for(8999999..(8999999+200)) {
    isPrime($_) && print "$_\n"
}


time scala PrimeNumbers
9000011 9000041 9000049 9000059 9000067 9000119 9000127 9000143 9000163 9000193
real    0m3.187s

time perl prime_numbers.pl
real    6m23.221s

Really? This huge JVM-bloated Scala is about 120x faster in such things than Perl... can't belive!

(Perl v5.14.2, Scala 2.9.1)

Tuesday, 27 September 2011

CPAN of the week

Carp::Always - this is what you need when perl -MCarp=verbose doesn't work the way you expect it. Spotted in some post by JRockway.

Mock::Quick - mock whatever you want, but also takeover/partially override existing modules and classes. The beauty of certainty for TDD or just when escaping from too many dependencies.

Friday, 1 February 2008

Exif auto-rotate & scale your photos

Typically after a journey to new place I finish with hundreds of photos from my camera. When I am browsing it on linux (or mac osx) everything is ok - gwenview can read EXIF rotation data and show them in proper direction (rotate on the fly). The problem begin when you upload such "good" portrait image or send it by email - it will be 90deg rotated. Ooops!

A small script can fix it and create subdirectories with 1280x1024 and 800x600 scaled images. It also rotates original images lossless preserving original EXIF data - don't be afraid about the quality!

To use this, simply add packages: imagemagick, exiftran & perl bindings Image::Magick.


#!/usr/bin/perl -w
`exiftran -aip *.JPG`;
`exiftran -aip *.jpg`;

use strict;
use Image::Magick;
use Cwd;

my $currdir = ( getcwd =~ m{([^/]+)$} )[0];

my $webdir = $currdir . '_to1280';
my $web800 = $currdir . '_to800';
print "Creating dirs: $webdir, $web800\n";
mkdir $webdir;
mkdir $web800;

print $| = 1;
foreach my $filename (<*.jpg>, <*.JPG>) {
# read this
my $image = Image::Magick->new();
$image->read($filename);

# scale it
print "$filename ";
$image->Thumbnail(geometry => '1280x1024>');
$image->write($webdir . '/' . lc($filename));

$image->Thumbnail(geometry => '800x600>');
$image->write($web800 . '/' . lc($filename));
print ".\n";
}

Friday, 21 September 2007

What I hate in perl5 and Catalyst

Perl - references



Let's remove all simple data-structures like @lists and %hashes. When we think "list" let use list-refs, ref-hashes instead of hashes. Let's keep only scalars and references with implicit dereferencing, like in Python or Ruby. No wantarray context-dependent behaviour! No @{$foo} and \@foo and other nasty code! Perl will be clear and more funny!

current perl:
$foo = [ map { ... } @$bar ]

better perl:
$foo = map { } $bar


As I see folks have problems why we write:
%hash = (foo => 42);
$hash{foo} # where is hash sign, % ?

@list = qw/foo bar baz/;
$list[1] # where is list sign, @ ?


and there is no much difference between lists and refs, but differences between various forms of using refs are probably bigger than list<->ref:

$list[1][2] # wolf
$$list[1][2] # dog1
$list->[1][2] # dog2
$list->[1]->[2] # dog3

Sure, there is only one dog, welcome to TIMTOWDI world!

There are also some troubles with
$ref = \@list;
$ref = [@ref]
# maybe it is @list?
$ref = [ $some->method ]
# or maybe it is %hash?
$ref = { $some->method }

# could be better to make something like
$ref = $some->method->return_as_hashref
$ref = $some->method->return_as_arrayref


Maybe I should stop writing perl code because I think too much about all those things? ;)

Catalyst - index is different than other actions


What is a difference between:
Controller app::foo;

sub index : Private {
my ($self, $c, $param1) = @_;
}

sub show : Local {
my ($self, $c, $param1) = @_;
}

?

Index will not catch arguments, will not work with /foo/123, although /foo/show/123 will work that way. To fix index, we need to use LocalRegex

sub index : LocalRegex('^(.*)$') {
my ( $self, $c ) = @_;
my ($param1) = @{ $c->req->captures };
}

Tuesday, 4 September 2007

YAPC::Europe 2007 in Vienna

Larry Wall, Damian Conway, many great perl hackers, interesting events, all in one place in one time.

It was my first YAPC and also first YAPC talk (slides from Catalyst refactoring are with others from this yapc on slideshare - also check perlbuzz.com) and Bartosz Jakubski's YAPC::EU 2007 photos.

However more interesting was to see in real life how this OS world works, met many interesting people from the perl community. There is probably nothing more interesting than long talks about POE, Catalyst and other kinda-like-app-servers or any other stuff during evenings near the Dunau Kanal and in other nice places in Vienna.

Huh, many words to say how great and cool was this YAPC!!!

PS: I will try to remember difference between Dbix and (Dbic or Dbix::Class) at least to avoid bot activation on #catalyst /and mst should also be happy ;)

Thursday, 16 August 2007

Ruby, Rails and Netbeans (compared to Catalyst)

In current time I am working on my bike-"startup" site, rowerem.org (in Polish: by-bike org) which promotes using bicycles in Krakow and cities at all. This is a free project, no money just fun and opportunity to learn something new.

For long time I was not sure I should do it in perl/Catalyst, some kind of python framework or Ruby/Rails. From the hosting point of view it should be php but... never mind ;) Because I like python, this simplicity, explicit constructions I was checking python frameworks. Unfortunately Django goes own way of MTV, Turbogears is not so well designed (opinions), Pylons - not so popular to have mature quality of all web-related libs. Inspired by latest billboards ads in the city I chose Rails ;)

What is good about Rails?

- Good documentation. You have few good, printed books: Rails Recipes, Rails Cookbook and of course Agile Web Development with Rails. If you have a problem with something, just google for it or check in book. Howtos on rails/doc are also helpful. The step-in cost is very small compared to eg. Perl/Catalyst even when I code in Perl for 10 years when Ruby is still fresh for me.

- not too deep directory structure. It is simpler compared to Catalyst.

- model (ActiveRecord) is really simple to use. Maybe DBIx::Class is powerful but hard to use for night development (after typical day in work). Or I am just too lazy ;)

- good IDE. Have you ever tried Netbeans 6? There are some issues like hard-to-work-without-mouse, hard to (re)define shortcuts / compared to Kdevelop / but... you have good, free IDE for Ruby and Rails projects!

Now it is very funny to extend Catalyst knowledge based on Rails usage - "there should be something like this in Catalyst", this works also in the opposite direction, because both frameworks are quite similar.

Do you use Catalayst? Or maybe you use Rails? What do you think about them?

Friday, 20 July 2007

Perl, unicode, utf-8, mysql

Handling unicode/utf8 in Perl is quite trivial when you understand "two string approaches".

Perl 5.8 by default can handle UTF-8 strings like a sequence of bytes (1-4 bytes per one char). You can "compress" them to unicode strings with
$wide_char_string = Encode::decode_utf8($octets)


Such encoded string has unicode flag, you can check it with:
Encode::is_utf8($checked_string)


If they have latin1/2/russian/etc. chars after unicode packing:
length($octets) > length($wide_char_string)


Remember: unicode flag does not mean you MUST have wide characters in string.
Wide characters (ord > 255) can be in such unicode string, but it can also has a set of unicode-octets, "unpacked" in unicode string.

Use Test::utf8 to check you have wide chars or not.

FAQ / typical problems:



- check all sources in your program are the same, coded as unicode (wide) or unicode-string. Typically wide-char-strings is a better approach then byte-string (see perldoc Encode). All inputs/outputs like files, DBI, network need to be converted to your choosen internal format.

- do not use use utf8 pragma unless you really need it. In this case all strings have unicode flag, but it does not mean they have wide chars!

(octet_string eq unicode_string) == false
You cannot compare such strings without decode/encode, they are natively different!

Perl, utf8, MySQL



Two approaches to get unicode working with MySQL:

1) after connecting to database, do("SET NAMES 'utf8');
All unicode strings will be octets, without unicode flag.

2) use DBI connection with flag: mysql_enable_utf8 (since DBD::mysql >= 4)
All unicode strings will have wide chars and unicode flag.

Only the second approach works correctly with AutoReconnect flag.

Good news: there is no difference for inserts/updates you use octets or wide_char_strings.

More to read



Check also this Martin Fowler - utf8 in perl

Friday, 13 July 2007

YAPC Vienna 2007 - my talk

I will have a Catalyst talk at Yet Another Perl Conference in Vienna, 28th to 30th August 2007 in Web track.

More on conference site,
http://vienna.yapceurope.org/ye2007/talks#18

Monday, 4 June 2007

Ruby & rails - thanks for nice weekend ;)

It is early Monday morning and I need to write it (however no one wants to read it).

My wife spent past 2 days in mountains (Tatry, never mind). I spent it with Rails. Or rather - with ruby. When I dig into Rails I sow all these :foo, :bar so I couldn't move forward without knowing WHAT DOES IT MEAN???

This is a time when books like "Programming Ruby" comes in hand. What I think now?

Ruby is really easy especially if you know perl :) I am not a fun of TIMTOWTDI and Ruby seems to be also... However there are ugly constructions like "reverse if/unless" so Ruby programmers can step to the Black World of Many-F^&*-Ways-To-Do-It... but they probably don't want, do they?

We need some ways in language to make different things looking different and Ruby allow us to do it. Oh, it looks like expressive programmers manifesto ;)

So once again:
Pretty nice language, with this object logic you thought "how it will be simple to have it done ONE WAY, objective". Really worth trying. Simple. Code looks nice.

I thought Ruby is too perlish and probably I should focus on clean Python syntax. However Ruby is better designed than Python. There are no "dirties" like:


$> Python
>>> foo = "bla bla bla"
>>> foo.upper()
'BLA BLA BLA'
>>> foo.len()
Traceback (most recent call last):
File "", line 1, in ?
AttributeError: 'str' object has no attribute 'len'
>>> len(foo)
11


In Ruby it is simple:
(even Python's dir(object) in Ruby is objective: obj.methods or even better obj.methods.sort)

irb> foo.upcase
=> "BLA BLA BLA"
irb> foo.length
=> 11


You can write it "Pythonic/java" way with parentheses, but they are optional:
irb> foo.length()
=> 11

Ok, so what about :foo, :bar and @others?


var = 123 # string
$var = 123 # global variable
@var = 123 # class variable
@@var = 123 # package variable


Classes:


def class1
attr_reader :duration
end

# which is a shortcut of:

def class1
attr_reader(:duration)
end

# which is a shortcut of:

def class1
def duration
@duration
end
end

# which is a shortcut of:

def class1
def duration
return(@duration)
end
end


:duration - means you passing "object symbol" not "value of it" (aiui)

If you come from Java world, now you know we don't need "make setters/getters" refactoring option in our Ruby editor/IDE ;)

We can strip methods parentheses(), we can strip returns, we can focus on code logic - this is why frameworks written in Ruby can be pretty nice, easy to understand and to remember. I just wonder when I will see a book named "Ruby best practices".

Friday, 11 May 2007

When is no Perl 6, we can try Ruby

You know perl, all the funny $_ / $& / q{} vars/ops, inline regexps but want to use object-oriented syntax with more typical dot, my_object.my_sub("blabla")?

There is Ruby, now probably more known as a one of dependency needed to fire up Rails MVC framework ;)

Start from here
http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-perl/

look for some code examples
http://migo.sixbit.org/papers/Introduction_to_Ruby/slide-index.html

and of course compare solutions in Ruby/Perl from mythical Perl Cookbook, done by PLEAC folks,
http://pleac.sourceforge.net/

For me Ruby is too perlish - after years of perl programming I fill better when I can do something in more clean Python syntax, with TIOOWTDI. However it's nice to see that Ruby is so close to Perl and there are some hacks you can do in Perl, Ruby but not in Python.

Wednesday, 21 March 2007

Benchmarks: python. Django vs. perl-> Catalyst

Ok so i made the benchmarks proper way.

Some stuff to know:
Perl: v5.8.8 built for i486-linux-gnu-thread-multi
Python: 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
Linux: ubuntu 2.6.17-11-generic #2 SMP Thu Feb 1 19:52:28 UTC 2007 i686 GNU/Linux
Apache/2.0.55 (Ubuntu) mod_fastcgi/2.4.2

Django was deployed in apache such way:
FastCGIExternalServer /path/to/fastcgi.fcgi -socket /tmp/mysite.sock

Catalyst was deployed that way:
FastCGIServer /path/to/script/mysite_fastcgi.pl -processes 3

Catalyst was easier to run as a FastCGI - in app/script you have ready to use fastcgi script. With Django you need to find the proper script at their web page. There was also problem problem with template paths - I fixed it by changing to absolute in settings.py. I didn't find on Django webpage the same deployment method as for Catalyst. I tried recipt from "Running Django on a shared-hosting provider with Apache" but got:

runfastcgi(method="threaded", daemonize="false")
TypeError: runfastcgi() got an unexpected keyword argument 'method'


All packages were "out of the box" - from system package manager, cpan or loaded from net (flup).

So how good are these frameworks? How much changed apache compared to own-servers benchmarks I made before?

Tested with: ab -c 25 -n 5000 http://adam/(django|catalyst)/; w
Numbers belowe are averages from 3 measurements/framework. Every measurement was started when 1m load average was below 2.0 (typically 1.75-1.9)

DJANGO

===============================>
Concurrency Level: 25
Failed requests: 21 (in 15k reqs)
Average: 127 requests/sec.
1-min load after: 11.93


CATALYST:

=============================>
Concurrency Level: 25
Failed requests: 0 (in 15k reqs)
Average: 118 requests/sec.
1-min load after: 2.49


Django was little faster however this method of deployment give me quite big number of errors. Catalyst with always 3 fastcgi processes was less CPU consuming and 100% error free.

Monday, 19 March 2007

First benchmarks: Catalyst vs. Django

This comparison is little silly because I don't have time to deploy mod_python / fastcgi / lighttpd or anything like such-good-approach. I just wrote the same code (make SQL join, show template with same fields, same logic) and check it with ab (apache benchmark tool) against framework own servers.

Catalyst




Server Software:
Server Hostname: localhost
Server Port: 3000

Document Path: /gallery/
Document Length: 7185 bytes

Concurrency Level: 1
Time taken for tests: 64.568406 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 36675000 bytes
HTML transferred: 35925000 bytes
Requests per second: 77.44 [#/sec] (mean)
Time per request: 12.914 [ms] (mean)
Time per request: 12.914 [ms] (mean, across all concurrent requests)
Transfer rate: 554.68 [Kbytes/sec] received


Django




Server Software: WSGIServer/0.1
Server Hostname: localhost
Server Port: 8000

Document Path: /
Document Length: 4981 bytes

Concurrency Level: 1
Time taken for tests: 61.358104 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 25625000 bytes
HTML transferred: 24905000 bytes
Requests per second: 81.49 [#/sec] (mean)
Time per request: 12.272 [ms] (mean)
Time per request: 12.272 [ms] (mean, across all concurrent requests)
Transfer rate: 407.84 [Kbytes/sec] received


With some modifications (eg: running together) the timings are like 100:96 for Django. A little faster. Will it differ much if we deploy it with Apache-and-the-fastest-way?

Sunday, 18 March 2007

Django (python) vs. Catalyst (perl)

Intro


I spend many days with Perl MVC framework - Catalyst. Catalyst for perl is like Rails for ruby but without all this marketing hype & noise (and unfortunatelly with lack of good books, tutorials and doc). I tried to use Rails for some easy-but-existing database. I was surprised that Rails ActiveRecord was not able to quote SQL names to get data from - as I remember - "lines" column in MySQL. Little stupid, hmm?

Anyway Ruby is too perlish. I know perl and know how much cost all this TIMTOWTDI when your project is growing up. Python is like clean, better Perl (however Python libraries are not at this quality level like Perl libs in CPAN - eg. unicode translations). So this is "why python".

Few days ago I started small project with Django. Here are first thoughts and differences:

Django view = Catalyst controller



Django call itself MTV framework (model-view-template). Going this way Catalyst is Model-Controller-(View)-Template - the only difference is: Django models == Rails & Catalyst controllers.

Django global switch


- In Django you need to define every URL action in urls.py and create appropiate "views"

Example of urls.py (aka "global switch"):

urlpatterns = patterns('',
('', 'hnl.photos.views.index'), # root page
(r'^admin/', include('django.contrib.admin.urls')), # admin
(r'^photo/update', 'hnl.photos.views.update'), # update
)


- In Catalyst, there is no such "global switch". You just create controllers (perl classes), put methods with "local" attribute inside - and they are visible in your URL namespace. Why you should define this namespace mapping if it is enough to do it only by proper view class-function naming? For me it looks like Catalyst dispatcher is more "pythonic" than Django.

Output validation



I created view to import some data from Yaml data-dump to MySQL database. Everything works however Django cannot display some rows because of problems with validation. Message error is not so helpfull -

ValueError at /
year is out of range

Hmm, but which field (i have 2 dates in every record) in which row? Exception is caught in template (first line)

{% for repo in gallery_list %}
* {{ repo.galleryname }}
{% endfor %}

but as you see - I do not fetch any date-time values here!

After looking into data I found '0000-00-00' date. Who pasted this record into database without problems? Django db model. Who was not able to show it? Django! I think it's a better idea to validate/filter data at input rather than output. Or in both places.

Wednesday, 14 March 2007

Objects in perl 5

There are many ways to do it. The old-school approach

blessed hash



sub new {
my $classname = shift;
my $rh = {}; # Reference to Hash, rh :)
bless $rh, $classname;
return $rh;
}


All variables can be created just by assigning something to blessed hash:



my $foo = my_class->new();
$foo->{bar} = 42;
print $foo->{bar};


Unfortunatelly there is lack of encapsulation - you have access to all methods and properties.

Get control of object properties - use fields




package my_class;
use fields qw(bar); # declare used object vars
sub new {
my $classname = shift;
return fields::new $classname;
}

# usage:
my my_class $foo = my_class->new();
$foo->{baz} = 42; # trying to use not declared field - error!
print $foo->{bar}; # works


We have control on object properties. Anyway, we still do not have private variables.


Private variables (scalar-bless)




package my_class;
{
my %bar; # All vars are declared as hashes
sub new {
my $classname = shift;
my $rs = \do{ my $scalar }; # reference to scalar
bless $rs, $classname;
return $rs;
}

sub set_bar {
my $this = shift;
$bar{$this} = shift;
}

sub get_bar {
my $this = shift;
return $bar{$this};
}
}


And now all variables are private!!!


Potential problem: it's not working with threads. To have a copy of such object instance in threads, you need to workaround this closure with get_all_data()/set_all_data() methods - and all object data need to be passed as additional parameters when creating new thread.


my $foo = my_class->new();
$foo->{bar} = 42; # sorry, no bonus! Use set/get method instead.
print $foo->{bar}; # This is private variable, not accessible outside class.
print $foo->get_bar(); # works!


Private, public, protected - objects like in perl6:


like in manual:


use Perl6::Classes;

class Composer {
submethod BUILD { print "Giving birth to a new composer\n" }
method compose { print "Writing some music...\n" }
}

class ClassicalComposer is Composer {
method compose {
print "Writing some muzak...\n";
$_->do_private;
}
method do_private is private { print "really private thing\n" }
}

class ModernComposer is Composer {
submethod BUILD($) { $.length = shift }
method compose() { print((map { int rand 10 } 1..$.length), "\n") }
has $.length;
}

my $beethoven = new ClassicalComposer;
my $barber = new ModernComposer 4;
my $mahler = ModernComposer->new(400);

$beethoven->compose; # Writing some muzak...
$barber->compose; # 7214
#$beethoven->do_private;
compose $mahler; # 8927586934796837469875


Pretty well, uhh? For me it was like "look, this is almost like in Java!" Just change is to extends.


Perl6::Classes works as a filter for code and do all this translation job. Well, we pay for it - it is 20x slower than hash/scalar blessed objects. If this latency doesn't matter - use it.