5 ms·
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 mo
by peteretep 15y ago
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 );
- perlgeek 15y agoThen I can highly recommend "Higher Order Perl" by M.J. Dominus (ebook for free on http://hop.perl.plover.com/book/ http://hop.perl.plover.com/book/ but the printed edition is well worth paying for too). It essentially teaches you functional programming in Perl.
- peteretep 15y agoIt'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 http://interglacial.com/hoj/hoj.html
- bsaunder 15y agoI'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.
- bluekeybox 15y agoHa! 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 :)
- wnight 15y agoNot 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.
- chromatic 15y agoFewer 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.
- wnight 15y agoIf 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)
- chromatic 15y agoWhat 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.
- draegtun 15y agoYour 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.