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.
I translate "Modern Perl Writing Style" to English
If you learn modern perl writing style, please see this topic. you can learn it quickly.
I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.
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 theopen
pragma, thePERL_UNICODE
environment variable, and various CPAN modules includingutf8::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.:)
I'm not satisfied by autodie.
1. autodie changed function behavior. so people have to read the top of script "use autodie". open function is widely used. I don't like change the open function behavior.
2. autodie only have effect some function. For example, File::Copy::copy and File::Copy::move don't throw exception.
> use Try::Tiny
I want you to understand that it is difficult for beginners to use CPAN module . $@ work well in most place. As we know, $@ have trap, but Try::Tiny also have other trap. I like $@ rather than Try::Tiny because $@ is default error handling system in Perl and work well in most place.
> 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:
> 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.
+1 - do tell!
>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.
I like PBP. but I don't like some of these. In my opinion, PBP is a little tight compared with current Perl trend.
And PBP is too long for beginners to read when they start Perl programming. I wanted to provide short topic for beginners.
>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:
In my opinion, pipe open is a little advanced topic. I don't contain topic for beginners.
>I would never ever recommend anyone, *especially* beginners, to use $@ directly.
$@ is not bad. I always use $@, but I never have big troubles. People likely to deny $@, but $@ is not bad.
>eval { ... }; if($@) { ... }
This is correct syntax.
eval {
...
};
if ($@) {
}
If eval is nested , $@ is maybe cleared, but you can save $@ to lexical variable when $@ is overwritten. This is edge case. Usually, $@ work well.
Try::Tiny big problem is that we use return function transparently.
I will recommend try { } catch { } syntax if this syntax is added to Perl core.