August 2010 Archives

Perlbuzz news roundup for 2010-08-27

| No Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Run PHP tests in your Perl test suite

| 5 Comments

Sometimes you've got a big codebase that isn't just Perl. Maybe you've got PHP mixed in with it, and you want to test the PHP along with all the Perl code, too. Perl's prove program doesn't care if the testing results it parses are from Perl, PHP or even static files, so long as they're in the TAP format. However, actually getting prove to run those PHP programs takes a little doing. Fortunately, Test::Harness 3.xx has hooks for source handlers.

David Wheeler has written about running PostgreSQL tests under prove in his blog and I stole from his code shamelessly to create TAP::Parser::SourceHandler::PHP, released to the CPAN this morning.

So now, to run the Perl .t files and the PHP .phpt files all in one swell foop, here's what we do at work:

prove -r \
    -I/home/alester/proj/Lib \
    --source=Perl --ext=.t \
    --source=PHP \
    --ext=.phpt \
    --php-option=include_path=/home/alester/proj/Class \
    --php-option=extension=.phpt

That --source=PHP tells prove to load up TAP::Parser::SourceHandler::PHP. The --php-option tells prove to pass those options through to the SourceHandler. If we had PostgreSQL tests or MySQL tests, we could use the SourceHandlers for those that David Wheeler has written as well.

Now we can test everything all in one run, and we get all the benefits of Test::Harness 3.xx, like parallel tests and TAP archiving and so on.

Vim 7.3 supports Perl 6, adds Perl 5 improvements

| 1 Comment

Vim 7.3 has just been released today. From the announcement:

This is a minor release of Vim. It consists of Vim 7.2 plus all patches, updated runtime files and some more, see below. It has been two years since the 7.2 release, thus it's not that "minor". But not "major" either. Something in between, don't know how to call that.

The most notable additions since 7.2:

  • Persistent undo and undo for reload
  • Blowfish encryption, encryption of the swap file
  • Conceal text
  • Lua interface
  • Python 3 interface

However, to the Perl programmer, this is a significant upgrade from 7.2, because of updates of all the Perl-related support files. Vim has language specific plugins for syntax highlighting, indenting and other filetype-related behaviors. Every one of these Perl-related support files has been updated. For example, syntax/perl.vim has not been updated since 2006. Now, Perl 5.10 keywords like given, when and state are properly highlighted.

For the Perl 6 hackers out there working with Rakudo Star, Vim now includes support for Perl 6. Until now, if Perl 6 programmers wanted Vim to support Perl 6, they had to use a perl6.vim file that got passed around from person to person. Now, it comes installed automatically as part of Vim 7.3.

Perl 6 detection is primitive, so you may have to explicitly add it to your modeline in your file, such as

# vi: filetype=perl6:

All of these changes are from the vim-perl project hosted on Github. In that project, I've aggregated syntax, indent and filetype plugins for the Perl and Perl 6 support files that get fed back to the Vim project. It's also got support for other filetypes like Template Toolkit that are not part of the vim distribution. I'm not making many changes to the Vim code directly. Like many of my other contributions to open source, my role is one of wrangler and coordinator and less of programmer and technologist.

If you're interested in Vim support for Perl 5 and Perl 6, I encourage you to check out the vim-perl project and join the vim-perl mailing list. Now that Vim 7.3 is out, we have some room to stretch out and make Vim do incredible things for Perl in the next release.

What's on your to-do list?

| 3 Comments

What's on your to-do list for Perl projects these days? Here's mine.

  • Test::Harness
    • Testing PHP code with Perl's prove program. I wrote code to do this under Test::Harness 2.x, but 3.x has an entirely different way to do it.
  • vim-perl
    • Just gave Bram the latest files for inclusion in vim 7.3
    • I want to have a test suite and a packaging system and to distribute it on CPAN
    • Write an FAQ and intro to let people know about cool tricks like "set equalprg=perltidy"
  • Rakudo Perl 6
    • Learning more Perl 6
    • Want to make perl101.org have parallel examples for Perl 5 and Perl 6.
  • Parrot patches
    • Fixing dependency problems in the Makefile
    • Checking regularly for errors that GCC's super-strict and persnickity warning settings turn up.
  • Updating links and articles on http://bobby-tables.com
    • I want bobby to be the canonical source to turn to help newbies know how to avoid SQL injection
  • Perlbuzz
    • Review of Effective Perl Programming
    • Aggregate information about Plack
    • Create a sitemap for Google's benefit
  • WWW::Mechanize
    • Making a ->text() method that caches its results, so that I can then...
    • Make Test::WWW::Mechanize's ->text_contains() method use it and not recalculate the same page contents over and over

