(tye)Re: Triggering 'strict' from within a package
by tye (Sage) on Aug 12, 2002 at 15:53 UTC
|
package Custom::Package;
require strict;
require warnings;
sub import {
if( $inDevelopment ) {
&strict::import;
&warnings::import;
}
}
1;
- tye (but my friends call me "Tye") | [reply] [d/l] |
Re: Triggering 'strict' from within a package
by Abigail-II (Bishop) on Aug 12, 2002 at 15:46 UTC
|
strict.pm is one of the simplest modules in
the Perl distribution. Just copy the functionality in
your custom package.
package Custom::Package;
sub import {
$^H |= 0x604 unless $ENV {ENV} eq "PR";
}
sub unimport {
$^H &= ~ 0x604 unless $ENV {ENV} eq "PR";
}
1;
Assuming you use the ENV environment variable
to flag a production environment.
Don't believe those who claim it cannot be done - this is only
one of the ways how to do it.
But I'd like to know why you want to turn off strictness
in production code.
Abigail | [reply] [d/l] [select] |
|
Hmm, $^H, that's the one with "don't cross the streams" written all over it, isn't it? :) I think I prefer tye's solution, although you've given me another lesson (always check the module). I had mistakenly assumed there wouldn't be a strict.pm, since it was just a pragma, although I was slightly confused in Exporter.pm when I saw there was no code to handle the pragmas differently.
As for turning off strict, it's mainly about keeping the webserver log files clean - we do a lot of traffic analysis on the log files (even the error logs), and Perl error messages can sometimes screw it up. Plus, we have an alternative system for collecting error messages and assertion failures. It's not as tight as strict, but that's the opportunity cost.
Thanks!
| [reply] |
|
| [reply] [d/l] [select] |
|
I still don't understand. Is there any case where strict merely produces a warning?
I was under the impression that strict's only effect was to cause the compilation phase to fail if it didn't like the code. Are you sure you are not confusing strict with warnings? Or did I miss something?
| [reply] |
|
|
|
|
> it's mainly about keeping the webserver log files clean
But if you get your script working on the stage server, before moving it to the production server your 'use strict' shouldn't add anything to your webserver logs. Unless I'm mistaken, which did happen once before.
| [reply] |
Re: Triggering 'strict' from within a package
by runrig (Abbot) on Aug 12, 2002 at 16:47 UTC
|
After reading Abigail-II's answer and tye's answer, I looked in perlvar for $^H and found something almost identical to tye's answer: BEGIN { require strict; strict->import('vars') if $condition }
# which to import all restrictions would be
BEGIN { require strict; strict->import if $condition }
| [reply] [d/l] |
Re: Triggering 'strict' from within a package
by Joost (Canon) on Aug 12, 2002 at 14:59 UTC
|
I rolled my own importer which automatically places the modules in main::
Ah, you mean the package main; importer code :-)
Anyway, strict is a lexical pragma, so you cannot set at it for other files. I assume this was done to keep non-strict code working together with strict code.
this is also one of the reasons you have to use strict; for every module you use instead of defining it in the top-level code.
Update: I cannot think of any reason to turn off strict based on the location of the script, so I would suggest keeping it on where it makes sense.
Hope this explains a bit,
Joost.
--
Joost downtime n. The period during which a system
is error-free and immune from user input.
| [reply] [d/l] [select] |
|
Ah, you mean the package main; importer code :-)
Sadly not :) It will actually look at other stuff - such as @INC ordering - based on location (& other factors) also, so it will actually load different version of a module based on what environment the script is being run in.
Anyway, strict is a lexical pragma, so you cannot set at it for other files. I assume this was done to keep non-strict code working together with strict code
I had a horrible feeling something like that might be the case. Note, though, that it's not a case of turning it off, but more a case of not turning it on - it's just not useful in a lot of cases.
Cheers!
| [reply] |
|
| [reply] |
|
Re: Triggering 'strict' from within a package
by erikharrison (Deacon) on Aug 12, 2002 at 18:54 UTC
|
Perhaps I am a bit late in the game here to answer your question, especially after such astute answers from tye and Abigail-II. However, with any case of trying to make "meta" declarations from outside the lexical space I would use a source filter to tack the appropriate boilerplate code to the begining of the file prior to it being parsed.
Cheers,
Erik
Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet
| [reply] |