in reply to How exactly does no work?

Warnings are not errors, and errors are not warnings.

The fatal error comes from strict because you didn't declare $d. And because it comes from strict and not warnings, no warnings; has no effect on it. While you can say no strict 'vars';, it's much better to just declare $d with my.

Replies are listed 'Best First'.
Re^2: How exactly does no work?
by PerlOnTheWay (Monk) on Feb 16, 2012 at 11:29 UTC
    Why is it NOT fatal when I print $b; in sub a?
Re^2: How exactly does no work?
by PerlOnTheWay (Monk) on Feb 16, 2012 at 11:37 UTC

    Is no a run time or compile time thing?

    And it seems it's only effective in the current block, not in outer or inner block

      Minimal reference to the docs -- say perldoc -f no -- would put you on the path to answers. See also, as the docs suggest, use.

        Okay, let’s just cut to the chase here.   use is somewhat of an “overloaded” construct in Perl.   It can be employed to reference an outside module (use foo;), or as a pragma to request certain compile-time behavior (use strict;).

        When I typed in what you entered as a “one-liner,” I got various compilation errors (as I should have).   There are several things quite wrong with the code ... undeclared variables, parentheses, and so forth.   On my system it will not compile at all.   If I add my ($b, $d); I get no output at all because of course everything is now undef.   So I am just going to set that (non-)example completely aside.

        no warnings; like its brother use warnings;, is a compile-time directive.   It instructs Perl to behave in different ways when compiling your source-code into its internal form for execution.   One form countermands the other.   I do not profess to know the whys and wherefores of these things because the one thing that I do know is how to use them correctly, which IMHO is:

        • At the top of every Perl program that you write, use strict; use warnings;.
        • In the extremely rare case where you are forced to do something that these declarations would prevent, and you cannot find another way (translation:   you haven’t looked hard enough yet, e.g. at UNIVERSAL), then you should bracket the smallest portion of code necessary.   Insert no warnings;, then do the minimal amount of work, then immediately use warnings; again ... having first inserted a prodigious comment explaining exactly why you had to do this.

        perldoc -f no doesn't tell the answer.