Какова полноценность ПРОВЕРКИ, UNITCHECK и блоков INIT в Perl?

JavaScript является однопоточным и не может получить доступ к файловой системе, таким образом, я не думаю, что необходимо волноваться о тех. Я не уверен, существует ли способ установить тайм-аут для принятия мер против бесконечных циклов, но Вы могли бы всегда порождать (Java) поток, который выполняет сценарий, и затем уничтожьте поток после такого большого количества времени.

14
задан DVK 4 November 2009 в 14:34
поделиться

3 ответа

I had a package import function which would do some heavy duty processing and then make an eval call. How do you debug something like that? The import function gets run when you use the module, which takes place at compile time (like it was inside a BEGIN block). For some reason (I think it was because I needed to pass parameters to import with heredoc notation, but it could have been something else), it wasn't good enough to say require Module; Module->import(@args).

So my workaround was to build the string for eval in import, and save it another variable. Then I ran the eval in an INIT block. When you ran the debugger, the very first execution point was at the start of the INIT block and I could use the debugger to step through the eval statement.

5
ответ дан 1 December 2019 в 13:59
поделиться

An interesting use of CHECK blocks is in "Advanced Perl programming" by Simon Cozens (O'Reilly) in Chapter 1, in "Doing things later with CHECK" section. He shows how to implement "final" java-like attribute

Also, Devel::Sub::Trace uses INIT blocks to incert traces (this info is from POD for Devel::Hook which is a module used for working with those named blocks)

7
ответ дан 1 December 2019 в 13:59
поделиться

Well BEGIN blocks are run at compile time, as you know. So I keep it to code that needs to be run in order for my module to be imported.

I wrote a script wrapper, to do everything that was being done in boilerplate code that occurred in a couple hundred scripts.

  • There were things I had to do to get the module reading to be use-d. That I ran in BEGIN blocks and import sub.
  • But there was also all that boilerplate that initialized the services the script would use. Therefore, I ran these actions in the INIT blocks.
  • And ran necessary cleanup and exit code in the END blocks.

I think CHECK makes sense if you write modules with XS engines, but I have only used it a handful of times. One time I think it was to check out the suggestions in Intermediate Perl. And I can't offhand remember the other reasons.

But I use INIT blocks when I feel that code is more part of the script, than setting up the module. In essence, I only do what is necessary during compile time.

3
ответ дан 1 December 2019 в 13:59
поделиться
Другие вопросы по тегам:

Похожие вопросы: