Live data from Hacker News

Half My Life with Perl

perladvent.org

121–130 of 139 posts

Re: Half My Life with Perl

#121

Earlier quoted context omitted.

sub foo { if (whatever) { open my $fh, ...; return $fh; } else { return \*STDOUT; } } will do the job.

Thanks. I feel certain that I tried exactly this a long time ago, and it didn't work as expected, but I may be misremembering. I'll give this a shot the next time I'm in front of a keyboard -- if it works, it will be nothing short of healing.

Slight oddities converting it into a one-liner but here's a copy and paste from a shell session running in my local WSL slice:

  demeisen=; perl -e 'my $in_fh = (defined $ARGV[0] and $ARGV[0] ne "-") ? do { open my $fh, ";'
  HELLO
  Line: HELLO
  demeisen=; perl -e 'my $in_fh = (defined $ARGV[0] and $ARGV[0] ne "-") ? do { open my $fh, ";' -
  HELLO
  Line: HELLO
  demeisen=; echo 'HI' >tmp/hi  
  demeisen=; perl -e 'my $in_fh = (defined $ARGV[0] and $ARGV[0] ne "-") ? do { open my $fh, ";' tmp/hi
  Line: HI
  demeisen=;

Re: Half My Life with Perl

#122
post #38

Tbh, for me it was a source of regex knowledge, in retrospect. Remove regexes from perl, what’s left? I can name: dynamic scoping, implicit filehandles, implicit $_, strange contexts, keyword-like list functions. That’s basically it, and while it sounds fun, it doesn’t do much in a sense of code reduction. $. $? $$ etc, well you can split lines with .split('\n'), get status as a part of result, call os.getpid(). , yo…

>>Remove regexes from perl, what’s left?

qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc.

In fact I've barely scratched the surface.

You must grok Perl more.

Re: Half My Life with Perl

#123
post #122
post #38

Tbh, for me it was a source of regex knowledge, in retrospect. Remove regexes from perl, what’s left? I can name: dynamic scoping, implicit filehandles, implicit $_, strange contexts, keyword-like list functions. That’s basically it, and while it sounds fun, it doesn’t do much in a sense of code reduction. $. $? $$ etc, well you can split lines with .split('\n'), get status as a part of result, call os.getpid(). , yo…

>>Remove regexes from perl, what’s left? qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc. In fact I've barely scratched the surface. You must grok Perl more.

OO is the part of Perl that I grok the least. What about it makes it "far advanced" compared to, say, Java or C++ versions of OO?

Re: Half My Life with Perl

#124
post #122

Earlier quoted context omitted.

>>Remove regexes from perl, what’s left? qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc. In fact I've barely scratched the surface. You must grok Perl more.

OO is the part of Perl that I grok the least. What about it makes it "far advanced" compared to, say, Java or C++ versions of OO?

https://metacpan.org/pod/Moose

Moose is definitely what I go for when I have to use OO with Perl, this more than a decade old and stable for production use cases.

Its based on Class::MOP, which is in turn based on Meta Object Protocol, the same concepts on which CLOS(Common Lisp Object System) is based on. Its always nice to have CLOS goodness in Perl. For eg- https://metacpan.org/dist/Moose/view/lib/Moose/Manual/Method... these methods like before, after, around do fix need for design patterns to a large extent.

But of course the more you explore, the more you discover the possibilities with this.

To a large extent I think Perl brings OO and functional paradigm in a far better package than Python does.

Perhaps HOP needs a new chapter for OO given how few people are aware of this.

Re: Half My Life with Perl

#125
post #83

Earlier quoted context omitted.

The ecosystem of PHP/Ruby/Python is miles ahead so if you need anything ranging from auth to validation, you will have to do 10 times the work. In fact, even modern JS frameworks with features that have no equivalent in Laravel/RoR/Django see their appeal diminished because those old school frameworks do the basic so well they are productivity cheat code for essentials. It's so easy to find people delighting in beaut…

The Mojolicious framework can do a lot of heavy lifting for you. It's not that hard to do interesting things while writing minimal code: https://www.mojolicious.org/

That's actually making my point, this is riduculously limited compared to the tools I mentioned.

Django's admin alone is richer in feature than this whole framework.

Re: Half My Life with Perl

#126

Any good reasons not to build web apps and API backends with Perl these days?

