Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: warnings pragma anomaly

by muba (Priest)
on Oct 19, 2015 at 17:23 UTC ( [id://1145344]=note: print w/replies, xml ) Need Help??


in reply to warnings pragma anomaly

Another place where using 1 in void context is commonplace is near/at the end of modules. When use Foo loads Foo.pm, then the last expression evaluated inside Foo.pm better be a true value, to signal that the module loaded succesfully. That's why you usually see the line

1;
near or at the end of a .pm file. Because 1 evaluates to a true value, but is special-cased not to raise a warning.

Replies are listed 'Best First'.
Re^2: warnings pragma anomaly
by choroba (Cardinal) on Oct 19, 2015 at 17:40 UTC
    As the return value of this expression is returned and checked for truthness, it's not used in a void context. Moreover, you can use any true value at the end of a module, and you won't get any warnings. See also Acme::ReturnValue.

    Update: You can check the generated lists here.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      As the return value of this expression is returned and checked for truthness, it's not used in a void context.

      Are you sure about that? If you end a package with just: 1; no warning is produced; but if you end it with just: 42;:

      C:\test>perl -Mstrict -w package x; sub f{ 1 } 42; ^Z Useless use of a constant in void context at - line 5.

      Although it is returned and checked at runtime; at the point of parsing it is in a void context.

      And you can't avoid that by prefixing a return statement either:

      C:\test>perl -Mstrict -w package x; sub f{ 1 } return 42; ^Z Can't return outside a subroutine at - line 3.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I usually don't run modules, I use them. No warning is produced in such situations.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found