Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 523 forks source link

CHECK and INIT parsed at runtime ignored #1324

Closed p5pRT closed 20 years ago

p5pRT commented 24 years ago

Migrated from rt.perl.org#2349 (status was 'resolved')

Searchable as RT2349$

p5pRT commented 24 years ago

From @ysth

Created by @ysth

I think CHECK and INIT blocks should trigger a warning if they are not going to be executed.

[1]D​:/​: cat bcie.pl BEGIN { print "in begin\n"; } CHECK { print "in check\n"; } INIT { print "in init\n"; } END { print "in end\n"; } print "in mainline\n"; 1; [2]D​:/​: perl -We 'require "bcie.pl"' in begin in mainline in end [3]D​:/​: perl -We 'do "bcie.pl"' in begin in mainline in end

Perl Info ``` Site configuration information for perl v5.6.0: Configured by sthoenna at Thu Mar 9 20:40:23 PST 2000. Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration: Platform: osname=os2, osvers=2.30, archname=os2 uname='os2 efn.org 2 2.30 i386 ' config_args='-des -D prefix=d:/perl -D optimize=-O2 -fomit-frame-pointer -malign-loops=2 -malign-jumps=2 -malign-functions=2 -s -DDEBUGGING' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=undef d_sfio=undef uselargefiles=define use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef Compiler: cc='gcc', optimize='-O2 -fomit-frame-pointer -malign-loops=2 -malign-jumps=2 -malign-functions=2 -s -DDEBUGGING', gccversion=2.8.1 cppflags='-Zomf -Zmt -DDOSISH -DOS2=2 -DEMBED -I. -D_EMX_CRT_REV_=62' ccflags ='-Zomf -Zmt -DDOSISH -DOS2=2 -DEMBED -I. -D_EMX_CRT_REV_=62' stdchar='char', d_stdstdio=define, usevfork=false intsize=4, longsize=4, ptrsize=4, doublesize=8 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4 alignbytes=4, usemymalloc=y, prototype=define Linker and Libraries: ld='gcc', ldflags ='-Zexe -Zomf -Zmt -Zcrtdll -Zstack 32000' libpth=d:/emx/lib d:/emx/lib/mt libs=-lsocket -lm -lbsd libc=d:/emx/lib/mt/c_import.lib, so=dll, useshrplib=true, libperl=libperl.lib Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags='-Zdll', lddlflags='-Zdll -Zomf -Zmt -Zcrtdll ' Locally applied patches: v5.6.0-RC1 @INC for perl v5.6.0: d:/perl/lib/5.6.0/os2 d:/perl/lib/5.6.0 d:/perl/lib/site_perl/5.6.0/os2 d:/perl/lib/site_perl/5.6.0 d:/perl/lib/site_perl . Environment for perl v5.6.0: HOME=d:\home\sthoenna LANG=en_us LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR=sthoenna PATH=d:\bin;C:\OS2;d:\perl\bin;C:\OS2\SYSTEM;C:\OS2\INSTALL;C:\;C:\OS2\MDOS;C:\OS2\APPS;C:\MMOS2;d:\os2apps\util;d:\DOSAPPS\UTIL;c:\sio;D:\WINDOWS;d:\pdksh;d:\emx\bin;d:\emacs\19.33\bin;d:\ispell PERL_BADLANG (unset) PERL_SH_DIR=d:\BIN SHELL (unset) ```
p5pRT commented 24 years ago

From @gsar

On Sun\, 12 Mar 2000 09​:22​:25 PST\, Yitzchak Scott-Thoennes wrote​:

I think CHECK and INIT blocks should trigger a warning if they are not going to be executed.

[1]D​:/​: cat bcie.pl BEGIN { print "in begin\n"; } CHECK { print "in check\n"; } INIT { print "in init\n"; } END { print "in end\n"; } print "in mainline\n"; 1; [2]D​:/​: perl -We 'require "bcie.pl"' in begin in mainline in end

Check this out.

Sarathy gsar@​ActiveState.com