I do. I've worked with other languages over the years but I always come back to Perl. There's a few good options for web apps/APIs.

Web frameworks:

https://www.dancer.pm

https://mojolicious.org

http://catalyst.perl.org

https://metacpan.org/pod/Kelp

https://metacpan.org/pod/Net::API::REST

Middleware:

https://plackperl.org

https://github.com/miyagawa/Starman

Some other useful stuff...

OO Frameworks:

https://zydeco.toby.ink

https://metacpan.org/pod/Moose

https://metacpan.org/pod/Moo

Type system:

https://typetiny.toby.ink

https://metacpan.org/pod/Function::Parameters

https://metacpan.org/pod/Function::Return

Coroutines/multithreading:

https://metacpan.org/dist/Coro/view/Coro/Intro.pod

Async:

https://metacpan.org/pod/Future

https://metacpan.org/pod/Promises

https://metacpan.org/pod/IO::Async

https://metacpan.org/pod/AnyEvent

https://metacpan.org/pod/EV

Re: Half My Life with Perl

#127
post #122
post #38

Tbh, for me it was a source of regex knowledge, in retrospect. Remove regexes from perl, what’s left? I can name: dynamic scoping, implicit filehandles, implicit $_, strange contexts, keyword-like list functions. That’s basically it, and while it sounds fun, it doesn’t do much in a sense of code reduction. $. $? $$ etc, well you can split lines with .split('\n'), get status as a part of result, call os.getpid(). , yo…

>>Remove regexes from perl, what’s left? qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc. In fact I've barely scratched the surface. You must grok Perl more.

Won’t nitpick, how much of what’s useful here is unique to perl? You could make a similar list for most languages and barely scratch the surface.

top class unicode handling

This one I remember struggling with, afair due to io vs perlio layers impedance mismatch and use utf8. Not sure about now, but perl was anything but unicode for the clueless.

Re: Half My Life with Perl

#128
post #40

Earlier quoted context omitted.

My experience with Perl is that often the batteries are rotting. Not a strong opinion though, i haven’t been writing perl in a while.

The analogy is quite the opposite here. Python was always the "rotting batteries" for me.... write something, some migration script, run it, works, forget. A few years later, new servers, hey, guess what, python 2.6 script has problems running on python 2.7... luckily a small fix. A fw years go by... new servers,.. hey, python 2.7 is not installed by default anymore, and pip has issues with dual versions... fix that.…

I never mentioned python in my comment, though

Re: Half My Life with Perl

#129
post #122
post #38

Tbh, for me it was a source of regex knowledge, in retrospect. Remove regexes from perl, what’s left? I can name: dynamic scoping, implicit filehandles, implicit $_, strange contexts, keyword-like list functions. That’s basically it, and while it sounds fun, it doesn’t do much in a sense of code reduction. $. $? $$ etc, well you can split lines with .split('\n'), get status as a part of result, call os.getpid(). , yo…

>>Remove regexes from perl, what’s left? qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc. In fact I've barely scratched the surface. You must grok Perl more.

given/when are deprecated and will be removed in a future release.

References are a PITA - cumbersome to use, and they make code less readable.

Perl doesn't have first class functions (you can pass or return functions via references, but that's cumbersome and less readable compared to languages with better FP support).

Re: Half My Life with Perl

#130
post #122

Earlier quoted context omitted.

>>Remove regexes from perl, what’s left? qw, qr, references, multiline regexes, a far advanced OO ecosystem, Data::Dumper, map, grep, pack/unpack, DBM, ``, top class unicode handling, given/when, functional programming etc etc. In fact I've barely scratched the surface. You must grok Perl more.

OO is the part of Perl that I grok the least. What about it makes it "far advanced" compared to, say, Java or C++ versions of OO?

OO in Perl is lower level compared to Java or C++, so e.g. instead of having a class construct, you have to simulate a class - typically by binding (blessing) a data structure to a namespace. Then any functions in the namespace become methods that can be called via the data structure (object).

In practice, this has turned out to be a mixed blessing because of how tedious it is to do this repeatedly. So over the years there have been many libraries created to make this easier, each with different features.

There's currently work underway to modernise Perl's built in OO to address these problems.

On the other hand, this makes some things easier, e.g. Design by Contract can be added to Perl just by writing a library.

Another example, adding traits to PHP required updating PHP itself, whereas in Perl there are libraries to do that.

Post reply on HN