Perl is not an acronym

| 5 Comments

A reminder to those out there, especially HR folks, that the language Perl is always spelled "Perl" and never "PERL." From the Perl FAQ:

Before the first edition of Programming perl, people commonly referred to the language as "perl", and its name appeared that way in the title because it referred to the interpreter. In the book, Randal Schwartz capitalised the language's name to make it stand out better when typeset. This convention was adopted by the community, and the second edition became Programming Perl, using the capitalized version of the name to refer to the language.

You may or may not choose to follow this usage. For example, parallelism means "awk and perl" and "Python and Perl" look good, while "awk and Perl" and "Python and perl" do not. But never write "PERL", because perl is not an acronym, apocryphal folklore and post-facto expansions notwithstanding.

Those post-facto expansions are "Practical Expansion and Reporting Language" and "Pathologically Eclectic Rubbish Lister." Both are bacryonms, created after the language was named.

I'm glad to hear "Rakudo is slow!"

| 4 Comments

I'm pretty tired of the sniping and punditry about the recent release of Rakudo Star. However, David Golden has put together a great article about what the Rakudo Star release means both to the team and to the public.

Key points from his article:

  • Rakudo Star is the first prototype of an end-user distribution tarball
  • There is no way reactions to Rakudo Star can possibly live up to the hopes and dreams of those involved the project (and I say that it can't live up to the expectations of those outside the project, either)
  • Rakudo Star still is a significant step forward for Perl 6
  • If anyone was waiting for Perl 6 to rescue Perl, then they'll need to keep waiting.

And then, down in the comments, Moritz Lenz points out something I'd overlooked:

Yes, people now say "Rakudo is slow as $funny_metaphor", but that's much better than "Rakudo is vapourware". Once we speed up Rakudo, we can simply post benchmarks and say "look, it's now $n times faster than before". "Rakudo is slow" implies "Rakudo is", and that's a big step forward.

Thanks for the reminder, Moritz. Next it will be "Rakudo is faster, but not fast enough." And then maybe "Rakudo doesn't have enough documentation," and then "Rakudo doesn't have all the modules it should." All of it is progress.

Perlbuzz news roundup for 2010-08-09

| No Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

What to respond to "Perl 6 isn't Perl any more"

| 23 Comments

Now that Rakudo Star is out, and people are able to easily install and work with an early implementation of Perl 6, the pundits and cranks have to put aside their tired Duke Nukem jokes and talk about how different Perl 6 is from Perl 5. They gripe that everything is different and scary, "it shouldn't be called Perl any more." I'm tired of it, and it makes no sense.

I bet none of those cranks remember Perl 4 and the shift to Perl 5.

  • Perl 4 didn't have lexical (my) variables
  • and there were no scalar filehandles
  • and you couldn't pass filehandles as parameters to functions except with typeglobs
  • and the package separator was ', not ::
  • and really nobody used packages anyway
  • and there was no object support whatsoever
  • and that meant no modules to speak of
  • and you couldn't pass around regexes as scalars (qr// operator)
  • and on and on.

Even with all those differences, we survived. In fact, we thrived.

The Pink Camel, first edition of Programming Perl, covering Perl 4, was only 450 small pages long, and a third of that was a section called "Real Perl Programs." (Imagine! Actual programs!) The Blue Camel, the 2nd edition, covering Perl 5, was over 600 bigger pages.

You know what I thought when I got my copy of the Blue Camel? It wasn't "Boy, this sure isn't Perl any more." No, I thought "Holy shit, look at all the stuff I can do." I couldn't even read the book straight through, because I kept skipping around, my mind amazed at the possibilities in front of me.

There are those who will read this and say "Yeah, but Perl 5 could still pretty much run any Perl 4 program, but Perl 6 won't be able to run Perl 5." And that's true. And it's irrelevant.

Perl 6 is still Perl, and is still called Perl, for many reasons, but only one that matters.

Larry Wall says that Perl 6 is still Perl.

Larry has his reasons. Some he's mentioned in past State of the Onion addresses. Maybe you don't agree with his reasons, or his decisions. But it doesn't matter one damn bit what you think. It's his decision. All arguments are a waste of time and brain cycles.

So when someone says "Perl 6 should have been named something else," I suggest a response of "OK, whatever you say. Now, isn't it cool that you can use list reduction to say my $sum = [+] @list;?"

Perl 6 has data dumping built in

| 3 Comments

Any Perl 5 programmer who's worked with Perl 5 more than a few months has learned about how invaluable the Data::Dumper module is. The ability to say

use Data::Dumper;
print Dumper( \%hash );

is a godsend to debugging data structures of any complexity.

Perl 6 has this dumping built in.

uniqua:~/rakudo/lab $ cat dumper
#!/usr/local/bin/perl6

use v6;

my %hash = (
    'this'     => 'that',
    'year'     => 2112,
    'matcher'  => regex { ^ M(r|rs|s)\. \s+ (\w+) \s+ Wall $ },
    'rational' => 0.5,
    'num'      => (0.5).Num,
);

say %hash.perl;
# Or %hash.perl.say

uniqua:~/rakudo/lab $ ./dumper
{"num" => 0.5, "this" => "that", "year" => 2112,
"rational" => 1/2, "matcher" => { ... }}

Regex dumping does not display the actual regex yet, but Patrick Michaud says it's coming soon.

Diving into Perl 6

| 7 Comments

I'm officially diving into Perl 6.

I wrote my first real Perl 6 from scratch today. It's a prime number tester, where we test both via checking factors iteratively, and also using the magic prime number tester regular expression. I was inspired by a blog post about how PHP can't always handle regexes with lots of backtracking, and I thought it would make a good stress for Perl 6.

My program as it stands now looks like this:

#!/usr/local/bin/perl6

use v6;

# Perl 5 @ARGV is now @*ARGS
my Int @candidates;
if ( @*ARGS ) {
    for @*ARGS -> $n {
        push @candidates, $n.Int;
    }
}
else {
    @candidates = 2 .. 100;
}

for @candidates -> Int $x {
    my $via_factors = is_prime_via_factors($x);
    my $via_regex   = is_prime_via_regex($x);

    if $via_factors && $via_regex {
        say "$x is prime";
    }
    elsif $via_factors xor $via_regex {
        say "Difference in opinion on $x :",
            "factors says $via_factors, regex says $via_regex";
    }
}


sub is_prime_via_factors( Int $n ) returns Bool {
    my $top = sqrt $n;

    for 2..$top -> $i {
        return False if $n %% $i;
        # %% is the divisible-by operator
    }
    # Could also use:  $n %% none(2 .. $top)

    return True;
}


sub is_prime_via_regex( Int $n ) returns Bool {
    my $str = 'x' x $n;

    # First capture is $0, not $1 or \1 as in Perl 5
    return False if $str ~~ regex { ^ x $ | ^ (xx+?) $0+ $ };

    return True;
}

I'm sure there are ways that are more Perl 6ish to do what I've done above, but I was glad to learn along the way. Big thanks to the #perl6 IRC channel for help. Some important lessons:

  • Command-line args are in @*ARGS, not @ARGV.
  • The old chestnut ($x mod $y == 0) is now ($x %% $y) with the %% divisible-by operator.
  • The first capture group in a regex is $0, not $1 or \1 as in Perl 5.

There's a document about the differences between Perl 5 and Perl 6 which I updated based on tonight's fun, and will keep adding to along the way.

My goal is to have all of perl101.org include examples in both Perl 5 and Perl 6, both for people new to Perl and for those moving to Perl 6. If you'd like to help in this effort, let me know or work on the perl101 github project.

« July 2010 | Main Index | Archives | September 2010 »