Inline Patch ```diff -----------------------------------8<----------------------------------- Change 5813 by gsar@auger on 2000/03/19 03:38:10 warn about CHECK and INIT blocks encountered at run time Affected files ... ... //depot/perl/op.c#279 edit ... //depot/perl/pod/perldelta.pod#189 edit ... //depot/perl/pod/perldiag.pod#132 edit ... //depot/perl/t/pragma/warn/op#18 edit Differences ... ==== //depot/perl/op.c#279 (text) ==== Index: perl/op.c --- perl/op.c.~1~ Sat Mar 18 19:38:17 2000 +++ perl/op.c Sat Mar 18 19:38:17 2000 @@ -4655,6 +4655,8 @@ if (!PL_checkav) PL_checkav = newAV(); DEBUG_x( dump_sub(gv) ); + if (PL_main_start && ckWARN(WARN_VOID)) + Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block"); av_unshift(PL_checkav, 1); av_store(PL_checkav, 0, SvREFCNT_inc(cv)); GvCV(gv) = 0; @@ -4663,6 +4665,8 @@ if (!PL_initav) PL_initav = newAV(); DEBUG_x( dump_sub(gv) ); + if (PL_main_start && ckWARN(WARN_VOID)) + Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block"); av_push(PL_initav, SvREFCNT_inc(cv)); GvCV(gv) = 0; } @@ -4803,6 +4807,8 @@ else if (strEQ(s, "CHECK")) { if (!PL_checkav) PL_checkav = newAV(); + if (PL_main_start && ckWARN(WARN_VOID)) + Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block"); av_unshift(PL_checkav, 1); av_store(PL_checkav, 0, SvREFCNT_inc(cv)); GvCV(gv) = 0; @@ -4810,6 +4816,8 @@ else if (strEQ(s, "INIT")) { if (!PL_initav) PL_initav = newAV(); + if (PL_main_start && ckWARN(WARN_VOID)) + Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block"); av_push(PL_initav, SvREFCNT_inc(cv)); GvCV(gv) = 0; } ==== //depot/perl/pod/perldelta.pod#189 (text) ==== Index: perl/pod/perldelta.pod --- perl/pod/perldelta.pod.~1~ Sat Mar 18 19:38:17 2000 +++ perl/pod/perldelta.pod Sat Mar 18 19:38:17 2000 @@ -1959,7 +1959,7 @@ =item "%s" variable %s masks earlier declaration in same %s -(W) A "my" or "our" variable has been redeclared in the current scope or statement, +(W misc) A "my" or "our" variable has been redeclared in the current scope or statement, effectively eliminating all access to the previous instance. This is almost always a typographical error. Note that the earlier variable will still exist until the end of the scope or until all closure referents to it are @@ -1972,7 +1972,7 @@ =item "our" variable %s redeclared -(W) You seem to have already declared the same global once before in the +(W misc) You seem to have already declared the same global once before in the current lexical scope. =item '!' allowed only after types %s @@ -2007,25 +2007,25 @@ =item /%s/: Unrecognized escape \\%c passed through -(W) You used a backslash-character combination which is not recognized +(W regexp) You used a backslash-character combination which is not recognized by Perl. This combination appears in an interpolated variable or a C<'>-delimited regular expression. The character was understood literally. =item /%s/: Unrecognized escape \\%c in character class passed through -(W) You used a backslash-character combination which is not recognized +(W regexp) You used a backslash-character combination which is not recognized by Perl inside character classes. The character was understood literally. =item /%s/ should probably be written as "%s" -(W) You have used a pattern where Perl expected to find a string, +(W syntax) You have used a pattern where Perl expected to find a string, as in the first argument to C. Perl will treat the true or false result of matching the pattern against $_ as the string, which is probably not what you had in mind. =item %s() called too early to check prototype -(W) You've called a function that has a prototype before the parser saw a +(W prototype) You've called a function that has a prototype before the parser saw a definition or declaration for it, and Perl could not check that the call conforms to the prototype. You need to either add an early prototype declaration for the subroutine in question, or move the subroutine @@ -2059,14 +2059,14 @@ =item %s package attribute may clash with future reserved word: %s -(W) A lowercase attribute name was used that had a package-specific handler. +(W reserved) A lowercase attribute name was used that had a package-specific handler. That name might have a meaning to Perl itself some day, even though it doesn't yet. Perhaps you should use a mixed-case attribute name, instead. See L. =item (in cleanup) %s -(W) This prefix usually indicates that a DESTROY() method raised +(W misc) This prefix usually indicates that a DESTROY() method raised the indicated exception. Since destructors are usually called by the system at arbitrary points during execution, and often a vast number of times, the warning is issued only once for any number @@ -2101,7 +2101,7 @@ =item Bareword found in conditional -(W) The compiler found a bareword where it expected a conditional, +(W bareword) The compiler found a bareword where it expected a conditional, which often indicates that an || or && was parsed as part of the last argument of the previous construct, for example: @@ -2117,17 +2117,17 @@ =item Binary number > 0b11111111111111111111111111111111 non-portable -(W) The binary number you specified is larger than 2**32-1 +(W portable) The binary number you specified is larger than 2**32-1 (4294967295) and therefore non-portable between systems. See L for more on portability concerns. =item Bit vector size > 32 non-portable -(W) Using bit vector sizes larger than 32 is non-portable. +(W portable) Using bit vector sizes larger than 32 is non-portable. =item Buffer overflow in prime_env_iter: %s -(W) A warning peculiar to VMS. While Perl was preparing to iterate over +(W internal) A warning peculiar to VMS. While Perl was preparing to iterate over %ENV, it encountered a logical name or symbol definition which was too long, so it was truncated to the string shown. @@ -2148,7 +2148,7 @@ =item Can't ignore signal CHLD, forcing to default -(W) Perl has detected that it is being run with the SIGCHLD signal +(W signal) Perl has detected that it is being run with the SIGCHLD signal (sometimes known as SIGCLD) disabled. Since disabling this signal will interfere with proper determination of exit status of child processes, Perl has reset the signal to its default value. @@ -2191,7 +2191,7 @@ =item Character class syntax [%s] belongs inside character classes -(W) The character class constructs [: :], [= =], and [. .] go +(W unsafe) The character class constructs [: :], [= =], and [. .] go I character classes, the [] are part of the construct, for example: /[012[:alpha:]345]/. Note that [= =] and [. .] are not currently implemented; they are simply placeholders for @@ -2237,7 +2237,7 @@ =item Did you mean "local" instead of "our"? -(W) Remember that "our" does not localize the declared global variable. +(W misc) Remember that "our" does not localize the declared global variable. You have declared it again in the same lexical scope, which seems superfluous. =item Document contains no data @@ -2251,14 +2251,14 @@ =item false [] range "%s" in regexp -(W) A character class range must start and end at a literal character, not +(W regexp) A character class range must start and end at a literal character, not another character class like C<\d> or C<[:alpha:]>. The "-" in your false range is interpreted as a literal "-". Consider quoting the "-", "\-". See L. =item Filehandle %s opened only for output -(W) You tried to read from a filehandle opened only for writing. If you +(W io) You tried to read from a filehandle opened only for writing. If you intended it to be a read/write filehandle, you needed to open it with "+<" or "+>" or "+>>" instead of with "<" or nothing. If you intended only to read from the file, use "<". See @@ -2266,7 +2266,7 @@ =item flock() on closed filehandle %s -(W) The filehandle you're attempting to flock() got itself closed some +(W closed) The filehandle you're attempting to flock() got itself closed some time before now. Check your logic flow. flock() operates on filehandles. Are you attempting to call flock() on a dirhandle by the same name? @@ -2279,19 +2279,19 @@ =item Hexadecimal number > 0xffffffff non-portable -(W) The hexadecimal number you specified is larger than 2**32-1 +(W portable) The hexadecimal number you specified is larger than 2**32-1 (4294967295) and therefore non-portable between systems. See L for more on portability concerns. =item Ill-formed CRTL environ value "%s" -(W) A warning peculiar to VMS. Perl tried to read the CRTL's internal +(W internal) A warning peculiar to VMS. Perl tried to read the CRTL's internal environ array, and encountered an element without the C<=> delimiter used to spearate keys from values. The element is ignored. =item Ill-formed message in prime_env_iter: |%s| -(W) A warning peculiar to VMS. Perl tried to read a logical name +(W internal) A warning peculiar to VMS. Perl tried to read a logical name or CLI symbol definition when preparing to iterate over %ENV, and didn't see the expected delimiter between key and value, so the line was ignored. @@ -2302,7 +2302,7 @@ =item Illegal binary digit %s ignored -(W) You may have tried to use a digit other than 0 or 1 in a binary number. +(W digit) You may have tried to use a digit other than 0 or 1 in a binary number. Interpretation of the binary number stopped before the offending digit. =item Illegal number of bits in vec @@ -2312,7 +2312,7 @@ =item Integer overflow in %s number -(W) The hexadecimal, octal or binary number you have specified either +(W overflow) The hexadecimal, octal or binary number you have specified either as a literal or as an argument to hex() or oct() is too big for your architecture, and has been converted to a floating point number. On a 32-bit architecture the largest hexadecimal, octal or binary number @@ -2372,7 +2372,7 @@ =item Missing command in piped open -(W) You used the C or C +(W pipe) You used the C or C construction, but the command was missing or blank. =item Missing name in "my sub" @@ -2406,7 +2406,7 @@ =item Octal number > 037777777777 non-portable -(W) The octal number you specified is larger than 2**32-1 (4294967295) +(W portable) The octal number you specified is larger than 2**32-1 (4294967295) and therefore non-portable between systems. See L for more on portability concerns. @@ -2428,7 +2428,7 @@ =item Parentheses missing around "%s" list -(W) You said something like +(W parenthesis) You said something like my $foo, $bar = @_; @@ -2440,12 +2440,12 @@ =item Possible Y2K bug: %s -(W) You are concatenating the number 19 with another number, which +(W y2k) You are concatenating the number 19 with another number, which could be a potential Year 2000 problem. =item pragma "attrs" is deprecated, use "sub NAME : ATTRS" instead -(W) You have written somehing like this: +(W deprecated) You have written somehing like this: sub doit { @@ -2483,7 +2483,7 @@ =item Reference is already weak -(W) You have attempted to weaken a reference that is already weak. +(W misc) You have attempted to weaken a reference that is already weak. Doing so has no effect. =item setpgrp can't take arguments @@ -2493,7 +2493,7 @@ =item Strange *+?{} on zero-length expression -(W) You applied a regular expression quantifier in a place where it +(W regexp) You applied a regular expression quantifier in a place where it makes no sense, such as on a zero-width assertion. Try putting the quantifier inside the assertion instead. For example, the way to match "abc" provided that it is followed by three @@ -2508,13 +2508,21 @@ =item This Perl can't set CRTL environ elements (%s=%s) -(W) Warnings peculiar to VMS. You tried to change or delete an element +(W internal) Warnings peculiar to VMS. You tried to change or delete an element of the CRTL's internal environ array, but your copy of Perl wasn't built with a CRTL that contained the setenv() function. You'll need to rebuild Perl with a CRTL that does, or redefine F (see L) so that the environ array isn't the target of the change to %ENV which produced the warning. +=item Too late to run %s block + +(W void) A CHECK or INIT block is being defined during run time proper, +when the opportunity to run them has already passed. Perhaps you are +loading a file with C or C when you should be using +C instead. Or perhaps you should put the C or C +inside a BEGIN block. + =item Unknown open() mode '%s' (F) The second argument of 3-argument open() is not among the list @@ -2530,7 +2538,7 @@ =item Unrecognized escape \\%c passed through -(W) You used a backslash-character combination which is not recognized +(W misc) You used a backslash-character combination which is not recognized by Perl. The character was understood literally. =item Unterminated attribute parameter in attribute list @@ -2563,7 +2571,7 @@ =item Value of CLI symbol "%s" too long -(W) A warning peculiar to VMS. Perl tried to read the value of an %ENV +(W misc) A warning peculiar to VMS. Perl tried to read the value of an %ENV element from a CLI symbol table, and found a resultant string longer than 1024 characters. The return value has been truncated to 1024 characters. ==== //depot/perl/pod/perldiag.pod#132 (text) ==== Index: perl/pod/perldiag.pod --- perl/pod/perldiag.pod.~1~ Sat Mar 18 19:38:17 2000 +++ perl/pod/perldiag.pod Sat Mar 18 19:38:17 2000 @@ -3053,6 +3053,14 @@ B<-M> or B<-m> option. This is an error because B<-M> and B<-m> options are not intended for use inside scripts. Use the C pragma instead. +=item Too late to run %s block + +(W void) A CHECK or INIT block is being defined during run time proper, +when the opportunity to run them has already passed. Perhaps you are +loading a file with C or C when you should be using +C instead. Or perhaps you should put the C or C +inside a BEGIN block. + =item Too many ('s =item Too many )'s ==== //depot/perl/t/pragma/warn/op#18 (text) ==== Index: perl/t/pragma/warn/op --- perl/t/pragma/warn/op.~1~ Sat Mar 18 19:38:17 2000 +++ perl/t/pragma/warn/op Sat Mar 18 19:38:17 2000 @@ -808,3 +808,54 @@ sub joe ($$) {} EXPECT main::fred() called too early to check prototype at - line 3. +######## +# op.c [Perl_newATTRSUB] +--FILE-- abc.pm +use warnings 'void' ; +BEGIN { $| = 1; print "in begin\n"; } +CHECK { print "in check\n"; } +INIT { print "in init\n"; } +END { print "in end\n"; } +print "in mainline\n"; +1; +--FILE-- +use abc; +delete $INC{"abc.pm"}; +require abc; +do "abc.pm"; +EXPECT +in begin +in mainline +in check +in init +in begin +Too late to run CHECK block at abc.pm line 3. +Too late to run INIT block at abc.pm line 4. +in mainline +in begin +Too late to run CHECK block at abc.pm line 3. +Too late to run INIT block at abc.pm line 4. +in mainline +in end +in end +in end +######## +# op.c [Perl_newATTRSUB] +--FILE-- abc.pm +no warnings 'void' ; +BEGIN { $| = 1; print "in begin\n"; } +CHECK { print "in check\n"; } +INIT { print "in init\n"; } +END { print "in end\n"; } +print "in mainline\n"; +1; +--FILE-- +require abc; +do "abc.pm"; +EXPECT +in begin +in mainline +in begin +in mainline +in end +in end End of Patch. ```
p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

