Daniel Ruoso

Últimos Posts

Categorias

Arquivo

Nuvem de Tags

01.07.2009  Perl 5 backward compatibility is not only for DarkPAN

NPEREZ has written that we shouldn't care about the DarkPAN, Both AdamK and SF had argued about it, but I'd like to take it to another perspective.

One of the key strengths of both Perl and perl, has been the way old code is still supported in the language. That is a decisive aspect as when to choose a language to invest a lot of time, and by that, money. I'll take an example that is not part of the DarkPAN and that has been, just last week, released as free software.

About a year ago, I started working in a software, and of course, I chose to write it in Perl. More than that, I have also made some interesting choices, that including the use of SOAP over XMPP to design the choreographies of the application, as well as the use of Catalyst to implement that. That led me to write some new CPAN modules. Basically I was using XML::Compile, XML::Compile::Schema and XML::Compile::SOAP as the foundation to the entire application.

As the software evolved, a new major release of the XML::Compile framework was released, and it mostly changed the API and a lot of default behaviors. This change costed me about 3 months in investigation and patches, both to my code, to XML::Compile's code and to some other modules just to have the code working again.

Imagine what happens if the defaults for the core changes? That would represent an incredibly higher number of issues to be dealt with, and we don't actually have a choice of "sticking with the old perl", because the developers usually are not the sysadmins, and having to convince a sysadmin that you need to install an old, unsupported version of a CPAN module is already bad, imagine having to convince a sysadmin to install an old, unsupported version of perl? and by that it would mean to keep an entire module tree specific for that version?

That would certainly be a reason for a manager to decide to switch over from Perl to something else, and a very good one. In fact, one of the key strengths of Perl is precisely the long backward compatibilty. And that is not only important only to the DarkPAN, but for every single piece of legacy code in the free software ecossystem as well.

Postado por autor: ruoso em Perl.   Tags  Perlbackpanbackward compatibilityperl5.

29.06.2009  Perl 6 - The quest for the holy grail

Follows the slides fo the talk I presented at FISL. I liked a lot the reaction of the public, I was afraid of using simplistic examples or to distance people, but the response was very much positive. In general, I saw faces of surprise and satisfaction in the audience. Very good indeed.

Slides: Perl 6 - The quest for the holy grail

Postado por autor: ruoso em Perl.   Tags  fisl10Perlfislslidesperl6.

28.06.2009  FISL10 - Almost YAPC

