Why not use strict and warnings in Perl?

前端 未结 3 2087
情深已故
情深已故 2021-02-13 17:40

I\'ve seen obfuscated and golfed code that keeps is off to avoid declaring variables, and I can see skipping them on the command line with the -e switch to keep the one-liner sh

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 18:32

    Sometimes strict and warnings prevent you from doing things you want to do, like doing certain manipulations on the symbol table that would violate strict 'refs', or redefining a subroutine where warnings 'redefine' would be triggered. At other times it is more convenient to ignore certain warnings than to write defensive code against them, like a quick-and-dirty database dump for a table that might contain NULL/undef values that would trigger warnings 'uninitialized'.

    use strict and use warnings, and their retardants no strict and no warnings can be locally scoped, so it is a best practice to disable strict and warnings in the smallest practical scope.

    @data = get_some_data_from_database();
    if (some_condition()) {
        no warnings 'uninitialized';
        logger->debug("database contains: @$_") for @data;
    
        ## otherwise, suppressing the warnings would take something
        ## less readable and more error-prone like:
        #  logger->debug("database contains: @{[map{defined?$_:''}@$_]}") for @data
        #  logger->debug("database contains: @{[map{$_//''}@$_]}") for @data
    }
    # end of scope, warnings `uninitialized' is enabled again
    

提交回复
热议问题