Perl confusing examples

  1. Confusing output order:

    use warnings; use strict; use 5.010;

    print 'OK'; my $x; say $x; $x = 42; say $x;

This code prints statements before the line generating the warning, the result might be confusing:

Use of uninitialized value $x in say at perl_warning_1.pl line 7.
OK
42

The reason is that by default Perl buffers STDOUT, while not buffer STDERR.

Turn off the buffering of STDOUT by: $| = 1; at the beginning of the script.

  1. Second most common warning: Use of uninitialized value:

Usually means variables have no value: either it never got a value, or at some point undef was assigned to it.

  1. Unknown warning category 'xxxx':

    use strict; use warnings

    print "Hello World";

Missing semicolon after using warnings statement. Perl is executing the print function and returns 1 indicating that is was successfull. changed to:

use warnings 1

Diamonds are a Moose's Best Friend

I was happily enjoying my usual playing about when I made a slight change to my nice new Moose class and suddenly my thingy no longer worked as it could no longer couple with my gonkulator to produce a splork.

Fortunately I am an old school perl programmer and spotted the problem right away. It seems Moose's 'extends' works the same way 'use base', or if you want to go real old school push(@Some::Class,"Some::Other::Class") , does. In other words, the order in which you get something out, in this case an inherited sub, depends on the order it goes in.

So I was wondering how moose handles The Diamond Problem . In other words given a class diagram like this
     {A}
     /   \
    {B}   {C}
     \   /
     {D}

We have D inherits from B,C which both Inherit from A. How does Moose handle this. Is it as I expect the same as trad perl or is it different.

So here is our little diamond in Moose

how I learned to plot a mandelbrot set

When I first met fractals in the school, I thought that they were if not magic, then something very close to it. The concept of a fractal dimension was something I couldn't react to logically, but only emotionally, basically, with a "wow" and nothing much else. I never tried though to write a program that plots the set, even though there plenty of examples around; a deity isn't there to be analyzed, only to be awed at.

Enter year 2013, and I find myself reading PDL mailing list where someone has posted a nicely written educational acticle that explains some concepts of Perl Data Language (i.e. PDL) and plotting the Mandelbrot set as an example. I liked it very much, the only trouble is that apparently PDL's plotting is done using external programs. I'd rather see a GUI toolkit used, withing the same program.

New Dancer 2 release: 0.11

Hi everyone!

A new version of Dancer 2 has just been uploaded to CPAN: 0.11.

Before we go over the changes we've had, I'd like to take a moment to welcome Russell Jenkins (veryrusty) as a member of the Dancer core dev team. Russell has done tremendous work on Dancer and provided patches, feedback, corrections, insight, and more. He's been a crucial member of the team for some time, and it's about time for him to join officially. Please welcome Russell!

Much has changed since the last release. There had been a lot of documentation fixes and improvements. Documentation is something developers tend to neglect, but it is crucial for the success of a project. Contributors and users depend on it. That's why we especially recognize and appreciate any contribution to the documentation.

Perl and Me, Part 2: The Power of OOP

This is part 2 of an ongoing series where I explore my relationship with Perl.  You may wish to begin at the beginning.

This week continues the topic of why a former C++ programmer might retain some nostalgia after moving to Perl: objects.

Last week I talked about my discovery of object-oriented programming and how it led me to become a C++ programmer.  As I described, it really was an epiphany: we would say back then that I suffered a paradigm shift, although that’s become somewhat cliché these days.1  From this you might gather that I’m an object zealot, but I’m not.

Here’s my thinking on OOP.

Here we go Route de Route

in principio creavit

Life was easy on the web so many years ago, urls where simple. You only bothered had one.

www.mysillylife.com/main.cgi

You simply tacked on all sort of extra little bits on end to know, what to do, what to show, where and who are you, and such things as how long you have been here. Something akin to;

