Category Archives: Coding

Posts about coding stuff

Teaching my first Software Carpentry Workshop

Group Photo at the finish of the AUTH SWC workshop (Oct 2016)
Group Photo at the finish of the AUTH SWC workshop (Oct 2016)

My first Software Carpentry workshop“. Sounds a bit like a first-grader’s essay. But in all honesty, it was a brand new experience for me. Sure, I have been teaching University lectures and seminars for quite some time, but doing a live-coding session was an experience of its own

All things considered, the workshop was a success (evident also in the group photo here); participants were engaged throughout the two-day workshop, there was continuous feedback and discussions on tough points in the lesson, so everyone left quite happy and (hopefully) more knowledgeable than before.

For me, two things have become even more clear:

  1. Better organization of the workshop in terms of the participants’ level of experience. Sure, very few actually complained for the pace, but I have the impression that we could do a better job if we had focused only on beginner level (for example) or intermediate.
  2. Two days may be convenient in terms of time allocated for any person (given the rather busy schedule of most people now), but it’s a very short time to cover R, Shell and Git. So maybe next time (because there will definitely be a next time) we will aim for three days.

Oh, and a final piece of advice: if you intend to have food or catering at a SWC workshop, ordering pizza may be the best solution…. ūüôā

File Upload using Perl and DANCER

One of the most common functions in a web application is File Upload. The following is a working code that can be used to this end. The prerequisites are only Perl (obviously) and the DANCER framework. Right! Here goes:
post '/upload/:file' => sub {
  my $upload_dir = "/home/fpsom/myApp/UPLOADS";
  my $filename = params->{file};
  my $uploadedFile = upload('file_input_foo');
  debug "My Log 1: " . params->{file};
  debug "My Log 2: " . ref($uploadedFile);
This should be copied in the myApp/lib/ file (check for the corresponding file in your app)
In order to check the functionality of this code, I’ve used cURL as follows, where testUploadFile is an existing file:
curl -i -F file_input_foo=@testUploadFile http://localhost:3000/upload/testUploadFile
The output on the “development dance floor” should be as follows:
[10269]  core @0.000119> request: POST /upload/testUploadFile from in /usr/local/share/perl/5.14.2/Dancer/ l. 56
[10269]  core @0.000413> [hit #4]Trying to match 'POST /upload/testUploadFile' against /^\/upload\/([^\/]+)$/ (generated from '/upload/:file') in /usr/local/share/perl/5.14.2/Dancer/ l. 84
[10269]  core @0.000530> [hit #4]  --> got 1 in /usr/local/share/perl/5.14.2/Dancer/ l. 102
[10269]  core @0.000627> [hit #4]  --> named tokens are: file in /usr/local/share/perl/5.14.2/Dancer/ l. 130
[10269] debug @0.001051> [hit #4]My Log 1: testUploadFile in /home/fpsom/myApp/lib/ l. 21
[10269] debug @0.001145> [hit #4]My Log 2: Dancer::Request::Upload in /home/fpsom/myApp/lib/ l. 22
[10269]  core @0.001446> [hit #4]response: 200 in /usr/local/share/perl/5.14.2/Dancer/ l. 179
And that is that! Hope it helps.

Perl DANCER framework

After quite some time, I had to go back to writing web-services. I can’t say I was really looking forward to it, but don’t get me wrong; I love writing code and I feel quite confident at it. On the other hand, my memories in writing web-services aren’t the most comforting ones. Admittedly, last time was in 2008 and was¬†writing in Java at that time, so I had to juggle with a lot of things to get them working just right, but still…
Anyway, my project now is in Perl so I had to look a little bit around for any existing frameworks that might make my life easier, when I came up with the DANCER framework. And it was a brand new day…
That was two days ago. In this time I’ve been able to setup almost everything writing only a fraction of the code I used to. Now I am working on the particulars of the application behind the web-service. The setup of the service is staightforward; the DANCER dev team actually provides a tool to help along the way. But (as in every transition between languages) there are several bumps along the road after that. So, in order to help anyone interested, I’ll share any “recipes” that I come up along the way.
Good coding to all!