Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: How exactly does no work?

by sundialsvc4 (Abbot)
on Feb 16, 2012 at 13:49 UTC ( [id://954232]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How exactly does no work?
in thread How exactly does no work?

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.

Replies are listed 'Best First'.
Re^5: How exactly does no work?
by chromatic (Archbishop) on Feb 17, 2012 at 01:08 UTC
    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;).

    I think that explanation confuses more than it enlightens. In both cases (pragma and module), use does the same thing:

    BEGIN { require 'Module.pm': 'Module'->import; };

    It always performs the require (modulo Perl's internal cache of what it's already loaded) and it always performs the import (modulo use Foo ();). use has no idea of whether what it's loading is a pragma. That part's up to the loaded thing's import() method.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://954232]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found