www.mysillylife.com/main.cgi?user=blogins&lastvisit=1928282&mode=update&sessionkey=24823498....

anyway you get the picture. We only had get and post and a good number of sites dropped get when they discovered you could hide things in post. Of course you would see cases just post to the same page and your "mode" took care of things for you or some pages knew what to do with a post or a get.

divisit lucem ac tenebras

More WordPress grepping

I got quite a bit off my blogging to-do list once I created WordPress::Grep to crawl through my WordPress database. With that stuff done, I had time to expand the module to get even more done.

I added stuff to search categories and tags, which are mixed together in this odd notion of WordPress "relationships". One of my issues has been the evolving set of categories. I create a new category realizing that I'm writing stuff that I want to group, but I don't go back to add the new category to old posts. Or, I need to do something to every post in a particular category (say, change links to use MetaCPAN). Or, all sorts of things.

After that, I had fun thinking about how I should report the results. I've been interested in Bart Lateur's String::Sprintf, so I could define my own handlers for the field specifiers:

Improve translation of some chapters of my Perl tutorial site 1

I am translating my Japanese Perl site to English.

I improve the quality of translation of the following contents.

1. Command Prompt
2. What is Perl?
3. Perl Installation
4. Master Perl Basic Syntax Quickly

Single process versus parallel tests

Whenever I present a talk on Test::Class or one of its variants, invariably someone asks me about parallelization. The reason is simple: I advocate running your xUnit tests in a single process instead of multiple processes, but it's hard to run tests in parallel when they're already forced into a single process.

For Test::Class, this means using separate *.t tests for every test class versus using a single *.t test and Test::Class::Load.

I am working on making parallel tests possible with Test::Class::Moose, and while I have test classes running in parallel, the confused is output (yes, that was deliberate). I know how to solve this using only publicly exposed APIs, but there are some tricky bits. I thought about asking for a TPF grant, but since most don't use xUnit style testing, the value seems marginal. Plus, I am on the Board of Directors for the Perl Foundation and that can look like a conflict of interest. Hence, my slow work in this area.

That being said, it's worth doing the math and asking ourselves where we get the greatest gain.

Am I a Convert???

Some time ago I was on a team that was rewriting a very large legacy system. We wanted to use a more maintainable OO style (rather then just a huge global hash-ref ) but unfortunately the team was restricted by using only perl 5.6.0. By the time I joined the project work was well underway and I was still stuck doing accessors by hand. So I came up with this little beastly Orignal to solve that problem.

It fixed things up nicely as we could have nice 5.14 style accessors with some silly other little bits that came in very handy at least to us.

Grepping my WordPress database

I created WordPress::Grep (or in GitHub) as a way to do power searches through my WordPress databases. I've often wanted a tool that could search with Perl patterns or arbitrary code to find odd CSS uses, check links, and all sorts of other things. I didn't see an easy way to do the things I wanted with WordPress::API, which seems more of an authoring tool than an administration or editing tool.

Having some time after patch -p1 in Paris, I started to work on this. After dealing with the horror of the PHP front end, I was surprised at how easy I got something working--the database setup isn't that bad. Now I have a basic tool that I use like this:

% wpgrep --host ... --user ... --database ... --regex ... 

I've got a good start and it's doing what I need for now, but there are other things on my mind:

Looking for some help with Japanese POD

I maintain Term::ReadPassword::Win32 that has a POD file in Japanese. Now it got 2 bug reports for the Japanese POD, but even though both Japanese and Hungarian write their family name first, I don't know Japanese that well.... To say the least.

I am looking for a nice person who would check if the content of that files is still relevant. Convert it to utf8 and fix the two bugs reports related to that file. (See the bug reports and the GitHub repo linked from the above page.)

Migrating from Locale::Maketext to Locale::TextDomain

Inspired by a recent Perl advent article, I'm migrating my CPAN distributions from using Locale::Maketext to Locale::TextDomain.

