Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re4: Scalars, Lists, and Arrays

by sachmet (Scribe)
on Apr 13, 2001 at 20:41 UTC ( #72387=note: print w/ replies, xml ) Need Help??

in reply to Re: Re: Re: Scalars, Lists, and Arrays
in thread Scalars, Lists, and Arrays

Void context ... Maybe its best to avoid this one altogether

Void context is where you're performing an operation and doing nothing with the result. That is,

#!/usr/bin/perl -cw "String";
tells me:
Useless use of a constant in void context at - line 2.
In fact, I care about this because it generally indicates a problem with my program. What use is a constant where I don't store what that constant is?

Void context can be useful. Consider:
grep { s/\s+$// } @lines;
which will remove all trailing whitespace from all the lines I have in @lines. grep normally returns the lines that matched the pattern. But because I'm in void context, the values just get thrown away and all I'm left with is @lines which contains every line missing its trailing whitespace. (By the way, the better way to do that is:
foreach (@lines) { s/\s+$// }

Consider also that some objects require methods to be run in void context. While you're not just throwing away their values, these things have side effects that perform useful work. For example, in CGI, you can do:
which, while run in void context, imports all parameters into the 'R' namespace.

So, in summary, -w isn't complaining because you're running things in void context. It's complaining because you're running in void context without doing anything that could be construed as useful.

Update: D'oh, chromatic beat me to it.

Comment on Re4: Scalars, Lists, and Arrays
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2016-02-07 01:42 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (241 votes), past polls