简体   繁体   中英

Can a Perl BEGIN block spread a virus or lose data?

I am still new to Perl. Since BEGIN blocks are run during compilation can't a virus spread or data loss occur from simply compiling? Does Perl do anything to stop it? If so does it mean the code in BEGIN blocks may act differently outside of it?

Yes to all these questions. The Eclipse IDE was vulnerable to this. It discussed in more detail here .

As with all software, you should avoid downloading and running anything from a source you do not trust. CPAN is generally trustworthy; although I am not aware of anyone intentionally releasing rogue code to CPAN, it's possible it has happened.

You can avoid running code during compile checks with the $^C flag , eg:

BEGIN { load_data_from_db() unless $^C; }

chromatic解释了Perl程序的工作原理

Note that sometimes this is a feature. BEGIN blocks inside mod_perl modules are executed only once, when they are first loaded. So you have a simple syntax to do page-level initialization in the same script, and place it "near" the code that it assists.

Occasionally it's similarly useful for writing complicated initialization code that you don't want to put at the top of a script.

But mostly it's just there for thematic compatibility with awk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM