45 points · by todsacerdoti · 5y ago · 60 comments
I still haven't found another language as easy and fast to do text manipulation, parsing, calling out to the shell, regex, whatever. For scripting, Perl is awesome.
Recently I've been using Python instead, simply because I feel like I'm the last person in tech who likes Perl. This stuff is all doable in Python but I still miss my good ol' Perl.
My 2 biggest complaints with Python vs Perl are 1) regex's - the perl syntax is so much faster and easier, and 2) running other command line programs from the shell and reading their output. Perl makes both of those things absurdly quick and easy to do.
With Python, I first have to ensure that the page I'm reading is no more than 2-3 years old, then the copy and paste requires me to re-fix the code, since the syntax is too brittle to stand up to whitespace changes.
That said I do like perl for many things python just isn't as elegant at and your comment about old documentation still staying fresh is pretty true
This comment strikes me as similar - here's a design decision that makes a simple task more complex, and error prone. Not that copying pasting large volumes is a good thing.
I find that I also often have to re-fix the code because so many Python example writers present their examples as REPL sessions, with the code I want to actually copy littered with ">>>" prompts and the output from each individual line's execution.
You can really see Perl's influence in Ruby when it comes to text manipulation, everything you need is likely in the standard library.
If you're not yet fully sold on Python, Ruby might be worth a try. It's like Perl's elegant, sensible cousin.
I think it quickly fell down when it became the way to do dynamic web applications for which it wasn’t really suited. As horrible as php was (then) from a language perspective it’s purpose built nature, made it way easier for early web uses. It did a good job of solving one problem and stayed in its lane.
It’s interesting that it seems, for the most part, that Ruby was mostly relegated to it’s use in Rails. It was probably one of the easiest and most intuitive languages I’ve picked up (I never got into python) and it quickly became my defacto scripting language though now I pretty much just use node since JavaScript is my day to day and I'm so familiar with the ecosystem.
At the time, I thought it made a lot of sense, after using other languages where this feature was standard and required.
However, I've since abandoned its use in Perl, because the traditional Perl way is much more flexible.
I do try to keep my `shift` statements towards the top of a sub, however.
sub PaintHouse {
my $house = shift;
my $color = shift;
if (!$house || !$color) {
WriteLog('PaintHouse: warning: sanity check failed');
return '';
}
print "painting $house with $color paint";
return 1;
}If so, that's not the kind of flexibility I ever want to see in a programming language.
I hardly ever use shift like this, preferring instead the more readable argument copying:
sub PaintHouse {
my ($house, $color) = @_;
... my $house = shift;
my $color = shift;
if (!$color) {
$color = GetDefaultHouseColor();
} sub pretty_print {
my ($filename, $text, $text_width) = @_;
Not as good as if it was just built into the language itself but better then the alternative. #!/usr/bin/perl
use Modern::Perl '2015';
use feature qw(signatures);
no warnings qw(experimental::signatures);
sub foo($bar, $baz) { $bar+$baz }
say foo(40, 2);
See `man feature` for details.Or use CPAN modules: https://metacpan.org/pod/signatures , https://metacpan.org/pod/Function::Parameters
You can pass various arguments to a subroutine like you do in any other programming language and they can be acessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on.
#!/usr/bin/perl
# Function definition
sub Average {
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;
foreach $item (@_) {
$sum += $item;
}
$average = $sum / $n;
print "Average for the given numbers : $average\n";
}
# Function call
Average(10, 20, 30);
reference:
https://www.tutorialspoint.com/perl/perl_subroutines.htmIn reality, function arguments are an abstraction. You push your arguments onto a stack and read them off that stack. It's just that so many modern languages have that abstraction that we forget how it works underneath.
Assembly language doesn't have function arguments. You just decide what to put in each register when you begin the function. So you can remember that eax is the count, ebx is the pointer to the buffer, or whatever. Or if you're a nice programmer you write a comment there.
Perl’s successor language, Raku, has sub arguments (even with lambdas) and optional type constraints.
[1] https://www.effectiveperlprogramming.com/2015/04/use-v5-20-s...
I don't write much Perl these days, I suppose the language will always have a spot in my heart, and whenever I need to quickly whip up a script that is too complicated for my puny shell-foo, I know where to turn to. :)
His quote at the end is ironic: “There are only two kinds of languages: the ones people complain about and the ones nobody uses.” I don’t hear people complain about Perl much. Mostly I see blogs about how people should use it more.
I've used it professionally on and off most of my career. I hate it.
\documentclass[tikz]{standalone}
\newcommand\irregularcircle[2]{% radius, irregularity
\pgfextra {\pgfmathsetmacro\len{(#1)+rand*(#2)}}
+(0:\len pt)
\foreach \a in {10,20,...,350}{
\pgfextra {\pgfmathsetmacro\len{(#1)+rand*(#2)}}
-- +(\a:\len pt)
} -- cycle
}
\begin{document}
\begin{tikzpicture}
\coordinate (c) at (0,0);
\coordinate (d) at (1,2);
\draw[blue,rounded corners=1mm] (c) \irregularcircle{1cm}{1mm};
\draw[red,rounded corners=1mm] (d) \irregularcircle{1cm}{1mm};
\end{tikzpicture}
\end{document}
Unfortunately I ran out of time and still have no idea what the final 3 lines of the irregularcircle command do, or why the \pgfmathsetmacro\len{} is repeated twice.Divide the circle in angles of 10 degrees. Each one will be a node for drawing the line. Then go to 10,20,30,40 degrees etc until reaching 350 degrees and assign a length for the radius in each node
This length will be is the desired radius of the circle plus a small amount of irregularity that is randomly assigned but starting at a value provided as second argument. Finally we need to close the circle with -- cycle. The polygon is defined as a -- b -- cycle, or in this case:
starting_node -- {iteration} -- closing_node. Iteration is a list of values for the radius each ten degrees.
If I'm not wrong, we need to use the macro two times because the first time we use a starting value with angle = 0 and then we iterate over the whole circle. We need to assure that we start and end exactly at the same radius value (the radius provided as first argument) so the polygon line will close correctly. Therefore the first and last node can't have any irregularity.
Especially in the modern era of type safety and type inference, it can be hard to tell what variable "is" just by looking at its name or assignment. Whereas `%names` gives you a clue.
On reflection, my biggest critique of Perl is also its biggest strength: it's just so easy to use regular expressions. It's such an important language primitive, people use regex everywhere.
Unfortunately, even experts miss regex edge cases, and when regex fails, it can fail spectacularly. Then future-you (or worse, someone else) has to parse and fix that massive regex you wrote after drinking two espressos.
In general, clear beats clever. Even if it means your clear, lengthier solution is actually slower.
There's things you can drink that are worse for clarity than espressos...
Of course Perl is more functional than OO so maybe it's time for a comeback now the pendulum has swung back...
Then another breaking factor was, I think, actually the long slow Apache 2 and mod_perl 2 migration back then, leaving Perl in one of the areas where it used to dominate in limbo for a long time as other alternatives like standalone appservers and the like simply were not very common initially, so there was no clear migration path. I still think, this was worse than the Perl 6 confusion.
In the end, we're now in some kind of downward spiral, where Python, Ruby, PHP have MASSIVE advantages in regards to the scope of their package repositories, while CPAN support of modern things, especially interfaces to Web APIs, is permanently half assed and unfinished.
Most people use Moose (or its variants) so they don't have to bless things anymore.
That is a personal preference. I dislike the experience of working in Perl and will not work with it again if possible.
this already confused me. is the $ vigil for scalars? Why is address using it? Isn't address an array here?
Don't get it.