Qore for the Perl Programmer


Published on

Slides for a talk on the Qore Language directed at the Houston Perl Mongers group back in 2011.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Qore for the Perl Programmer

  1. 1. The Qore Language ... for the Perl programmer B. Estrade http://houston.pm.org/ September 8th, 2011
  2. 2. What is Qore? According to Qore.org: "... ●thread-capable, ●embeddable, ●weakly-typed ●optionally strongly typed ●procedural ●object-oriented ..."
  3. 3. Qore's Influences According to http://en.wikipedia.org/wiki/Qore_Language: But, docs mention: ●Perl ●D ●Java ●C++
  4. 4. Some of Qore's Primary Design Goals ●natively multi-threaded, so scalable on SMP architectures ●embeddable ●native object serialization (JSON, XML) ●native database support (MySQL, Oracle, etc)
  5. 5. Never heard of Qore? Neither had I. ●used to do a lot of simulations of parallel things with Perl, serially ●wanted to use real threads to model threading, but "knew" Perl's threading options sucked ●So, I searched for a while and found Qore ●I liked it so much, I created a FreeBSD port for it - http://www.freshports.org/lang/qore/
  6. 6. What's to like about Qore? ●Familiarity ○it's eerily similar to Perl in many ways - data structures, operators, sigils, subroutines, TIMTOWTDI (usually), etc ●Allows Freeflow of thought ○The language syntax and semantics stay mostly out of the way of thought and expression, much like Perl ●It's meant to be threaded ●Imagine if Perl was designed from the beginning to support threads.
  7. 7. The Reality ●Qore is not nearly as expressive as Perl ●Not as "DWIM" as Perl ●Threading support could be at a higher level ●No real community (like none), so no C"Q"AN ●Still very cool and not a toy
  8. 8. Scripting's Role in the Many-Core Era ●scripting languages will be exposed as handicapped if they can't make native use of many-core ●most scripting languages are terrible at this ●I believe that interpreted offer the greatest opportunity for intuitive interfaces to many-core; as opposed to: ○ compiler/directive support (e.g., OpenMP) ○ low level threading libraries (e.g., PThreads, Portable Coroutine Library) ○ low level communication libraries (MPI) ● Don't get me wrong, I still <3 OpenMP & PCL :)
  9. 9. State of Threads Many-Core in Perl? ●It's not just about threads, it's about taking advantage of many-core environments in as many Perlish ways as possible. ● Some options: ○ Coro, AnyEvent ○ ExtUtils-nvcc, CUDA::Minimal, Perl OpenGL ○ PDL::Parallel::MPI, Parallel::MPI::Simple ○ ..? ● Wishful Options: ○ Parallel::OpenMP ○ Perl OpenCL ○ Inline::Qore ○ ..?
  10. 10. Where to get Qore? ●http://www.qore.org (latest, 0.8.2) ●MacPorts (0.8.2) ●lang/qore in FreeBSD Ports (mine, @0.8.0)
  11. 11. "Core" Qore, Under the Hood ●Bison ○parser generator (like Yacc) ●Flex ○lexical scanner, tokenizer ●pcre ○Perl compatible regular expression library ●libxml2 ●supported modules require other libaries
  12. 12. Qore has Optional Data Typing ●boolean ●string ●int (64 bit, signed) ●float (double) ●date ●binary blob (opaque) ●NULL - a state of undefinedness, like undef; ●NOTHING - no value, like the empty string, q{}; NULL != NOTHING; NULL really is something; NOTHING really is nothing.
  13. 13. The World says, Hello. #!qore %enable-all-warnings print("Hello!n"); #!perl use strict; use warnings; print("Hello!n");
  14. 14. Data Containers #!qore %enable-all-warnings # scalars my $x = 1; # arrays my $list=(1,2,'three',4.0,2001-01-15Z); # hashes my $hash=("a":1, "b":'two',"c":2.4); #!perl use strict; use warnings; # scalars my $x = 1; # arrays my @list=(1,2,'three',4.0,'2001-01-15Z'); # hashes my %hash = ("a"=>1, "b"=>two',"c"=>2.4);
  15. 15. Data Container Iteration #!qore %enable-all-warnings # scalars my $x = 0; printf("%sn",$x); # arrays my $list=(1,2,'three',4.0,2001-01-15Z); foreach my $i in ($list) { printf("%sn",$i); } # hashes my $hash=("a":1, "b":'two',"c":2.4); foreach my $k in (keys $hash) { printf("%s = %sn",$k,$hash.$k); } #!perl use strict; use warnings; # scalar my $x = 0; printf("%sn",$x); #arrays my @list=(1,2,'three',4.0,'2001-01-15Z'); foreach my $i (@list) { printf("%sn",$i); } # hashes my %hash = ("a"=>1, "b"=>'two',"c"=>2.4); foreach my $k (keys %hash) { printf("%s = %sn",$k,$hash{$k}); }
  16. 16. Complex Data Structures #!qore %enable-all-warnings # arrays of arrays my $list=((1,2,3), (4,5,6), (7,8,9)); # hash of arrays my $hash=('a':(1,2,3), 'b':(4,5,6), 'c':(7,8,9)); #!perl use strict; use warnings; # arrays of arrays my @list=([1,2,3], [4,5,6], [7,8,9]); # hash of arrays my %hash=('a'=>[1,2,3], 'b'=>[4,5,6], 'c'=>[7,8,9]);
  17. 17. Some Qore Array and Hash Operators Arrays: ●shift, unshift ●pop, push ●splice ●map, foldl, foldr ●elements (counts) Hashes ●keys (insert/creation order) ●delete (clear value) ●remove (remove from hash) ●elements ●find (query contents of hash on key and value)
  18. 18. Regular Expressions - whoa... #!qore %enable-all-warnings my $t = 'Branches'; # text my $s = 'abc'; # string my $p = 'a|z'; # pattern printf("%s %s: "'%s' =~ /%s/"n", ( $s =~ /a|z/ ) ? 'PASS' : 'FAIL' ,$t, $s, $p); $s = 'qrs'; printf("%s %s: "'%s' !~ /%s/"n", ( $s !~ /a|z/ ) ? 'PASS' : 'FAIL' ,$t, $s, $p); #!perl use strict; use warnings; my $t = 'Branches'; # text my $s = 'abc'; # string my $p = 'a|z'; # pattern printf("%s %s: "'%s' =~ /%s/"n", ( $s =~ /a|z/ ) ? 'PASS' : 'FAIL' ,$t, $s, $p); $s = 'qrs'; printf("%s %s: "'%s' !~ /%s/"n", ( $s !~ /a|z/ ) ? 'PASS' : 'FAIL' ,$t, $s, $p); ... code is valid in both Perl and Qore!
  19. 19. Subroutines & Closures #!qore %enable-all-warnings # defined sub sub say_hello (string $name) { printf("Hello, %s!n",$name); } # call sub say_hello("Frank"); # anonymous sub via closure my code $anonymous_sub = sub (string $name) { printf("Hello, %s!n",$name); }; $anonymous_sub("Frank"); #!perl use strict; use warnings; # defined sub sub say_hello { my $name = shift; printf("Hello, %s!n",$name); } # call sub say_hello("Frank"); # anonymous sub via closure my $anonymous_sub = sub { my $name = shift; printf("Hello, %s!n",$name); }; $anonymous_sub->("Frank");
  20. 20. Qore Subroutes & Closures ● General form is one of 2: [return_type] sub func_name([[type] variable,..]) { ... code block } OR sub func_name([[type] variable,..]) [returns return_type] { ... code block } ● Subroutines and closures also provide event handlers (inspired by D): ○ on_exit ○ on_success ○ on_exit sub myfunc() { on_exit do_x(); #subroutine called on exit, unconditionally ... code block; return ..something; }
  21. 21. Classes in Qore (w/o Perl counter example :( %require-our %enable-all-warnings class MyClass { # declare some private members private $.base1, $.x; constructor($a) { printf("Base1::constructor(%n)n", $a); $.a = $a; } destructor() { printf("Base1::destructor() (%n)n", $.a); } copy() { printf("Base1::copy() (%n)n", $.a); $.a = $.a + "-copy"; } hello() { printf("Base1 hello (%n, derived class %n)n", $.a, cast<Mid>($self).subclass()); } }
  22. 22. Qore Threading...finally ●invoked with background keyword ●%require-our to declare shares globals with "our" ○useful for implicit communication via shared memory ●use "my" to specify thread-local variables ●synchronization ○locks ○gates (recursive locks) ○conditional block ○mutex ●communication ○thread safe Queue class
  23. 23. The World says, Hello_r. #!qore %require-our %enable-all-warnings #shared, thread safe our $tcount = new Counter(); sub say_hello_r () { on_exit $tcount.dec(); my $tid = gettid(); printf("Hello! from Thread %sn",$tid); } for (my $i = 0; $i < 9; $i++) { $tcount.inc(); background say_hello_r(); } $tcount.waitForZero(); $qore ./hello_r.q Hello! from Thread 2 Hello! from Thread 3 Hello! from Thread 4 Hello! from Thread 7 Hello! from Thread 6 Hello! from Thread 10 Hello! from Thread 5 Hello! from Thread 9 Hello! from Thread 8 Hello! from Thread 11
  24. 24. ●higher level constructs, similar in spirit to OpenMP (fork/join), e.g.: ●better data environment control (private,shared,etc) ●process affinity control ●memory allocation/migration control (first touch, next touch) ●logical affine "locations" My Thread Support Wish List our $count = 0; background { critical { $count++; }; };
  25. 25. A lot more to Qore ●modules for database, XML, JSON ●interesting operators and idioms ●embeddable ●plenty of warts and room for improvement ○lacks syntactical sweetness of Perl ○higher level threading features ○needs more DWIM
  26. 26. Possible Future Talks, Discussions ●Perl's threading and asynchronous options (not me:) ●More on Qore's threading ●How to embed Qore (a design goal) ●Qore for web apps (design goal) ●Using Qore for threading in Perl - Inline::Qore, anyone? ●'Perqore' challenge, Polyglot programming with Perl & Qore ●Cray's Chapel Language - not as Perlish as Qore ●Lua - threading and coroutines ●Any D fans?
  27. 27. Qore Resources http://www.qore.org ●core language documention ●module documention ●C++ API Doxygen docs (for embedding & internals)