warn about CHECK and INIT blocks encountered at run time

Why not schedule those for end-of-file/eval execution?

  while compiling unit {   if a CHECK or INIT seen too late   push coderef on end-of-unit execution stack   }   foreach item in delayed execution stack   run it

--tom

p5pRT commented 24 years ago

From @gsar

On Sat\, 18 Mar 2000 20​:47​:37 MST\, Tom Christiansen wrote​:

warn about CHECK and INIT blocks encountered at run time

Why not schedule those for end-of-file/eval execution?

while compiling unit { if a CHECK or INIT seen too late push coderef on end-of-unit execution stack } foreach item in delayed execution stack run it

Good idea\, but this is going to only hurt more than it helps until we figure out how to compile individual modules into distinct units. It also needs more per-file infrastructure than we presently have (to hang those INITs on to\, for instance).

Anyway\, this is something for the Todo list.

Sarathy gsar@​ActiveState.com

p5pRT commented 24 years ago

From @ysth

In article \200003190427\.UAA28076@&#8203;maul\.ActiveState\.com\, Gurusamy Sarathy \gsar@&#8203;ActiveState\.com wrote​:

On Sat\, 18 Mar 2000 20​:47​:37 MST\, Tom Christiansen wrote​:

warn about CHECK and INIT blocks encountered at run time

Why not schedule those for end-of-file/eval execution?

while compiling unit { if a CHECK or INIT seen too late push coderef on end-of-unit execution stack } foreach item in delayed execution stack run it

Good idea\, but this is going to only hurt more than it helps until we figure out how to compile individual modules into distinct units. It also needs more per-file infrastructure than we presently have (to hang those INITs on to\, for instance).

Anyway\, this is something for the Todo list.

Yup. I didn't see the warnings as being a permanent fix. WRT Tom's suggestion\, *perhaps* INIT blocks could be "promoted" to BEGIN blocks (or be run after all the BEGIN blocks in this parse phase) but I don't know that the same is true for CHECK blocks.