I love Perl's personality, its quirkiness, and its massive amount of libraries. Perl was _massively_ influential on me as a young programmer. But I'm pretty sure that I won't be writing any Perl ever again.
I love Perl's personality, its quirkiness, and its massive amount of libraries. Perl was _massively_ influential on me as a young programmer. But I'm pretty sure that I won't be writing any Perl ever again.
I don't really think that the people who left perl all went to PHP.
Now if only there was a "Ruby lite" that ran more like Perl 5 (skipping the GC for reference counting and a few other performance shortcuts)
If ruby had OO as good as Moose, I'd seriously consider switching. Sadly the projects to port Moose to ruby never seem to get finished.
Most of my object attributes look like one of -
has foo => (is => 'ro', required => 1);
has bar => (is => 'lazy', builder => sub { Some::Logic::here() });
which translates through to, in old-school perl style, sub new {
my ($class, $args) = @_;
my $new = bless({}, $class);
$new->_init($args);
return $new;
}
sub _init {
my ($new, $args) = @_;
die "foo is required" unless exists $args->{foo};
$new->{foo} = $args->{foo};
if (exists $args->{bar}) {
$new->{bar} = $args->{bar};
}
}
sub foo { $_[0]->{foo} }
sub bar {
my ($self) = @_;
unless (exists $self->{bar}) {
$self->{bar} = $self->_build_bar;
}
return $self->{bar}
}
sub _build_bar {
Some::Logic::here();
}
So far as I'm aware, ruby provides 'attr_reader' which would generate the 'foo' method for me, and of course the above 'new' is built-in.However, again so far as I'm aware, I'd still have to write the bar() lazy/build accessor, and an initialize() method to do the equivalent of '_init' - and name _build_bar by hand, which is less of a big deal but turns out to be really quite nice.
Plus I'm unaware of any ruby equivalent for the Class::Method::Modifiers CPAN module, although I'd imagine that's pretty easy to write and wouldn't be surprised if there's a gem somewhere that I've missed.
In fact, there's a MooseX gem that brings some of this syntax to ruby already, which suggests that it isn't already there anywhere else or at least that that gem's author didn't find it either; next time I play with ruby I intend to do so with the assistance of said gem and maybe that'll solve my problem, but I'm open to being told I'm doing it completely wrong - this isn't me harshing on ruby so much as complaining that an otherwise rather pretty language isn't something I can enjoy writing and I'd like to fix that.
> I'd still have to write the bar() lazy/build accessor,
Yes, in Plain Ruby
> and an initialize() method to do the equivalent of '_init' - and name _build_bar by hand,
There's a 'trick' involving Struct that some people use to handle this. If you asked Rubyists, they'd probably say 50/50: http://blog.steveklabnik.com/posts/2012-09-01-random-ruby-tr...
> I'm unaware of any ruby equivalent for the Class::Method::Modifiers CPAN module,
Rails offers this in controllers as {before,after,around}_action, and actions are just methods. You'd have to write it yourself, though, you're right, or use a gem.
> I'm open to being told I'm doing it completely wrong
Naw, though I will say that I very rarely need to do things like this, or when I do, the boilerplate doesn't bother me enough to justify the metaprogramming required to do it in a mega generic way. :)
Plus it misses out the whole "I can have required and optional parameters and it just works" part of what I was showing you.
> Naw, though I will say that I very rarely need to do things like this, or when I do, the boilerplate doesn't bother me enough to justify the metaprogramming required to do it in a mega generic way.
Right. Thing is ... you know how people often say "I didn't really find perl that kludgy until I used ruby for a while, and then going back to perl was heinous"? After a couple of years of using Moose, I found going back to anything that couldn't do all of that easily to be heinous.
The last time somebody I knew who writes both perl and ruby tried to port some of my perl code to ruby, they ended up with 3x or 4x as much code per class because I lean more heavily on this stuff than you'd think if you weren't already using it, and got bored about three classes in and decided to wait for somebody to port Moo/Moose to ruby before trying again.
Which is a shame, because I was really hoping they'd finish, stare at the boilerplate, and then find a more rubyish way of writing the whole thing that made it not have boilerplate ... but the fun ran out before any of it ran.
The thing is, my gut feeling is that rubyists probably design their classes -just- differently enough to me that they don't need what they don't have, but I can't figure out how to devise an experiment to figure that out.
If you come up with such an idea, I'd love to hear it, since some sort of comparison that shows how the idioms fall out differently in the two languages would be really nice, but I can't think of one that wouldn't be either trivial or more work than its worth.
Thanks for responding; it's really quite pleasant to have this sort of conversation without getting derailed into language wars :)
Absolutely. :)
> I can't think of one that wouldn't be either trivial or more work than its worth.
This is the hardest part about finding examples.
> Thanks for responding; it's really quite pleasant to have this sort of conversation without getting derailed into language wars :)
Agreed. Thank _you_!