Getting home after FISL10, which happened in Porto Alegre, where all the free software geeks from Brazil get together to not attending any of the talks, but drinking beer and having lots of fun. This time it wasn't different, besides all the distractions in the environment, including:

  • The Sun booth, which frequently was throwing small balls away with both "Java" and "Mysql" logos on it. The only thing we had to say about it: "it isn't a bad idea being able to kick java and mysql all day".
  • The globo.com booth with the Brasil x South Africa, including all the noice from the crowd around the TV.
  • The lines that were constantly formed in the booths that were serving espressos.
  • Eventual use of speakers to announce some random prize or contest in some random booth.
  • The visit of the president, which kept us away from the user group area an entire day, but didn't stop us from having the Perl booth open on the outside (Flavio Glock has pictures of the process of putting the banner in the light post)
  • One more example of the sexist intent of the ruby community, with three fashion models being casted to walk the entire event using shirts with "We hire rails" (I don't know if there was a pun intended or not, but still, a bad sexist idea).
  • The flacky network infraestructure, lucky of us who had 3g modems from home, hoping no roaming taxes will be charged.

All that being said, I had very much fun with Perl people from all around Brazil. I took part in the cerimony for the launch of a system I've been working for more than a year as free software in the Brazillian Public Software Portal, and I gave a talk titled "Perl 6 - the quest for the holy grail". Each of one deserves a particular post. So that's all for now.

Postado por autor: ruoso em Perl.   Tags  fisl10Perlfislcommunityfree softwareperl6perlmonks.

23.06.2009  1984 Revisited

A lot of people think that in 1984 Orwell was more focused in the control of the people by the state. Independently of what the author originally had to say, my reading of the book was always more focused in the control of the people by the people, and this was always the most scary part of the prognostic of the future.

The Panoptic, as described Foucault, is the model of social control where the subject lives under a constant surveillance, at the same time as the watcher can "see without being seen". What means is that the survaillance is amplified by its potentiality, you never know wether you're being watched or not, then you have to presume you're being watched all the time.

This is a very serious "disciplinement" process, and, as says Foulcault, at a certain point, the subject will absorb the "disciplinement" to an extent that the "indisciplinated" behavior ceases existing.

To fight the over-surveillance by the state is not a new issue, 1984 is an over-60-years-old book. What changes is the perspective of the reading. Today there's no doubt that the Big Brother is actually "virtual" and that we could understand it as the hegemonic moral principles in our society. We're entering in a critical time for self-censorship and self-regulation.

The Big Brother, now, is ourselves. Everybody exposes their private lives in the social networks, publish the pictures of the weekend for everyone to see. And this news I saw today only shows one more aspect of this contemporary "auto-disciplinement". Fortunally this is still an example of perceived "auto-disciplinement", but as the things go, this will also become normal, and people will push you to be present there as well.

Anyway, the news: Lancaster, Pa., keeps a close eye on itself

Postado por autor: ruoso em Social.   Tags  1984censorshipfoucaultSocialorwellcontrolpanoptic.

18.06.2009  Ninar -- Lullaby

A few years later, I wrote another song. There isn't much to say, except that the song is lincensed under the CC-BY-SA, so you're free to use it as you please. I'm including the Rosegarden file, the mp3 export using freepats with timidity, and a pdf export of the sheet music by lilypond.

Ninar -- Sheet Music

Faça o download do Flash Player para visualizar o player.

Ninar -- Rosegarden

Making Of

The tools I used were Rosegarden, Jack, Timidity, Freepats and Audacity. To do it I started jack, then I started timidity with "-iA -Oj" as arguments to tell it to register as an alsa midi device and to have its output connected to jack. After that I started Rosegarden, the default conections happen in a pretty straight-forward way. The timidity part is needed because my sound controller, as most notebook controllers, don't have a midi sequencer.

First I recorded an audio track with the melody, singing it, then I started the first midi track by putting that melodie into the note editor of rosegarden, fixing the tempo in the process. After that, I wrote the second midi track, which is the bass.

When that was over, I took the timidity output in jack and connected it back as a rosegarden input, so I could record an audio track with the timidity's output. Then I openned that segment in Audacity and exported as mp3.

Postado por autor: ruoso em Music.   Tags  audacityfreepatsjacktimiditysheet musicMusicrosegarden.

17.06.2009  Serial communication in Perl

João Gabriel asked in cascavel-pm:

Is there a possibility of using a Perl script (in a Linux environment) to send and receive data from a parallel port (/dev/ttyS0)? Did someone do that with Perl already? Should I adventure myself in Perl or should I jump to C?

Well, you just listed the most evident case of when programming for Linux is easier that when programming to Windows. In summary, the serial port is a file you read from and write to. It's worth to mention that /dev/ttyS0 is a serial port, not parallel. The parallel port is usually /dev/lp. They are fundamentally different, as one implements the RS232 protocol to encode a given stream of information, while the parallel port is just a set of pins you can manipulate. Well, that being said, let's go to the interesting part... First of all, you need to open the port, and to do that:
  use IO::Handle; # so you can use OO API with filehandles
  open my $fh, '+<', '/dev/ttyS0' or die $!;
The '+<' open mode means that you're openning it for both reading and writing (perldoc -f open is your friend). After that, you probably want non-blocking IO, except if your protocol os considerably predictable (it never is). To do that:
  $fh->blocking(0);
It might be necessary to configure the serial port, since the device might not be using the same default configuration as you are. In oreder to do that you need access to the POSIX API, specifically termios.h. So, the first thing you need is:
  use POSIX qw(:termios_h);
Then, to configure the port you can do something like:
    my $term = POSIX::Termios->new;
    $term->getattr(fileno($fh)) or die $!;
    $term->setiflag( $term->getiflag &
      ( &POSIX::IGNBRK | &POSIX::IGNPAR &
        ~&POSIX::INPCK & ~ &POSIX::IXON &
        ~ &POSIX::IXOFF));
    $term->setlflag( $term->getlflag &
     ~( &POSIX::ICANON | &POSIX::ECHO |
        &POSIX::ECHONL | &POSIX::ISIG |
        &POSIX::IEXTEN ));
    $term->setcflag( $term->getcflag &
      ( &POSIX::CSIZE | &POSIX::CS8 & ~&POSIX::PARENB));
    $term->setospeed(&POSIX::B1200);
    $term->setispeed(&POSIX::B1200);
    $term->setattr(fileno($fh), &POSIX::TCSANOW) or die $!;
perldoc POSIX and man termios (install manpages-dev) help you understand what it does. Now you're ready to read and write from/to the serial port, but unless you have a very controlled environment, you don't want to do that in a synchronous way. And for that I recommend using the EV module, which will give you all the tools you need to do the async communication.
Postado por autor: ruoso em Perl.   Tags  Perlserial porttermiosevasyncperl5io.

16.06.2009  A plan for module loading in mildew

Module loading in Perl 6 is considerably different than in Perl 5. The biggest difference is that in Perl 5, you're always in the main namespace and that is global, which means that it doesn't matter what gets some module loaded, if it ends up in main, you'll be able to find it. Like:

# in some random file loaded indirectly...
*{'main::Foo::Bar::new'} = sub {
  return bless {}, 'Foo::Bar';
}
# in your source file
my $a = Foo::Bar->new();

In Perl 6 that is not the case, when you ask for Foo::Bar, it will do a lexical lookup for the package Foo, which means that it won't immediatly look for it in the global namespace. Although all files start parsing in the GLOBAL package, and by default a class is our, which means that

use v6;
# in some random file loaded indirectly
class Foo::Bar {
  ...
}
# in your source file
my $a = GLOBAL::Foo::bar.new(); # this will work
my $b = Foo::Bar.new(); # this won't

That means that, unlike Perl 5 require, module loading in Perl 6 is not just about loading a file, but it also needs to install a symbol in your lexical scope pointing to the loaded module. And to do that Perl 6 has four control statements: “need”, “use”, “require” and “defines”.

  • “need” simply loads something at compile-time without doing any import;

  • “require” is the run-time version of “need”;

  • “defines” explicitly tells to import some module's exports;

  • “use” is a shortcut to “need X defines *”

But that goes a bit beyond that. Consider the following source file, named Sense.pm:

class Sense {
  ...
}
class Nonsense {
  ...
}

When that module is loaded, the two classes are loaded into the comp_unit scope and aliased in the GLOBAL package (as they are “our”). When you

need Sense;

It will load Sense.pm, but it also need to have a local symbol for the “Sense” name, so somehow the implementation of “need” will, at some point, have access to the comp_unit scope of Sense.pm, in order to find a symbol named “Sense”, and alias it back to the current scope. The practical implication to that is that “Nonsense” is not available as a local symbol, but it also tells us a lot about how the loading process work. Basically:

The module loading requires the code causing the load to have access to the recently-loaded-module's outermost lexical scope!

So, it seems that module loading will require one to cache the relationship between the filename and the outermost lexical scope, so the compilation is re-used when some other code “need”s this module. There are some other subtle issues about module loading that I'm going to adress in other posts, but that's it for now.

Postado por autor: ruoso em Perl.   Tags  loadingPerlmildewsmopperl6module.

16.06.2009  RPN Calculator in Perl 6

As the wikipedia says, RPN (Revered Polish Notation), is a kind of calculator that accepts an input which is a bit different than some people is used to. But it is a great way of dealing with long calculations without needing parens grouping.

This notation is very common in finance calculators. Besides the use in math, the problem of implementing a RPN calculator is one of the “Hello World” problems implemented in a lot of programming languages. And, as we are talking about Perl 6, let's get our hands dirty.

Daniel Carrera implemented a version using grammars, but I wanted to make a different version using multi-subs, which is one of the most important features of Perl 6. I'll just throw the code, that you can save as rpn.pl

multi infix:<rpn> (@stack, $num where /^ \d+[\.\d+]? $/) {
  [ @stack, $num ];
}
multi infix:<rpn> (@stack, $op where /^ '+' | '-' | '*' | '/' $/) {
  my $lhs = @stack.pop;
  my $rhs = @stack.pop;
  my $res = do given $op {
    when '+' { $rhs + $lhs }
    when '-' { $rhs - $lhs }
    when '*' { $rhs * $lhs }
    when '/' { $rhs / $lhs }
  }
  [ @stack, $res ]
}
multi infix:<rpn> ($any, $unknown) {
  die "Unkown expression near $any $unknown";
}
say [rpn] [], (~@*ARGS).words;

That being done, considering you already has rakudo installed, you can just run

perl6 rpn.pl "5 4 + 3 / 5 3 - *"

And by that you should have the result “6”. Don't worry if you didn't understand all the details of that code, it uses some new features of Perl 6, some of them don't exist in any other language. If you have experience in some other languages, you might find weird that this code doesn't have any loop, as you would need to iterate all the items of the expression. In fact, this code shows, to all its extent, the functional heritage of Perl 6, and all happens by the “reduce” operator and the signature of each of the candidates of the multi-sub.

infix what?

One of the most unexpected things in this code is the “infix” in the name of the routines. The use of that term indicates that this is not a regular routine, but it is an operator, and that it can be used as any other operator.

What that means is that in Perl 6 all operators are defined in high-level. They are not language primitives, which means you can do:

multi infix:<+> ($a where 2, $b where 2) {
  return 5;
}
say 2 + 2;

And the result will be “5”, because the signature of this candidate is narrower than any other candidate for infix:<+> (Rakudo currently has a bug that prevents this code from working).

But after all, what is “infix”? Well, it defines the kind of the operator, follows a table explaining the types of operators you can create:

Type

How it appears

Examples

How to declare

Infix

$a OP $b

1 + 1, 4 * 4, 'a' x 4

multi infix:<+> {...}

Postfix

$aOP

$a++, 4i

multi postfix:<i> {...}

Prefix

OP$a

++$a, ^$a

multi prefix:<^> {...}

Postcircumfix

$aOP $b OP

$a{$b}, $a[$b]

multi postcircumfix:<{ }> {...}

Circumfix

OP $b OP

( $a )

multi circumifx:<( )> {...}

What that means is that, in fact, that multisubs are declaring a new operator, the “rpn” operator. And, after that declarations, you can simply write:

2 rpn 3

And it would take one of the candidates to execute (in this case it would execute the “die” one). But we could succesfully do the following using the operator:

(([] rpn 2) rpn 3) rpn '+'

Then it would return 5. But fortunally, to our sanity, there is a meta-operator in Perl 6 that does that in a much nicer way, which is “reduce”.

[rpn] [], 2, 3, '+'

The reduce meta-operator will call the infix rpn operator using the same kind of grouping of the previous example, making it all simpler.

The last line

This is a very concise line that says a lot, let's go step by step:

~@*ARGS

The @*ARGS variable contains the parameters sent to the program, and the the prefix ~ operator is used to “stringify” a give value, which in the case of the array, means that it will join all elements with a space. What that menas is that it doesn't matter if you send the entire expression as a single parameter, or each part of the expression as a different parameter, it will work either way..

(~@*ARGS).words

The “word” method in strings, will split this string into a list of words. It's more or less the same thing as using split with space, but as this is so common, there's a specific method to do that.

[], (~@*ARGS).words

That line is composing a new list which will contain an empty array as the first element.

[rpn] [], (~@*ARGS).words

That line will call the reduce meta-operator with the rpn operator, using the list as the argument, and that will reduce the input list to the rpn result, which in the end is sent to the user with “say”.

Postado por autor: ruoso em Perl.   Tags  snippetperl6Perlprogrammingfunctionalmultiinfixcodereduce.