I translate "Modern Perl Writing Style" to English

I translate "Modern Perl Writing Style" to English

If you learn modern perl writing style, please see this topic. you can learn it quickly.

Modern Perl Writing Style

12 Comments

Thanks for translating this into English. Can I offer a few thoughts?

"Do error handling in file open (Required)" You also might want to mention the autodie module which makes open errors into hard errors. Honestly this behavior for open is a bit odd and doesn't feel so modern (at least to me). Using return values to indicate error state feels like a 1990s holdover. 'autodie' does have some indicated performance issues, so I recommend importing only the methods you need 'use autodie qw(open close);'

"Don't use prototype(Strongly recommended)" I would say don't use this needlessly. Generally I only reach for it when I am doing domain specific language stuff. For regular methods I never touch it.

"Using eval to trap exceptions." The example code you gave:

eval { func() };

if ($@) {
# Write process when error occur
}

Is probably not the recommended approach anymore due to the global nature of $@ (and other issues). Many people use Try::Tiny which gives you a nice syntax while encapsulating most of the sharp edges but in a pinch if you don't want Try::Tiny (there's a performance time penalty to it compared to plain old eval) I would recommend the following idiom instead:

eval {
## some stuff;
## more stuff;
1 } or do {
## handle the exception and $@ as needed;
};

This works by taking advantage of the side effect where eval returns true unless there's an exception thrown prior to executing '1'. In this case you can be sure there's really an error and not just $@ being globally set from some other error totally unrelated. Its not a total solution like Try::Tiny but it does have much less edge cases then the old recommended approach.

Thanks again for sharing!

jnap

jnap, one important note about autodie: I wouldn't recommend using any version before v2.12, released in June 2012, because of a serious bug that prohibits its use with the open pragma, the PERL_UNICODE environment variable, and various CPAN modules including utf8::all. Perl v5.18 is the first release to bundle a safe version of autodie: v2.13. Other than that, I think autodie is a great pragma. Since it was only fixed so recently though, especially in core Perl, this pitfall is worth mentioning.

On that note, the open pragma is an important component of modern Perl. :)

> As we know, $@ have trap, but Try::Tiny also have other trap

Agree.

However that example is indeed incorrect:

eval { func() };

if ($@) {
# Write process when error occur
}

You should add a section on using Perl::Critic and its script, perlcritic. This will tell you everything about a Perl script or module that is not compatible with PBP.

If you use the three argument open, you can use an array for the command of a pipe. This will bypass the shell interpretation of the command:

open my $fh, '-|', @cmd or die "could not pipe from `@cmd`: $OS_ERROR\n";

> I want you to understand that it is difficult for beginners to use CPAN module . $@ work well in most place.

I would never ever recommend anyone, *especially* beginners, to use $@ directly. Once that habit is formed, it's difficult to break. (There are times when it is appropriate to look at $@, but you have to understand all the ramifications, so it's not appropriate for beginners.)

Yuki, no doubt 'autodie' has downsides, both as you and as Nick pointed out. I guess I intended my comment to be more about mentioning it and showing an example, rather than using it to replace the classic syntax. I definitely would never mean to say to not teach how it works out of the box!

Same goes with Try::Tiny... I know there's downsides. If I think about it a bit more, if I was teaching this I'd point out the classic example:

eval { ... }; if($@) { ... }

and mention that you will probably see this in a lot of code and a lot of examples, but that it is preferred nowadays to do:

eval { ...; 1 } or do { ... }

Because it fixes more of the types of edge case problems people run into with the classic syntax, mostly around the globalness of $@ and related things. And then I would mention Try::Tiny as the current best solution accepted broadly in the community IF cpan is possible and IF some of its performance issues are not a problem (the check is not in a very hot part of the codebase, for example).

And again, if I was teaching it, I'd probably be tempted to mention another CPAN module that is quite intriguing to me, in that it is a sane attempt to graft LISP style conditions into Perl, "Worlogog::Incident"(https://metacpan.org/module/Worlogog::Incident) which I think are a much better way to deal with exceptions. That modules description doesn't really explain the LISP approach, but I highly recommend looking at: http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html which is a great explain of it, although I can't find a native Japanese version to share with you, quite sorry :(

Thanks!

Jnap

> There are times when it is appropriate to look at $@, but you have to understand all the ramifications, so it's not appropriate for beginners

There's a blog post, right there.

Leave a comment

About Yuki Kimoto

user-pic I am Yuki Kimoto, Japanese Perl programmer. I create tutorial site in Japanese and English. I create Perl module and upload it to CPAN. I create Web application in Github.