Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to make warnings disappear

by Dominus (Parson)
on Dec 15, 2000 at 07:47 UTC ( [id://46785]=obfuscated: print w/replies, xml ) Need Help??

In a recent article at www.perl.com, I showed some code that looked like this:
"nosehair" while s/(.{1,3})\*/*,$1/;
The code was a joke, and I was rather irritated when people write to me to 'correct' it.

Today I got mail from someone who complained that the string "nosehair" generated a 'Useless use of constant in void context' warning under -w, and suggested that I change it to 1 instead. I thought this was a little silly, because you should not have to turn on -w to know that "nosehair" is a bad idea.

One of the big problems with -w is that some people have it wired to a toggle switch and when they turn on -w they disable their brains at the same time.

Anyway, I suggested that if this guy's only problem with "nosehair" was that it generated a warning under -w, he could make the warning go away with the following change:

"ignore my nose hairs, please" while s/(.{1,3})\*/*,$1/;

That's the obfuscation. The puzzle is, why does changing the string make the warning go away?

I will reveal the answer in a couple of days; or you can find it on page 321 of the pink camel book.

(Please don't post spoilers, especially if you were hanging around when I explained it in the chatbox this afternoon. Also if you are Randal, then we already know that you know the answer, since you are an author of the pink camel book.)

Replies are listed 'Best First'.
Re: How to make warnings disappear
by Dominus (Parson) on Dec 16, 2000 at 21:43 UTC
    OK, here's the answer.

    In Perl 4, there was no POD. But there was a similar trick for wrapping the documentation up in your program. You would write documentation in troff code. (troff is the unix formatting program that is used to format the man pages.) Then you would embed the troff documentation after the __END__ marker for your program, and add some troff commands at the beginning of your program to tell troff to ignore the rest of the Perl code in between. When you ran your program with Perl, the troff commands would be ignored because they looked like string constants in void context.

    The result is that you would have one file which would serve as input to Perl or to troff. If you fed it to Perl, it would run. If you fed it to troff (or to the man program, which is just nroff) you would get formatted documentation out the other side.

    But when you ran the program, you wouldn't want to get a warning that the documentation strings were being uselessly used in void context, so strings that begin with "ds", "di", and "ig" were exempted from the warning. "di" diverts input into a nother macro, and "ig tells troff to ignore all the following text until it sees a certain character sequence; that's how troff was told to ignore the Perl code. "dr" doesn't appear to have been used.

    The pink camel book contains a program called wrapman, which takes a Perl program and wraps it up with the right troff incantations, and appends a stub man page to it. To see an example of the result, look at the file eg/relink that is still in the Perl source distribution.

    Astute readers may realize that arranging a file so that it is syntactically correct in two different languages at the same time is a common trick played by people who like to enter obfuscated code contests. It's probably no coincidence that Larry Wall was a repeat winner in the IOCCC.

      I said:
      > arranging a file so that it is syntactically correct in two
      > different languages at the same time is a common trick...
      > Larry Wall was a repeat winner in the IOCCC.
      Another related item I forgot about: perlrun says:
      #!/bin/sh -- # -*- perl -*- -p eval 'exec perl -wS $0 ${1+"$@"}' if $running_under_some_shell;
      If this code is run by /bin/sh, it tells the shell to execute perl instead to run the script. But when perl sees this code, it does nothing.

Re: How to make warnings disappear
by merlyn (Sage) on Dec 15, 2000 at 07:52 UTC
    I will reveal the answer in a couple of days; or you can find it on page 321 of the pink camel book.

    (Please don't post spoilers, especially if you were hanging around when I explained it in the chatbox this afternoon. Also if you are Randal, then we already know that you know the answer, since you are an author of the pink camel book.)

    Just checked my "every book I've had something to do with" archive shelf, and it's page 318 in the first pink camel printing, but page 321 on the second and third printing.

    FYI, from someone who would know. {grin}

    -- Randal L. Schwartz, Perl hacker

      Says merlyn:

      > page 321 on the second and third printing.

      Yes, my copy is the third printing, which is no doubt indicative of my own relative latecoming to Perl., The date I wrote on the flyleaf is "2 September 1993", which contradicts various other sources that say I have been programming in Perl since 1992. But it is probably authoritative.

Re: How to make warnings disappear
by Aristotle (Chancellor) on Jul 04, 2013 at 23:16 UTC

    And now, as of 60282a494b5eb9b1c93b895d1cc5849b1d17d28a, which will be in Perl 5.19.2 and ultimately 5.20, this era comes to a close… I admit it makes me a little sad to see this little quirk disappear.

    Thanks for giving me a laugh all those 12½ years ago, Mark.

    Makeshifts last the longest.

Re: How to make warnings disappear
by japhy (Canon) on Dec 15, 2000 at 09:25 UTC
      worse, it seems to turn on the word 'ignore' ... and all my books are at work. Ignore, nevermore, close the door, Eleanor - argh! its only 'ig' that matters! Hmm, ig are valid s/// etc. operators but, but 'gi' doesn't work, nor does i or g alone ... but ig unquoted does ... WitchCraft! Burn them! throw them in the pond!

      a

Re: How to make warnings disappear
by autark (Friar) on Dec 16, 2000 at 01:16 UTC
    I bumped into this feature while reading some of the scripts in the eg/ directory (of the perl installation). A lot of, uhm, interesting code in there :-)

    The "fun" thing about this is that it is a feature we all depend on almost every day (hint: module writing).

    Oh, the joys of deprecated constructs, perl -e '@a{a,b,c,d} = (); do { print if $_ NE d } for keys a'

    *shrug* :-)

    Autark.

      NE isn't just deprecated any more. I actually got rid of it for good in 5.6.1.

      And good riddance.

        Could that be called constructive destruction ? :-)

        I was just thinking, why hasn't this "disgusting" "ignore" business been deprecated ? Then again, I guess that would be counterintuitive. After all, they were introduced to avoid warnings from -w. But they could surely be removed all together ? As long as they match m/\w+/ or some such ?

        Autark.

Re: How to make warnings disappear
by ambrus (Abbot) on Oct 11, 2007 at 06:29 UTC
Re: How to make warnings disappear
by jima (Vicar) on Dec 15, 2000 at 20:03 UTC

    I admit it, I had to look it up. Interestingly, I was actually thinking along these lines, but couldn't find any supporting documentation in my Perl 5 reference material to point me in the right direction.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found