Pros:

  • I get to use all the available gettext tools (for scanning translateable strings, for merging and updating them to each translation file, specialized text editors for editing translation, etc). This is the definitely nicest thing about the migration. With David Wheeler's Dist::Zilla plugin, the workflow is basically 'dzil msg-scan', 'dzil msg-merge', and update translations (the plugin will do 'dzil msg-compile' for you during build).
  • I no longer need to create project (translation) classes. I've always disliked having to do that, especially if my module or application is not OO.
  • I get named parameters for values. Instead of having to write [_1], [_2], etc, I can now use {foo}, {bar} instead. Translation text become clearer.

Cons:

What is in The End?

Well a good day today. Had an interesting though come by my desk. Was debugging a problem with a colleague and in our back and forth he came up with the little jape'
'I prefer to never return anything from a perl subroutine!'
I could of course dive into the often fought and confusing battle of the difference between a function or a subroutine, but that dead horse has been done over like yesterday's poutine gravy. We could of course take a vote on it but I think it would be this sort of reaction in the community?
self.jpg
Anyway on with my story I did a little digging and was surprise to discover that in perl a subroutine always returns a value. This goes back to day 1 when Larry

Reconsidering Exercise 1

Hao Wu provided a great comment showing how I could solve exercise one using sum from List::Util and grep. I'd considered sum but utilzed false laziness and didn't use it. I'd also considered grep, but did not immediately hit upon the elegant solution that Hao suggested and so went with a more verbose solution.

So, here are some new solutions.

Perl 5:

Showing Hidden Files on OS X and Time Machine

Not Perl related, but I suspect some folks may appreciate this.

Today after a nasty mistake on the command line involving find and rm, I discovered that I deleted a number of files I didn't mean to delete, including some hidden files. Oops! I opened my Time Machine backup, only to discover that it doesn't show hidden files. However, it turns out that you can use that to show hidden files so long as your main system shows hidden files. I'm using OS X Mavericks, so I dropped the following bash script into my bin folder and named it togglehidden. Running this from the command line will toggle showing hidden files in the Finder on or off.

My database involved testing setup

Hi!

Today I'd like to show you my testing setup which involves database testing. Hopefully it can help someone out or maybe someone could suggest me better ways of doing things.

First of all, having all the tests in the root of the 't/' directory got too messy, so I'm using mostly the same directory structure in my 't/' directory, as I have in my 'lib/' directory for the ease of navigation. Let's say this is the 'lib/' tree:

- lib/
-     MyApp.pm
-     MyApp/
-         MyModule.pm

then my 't/' directory would have the following layout:
- t/
-     MyApp/
-         0001-MyApp_first_test.t
-         0002-MyApp_second_test.t
-         MyModule/
-             0001-MyModule_first_test.t
-             0002-MyModule_second_test.t

Because of the nested structure it would be messy to add the 'use lib' statement into the testfiles themselves to use my 'lib/' directory, so I give it as a parameter to prove. I run all my tests from the 't/' directory, so for the ease of use I created a 't/prove'

Here we go loop de loop

Well looks like old fiend smartypants was up to her old tricks again.

You know the type, spends the day at work in some IRC, never forgets the minutia of page 67 of the help file of a sys-admin command, has memorized and and has a better version every regex ever written, knows she is always right and in the bosses eyes can never make a poor choice when designing code.

Anyway let's get on with the code example. Sometimes the ugly way to do
things is the best. In this case processing a large array where lets say the following has to be extracted,

  1. The maximum value
  2. The minimum value
  3. Clean out any duplicates
  4. Group the data into 3 sets

Simple enough really. But then I saw the code (changed to protect the guilty and save space)

Exercise 1. 3s and 5s.

My starting problem is easy. Coming from Project Euler:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Perl 5:

MadMongers Tonight

Timm Murray will be giving a talk on using Perl on a Raspberry Pi to control an Arduino to make drones and robots. 

[From my blog.]

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.