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

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.]

Netting apples against possums

I officially join Fruit Protectors R Us:

The pix of the tricks.

Another Little Head Scratcher

So I though it would be just a regular day until with me just doing a little coding, drinking my coffee* and generally enjoying life, But I was wrong.

A Little Vulnerable

So I have been babysitting and ever so slowly migrating a 15+ year old application over to a more manageable and of course we have to keep doing improvements to the code so on a very old part I found something like this. (SQL changed to protect the innocent)

my $usr_ids = join(",",@user_sel);

my $sth = $h->prepare("Select * from a_table where id in ($usr_ids)");

$sth->execute();

Params? We an't got no params. We don't need no stinking params!!

Well the old bug-bear of little Bobby Tables or SQL injection shows it ugly little head again.

But what to do? There is no bind_array, well lets just give 'execute_array' a try

my $sth = $h->prepare("Select * from a_table where id in (?)");
my $tuples = $sth->execute_array(
{ ArrayTupleStatus => \my @tuple_status },
@user_sel,
);

ora_st_execute_array(): SELECT statement not supported for array operation.

Ouch so no luck there!

Release a new distribution every month in 2014

This challenge is a stencil quest on questhub: in each calendar month of 2014 you have to release a distribution that you haven't released before, and write a blog post about it. This might be an entirely new distribution, one that you've adopted, or one that you're helping with. The rules so far are:

  • You have to release at least one such dist within each calendar month. You can't catch up, by releasing 12 in December.
  • You must blog about the dist and link to the blog post in a comment on your quest. It doesn't matter if the blog post is in a following month.
  • Renaming one of your existing dists doesn't count :-)

Come and join us!

Perl Exercism

I blame Jeff Thalhammer. He is the one who directed me to The ChangeLog, and that's where I've heard the interview with Katrina Owen, the creator of Exercism.io.

What is Exercism.io?

Perl and Me, Part 1: The Road So Far

Lately, I’ve been pondering my relationship with Perl.  Every programmer has a different relationship with their language-of-choice, and it impacts how they approach many different aspects of coding, what features they like and which ones they hate, what they clamor for and what they disparage.  And these choices are always going to be challenged by our friends, our coworkers, and our open-source collaborators.  Which is a good thing, for the most part: being challenged is what keeps us honest, keeps us fresh.  Without constant challenge, we can lapse into dogma and cargo cult coding.  But we can’t properly defend our positions—can’t explain why we feel the way we do about certain things—unless we take the time to understand our relationship with our language.

So this is part 1 of an X-part series (we’ll just have to see how long it takes me to work through it all).  Primarily I want to explore this for my own self-awareness, but I hope it will be interesting to others as well.  And perhaps it will make some of my other blog posts make sense.

I begin at the beginning: how I came to Perl.

Does This Mean Me??


In an odd turn of fate as I was just about to write today's post. I got this email announcing the release of DBI 1.6.31, normally this causes me little concern as DBI has been stable for a very very long time, but given yesterdays Part 18 and this line in the change file

Changed the handle passed to Callbacks from being an 'inner' handle to being an 'outer' handle.

I though maybe I should look at this change?

Are You an Innie or Outie

I'll start by giving the 25 cent tour of inner and outer handles. DBI has been using Tied Hashes since time immemorial, and all handles in DBI are references to tied hashes so you never really get a $dbh or $sth object just a reference to it.

Purpose of this blog

At this point, I don't have a Perl blog. I do Perl things, but nothing that ever seems worth blogging about. However, I've decided to improve my knowledge of Perl 5i and Perl 6, and generally practice my proramming, so I'm going to work through a bunch of easy through to difficult problems in each language. Hopefully I'll pick up the idiomatic solution ideas as I go along.

Feedback and alternate solutions are also welcome.

At some point I may repeat the problems with Python, but not yet.

Problem sets I'm planning on starting with include:

The latter two are language-targetted, but I believe that I should be able to gain some benefit from some of them anyway. Suggestions for other problem sets are welcome, but this is certainly enough to get me started.

Visualizing a CPAN install plan

At the patch -p1 Perl hackathon, I'm working on bits related to my idea for a new sort of CPAN client. One of the features I want is a graphical display of the work to do and the work being done.

Creating the data structure in Perl is easy. Start with a module, figure out it's distribution, then grab that META.{json|yml} to get dependencies (for now, with more discovery later). Put that stuff into a data structure.

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.