Driving Behavioral Change for Information Management through Data-Driven Gree...
Â
Qore for the Perl Programmer
1. The Qore Language
... for the Perl programmer
B. Estrade
http://houston.pm.org/
September 8th, 2011
2. What is Qore?
According to Qore.org:
"...
âthread-capable,
âembeddable,
âweakly-typed
âoptionally strongly typed
âprocedural
âobject-oriented
..."
3. Qore's Influences
According to http://en.wikipedia.org/wiki/Qore_Language:
But, docs mention:
âPerl
âD
âJava
âC++
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. 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. 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. 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. 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. 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. 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. "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. 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. The World says, Hello.
#!qore
%enable-all-warnings
print("Hello!n");
#!perl
use strict;
use warnings;
print("Hello!n");
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. â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. 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. 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?