Hacker News new | past | comments | ask | show | jobs | submit login

Learning Javascript changed my Perl for the ... more fun to write. Possibly better, too. I've started passing around coderefs as arguments to functions a lot more recently, and allowing people who use my APIs to do just that...

I've been writing code that allows you to describe a website in data and at a high level recently, and builds you up all the methods you need to interact with it. In general, when people need to provide parameters, I also let them pass in coderefs that can be executed to provide those eg:

            my $name = ref($args{'form_name'}) eq 'CODE' ?
                $args{'form_name'}->( $self, @user_options ) :
                $args{'form_name'};
            my $form = $self->mech->form_name( $name );



Then I can highly recommend "Higher Order Perl" by M.J. Dominus (ebook for free on http://hop.perl.plover.com/book/ but the printed edition is well worth paying for too).

It essentially teaches you functional programming in Perl.


It's a great book! Here's something really cool: ultra-linguist and general bad-ass Sean Burke went through it doing all the examples in ... Javascript!

http://interglacial.com/hoj/hoj.html


Ha! I thought I was the only one... When I first learned Perl two years ago, I wrote my programs in a very boring, imperative C-like style; in the time that passed, I exposed myself to the functional side of Python while also learning some deep Javascript; last month after having come back to Perl, I noticed that the new code I am cranking out basically looks like Lisp with some curly brackets in-between.

God, do I hope that the programmer that comes after me is as excited about the code-as-data, data-as-code paradigm as I am... Or at least can understand anything I wrote :)


I've explored similar concepts. In particular I've found it rather powerful to be able to export/import code refs with:

use YAML::Syck; $YAML::Syck::UseCode = 1;

Watch out though, when you take executable code from a user.


Not to golf your code or anything, but that's around the point where I switched to Ruby. It's almost a drop-in replacement, you can be coding in minutes with all the handy shortcuts non-PERL coders don't know they're missing.

name = args[:form_name].respond_to?(:call) ? args[:form_name].call(user_options) : args[:form_name]

Fewer sigils, no array/scalar access nonsense, real and convenient objects, etc.


Fewer sigils

As with PHP, reusing the same sigil for everything misses the point of sigils rather badly. I count five instances of two sigils in your example and each sigil character has another syntactic meaning within the same line.


If sigil means non-ascii character, there are six. If you use it to mean name-modifier (changes what the following string means) there is one, the colon. What did you mean?

And yes, colon is also the or in the ternary operator.

But there are still less than Perl, for the same length of code, and the code is simpler - .call() the thing if it.respond_to?(:call)


What did you mean?

A punctuation character considered as part of an identifier by way of a parser rule. The ? in respond_to?, in this case.


Your example isn't exactly identical because its not making use the $self object. Your example would look more like this in Perl:

my $name = ref $args{form_name} eq 'CODE' ? $args{form_name}->(@user_options) : $args{form_name};

Taking the original Perl example I would probably write it like this:

my $name = do { my $c = $args{form_name}; ref $c eq 'CODE' ? $self->$c(@user_options) : $c };

Which seems DRYer to me.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: