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

(tye)Re: Regular expression

by tye (Sage)
on Jun 07, 2001 at 20:50 UTC ( [id://86648]=note: print w/replies, xml ) Need Help??


in reply to Regular expression

No one would take the bait in the chatterbox, so I'll post a node instead...

You can use the following do-nothing values:

  • 0
  • 1
  • "ignore"
  • "diabolic"
  • "ds..."
Any other values can get you warnings. I prefer 0 as it is much clearer that you intend it to do nothing. q-:

Oh, and the valid do-nothing constant strings only have to start with the same two letters as one of the strings I listed above.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Regular expression
by japhy (Canon) on Jun 07, 2001 at 20:57 UTC
    Yes yes, we all remember the node about "ig" and "ds" and "di" being legacy Perl 4 things...
    /* from op.c */ if (ckWARN(WARN_VOID)) { useless = "a constant"; /* the constants 0 and 1 are permitted as they are conventionally used as dummies in constructs like 1 while some_condition_with_side_effects; */ if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) useless = 0; else if (SvPOK(sv)) { /* perl4's way of mixing documentation and code (before the invention of POD) was based on a trick to mix nroff and perl code. The trick was built upon these three nroff macros being used in void context. The pink camel has the details in the script wrapman near page 319. */ if (strnEQ(SvPVX(sv), "di", 2) || strnEQ(SvPVX(sv), "ds", 2) || strnEQ(SvPVX(sv), "ig", 2)) useless = 0; } }


    japhy -- Perl and Regex Hacker
Re: (tye)Re: Regular expression
by Adam (Vicar) on Jun 08, 2001 at 01:37 UTC
    Don't forget, "0 but true"

      #!/usr/bin/perl -w $_= "193287461528"; "0 but true" while s/(\d)(\d\d\d)(\D|$)/$1,$2$3/; print;
      produces:
      Useless use of a constant in void context at warn.pl line 3. 193,287,461,528

      because the "0 but true" doesn't yet have a numeric value so the "is 0 or 1" test doesn't even get attempted.

      To get "0 but true" to work as an "unwarned useless constant", you have to jump through some interesting hoops:

      #!/usr/bin/perl -w $_= "193287461528"; sub zero() { "0 but true" } BEGIN { if( @ARGV ) { eval '0+zero'; } } zero while s/(\d)(\d\d\d)(\D|$)/$1,$2$3/; print;
      produces
      $ perl warn.pl Useless use of a constant in void context at warn.pl line 9. 193,287,461,528 $ perl warn.pl x 193,287,461,528
      The "()" in "sub zero()" is required for Perl to compile uses of "zero" as a constant (instead of as a subroutine call). The "0+zero" causes Perl to cache a numeric value for the "zero" constant. But you'll note that this caching is done at compile time so I have to use stringy eval to prevent it from happening when I don't want it to. But stringy eval also prevents it from happening at compile time even if that code gets run so I have to use a BEGIN block to get conditional compile-time behavior.

      So, how to use "0 but true" as a unwarned useless constant can be reduced to:

      #!/usr/bin/perl -w $_= "193287461528"; sub zero() { "0 but true" } zero while s/(\d)(\d\d\d)(\D|$)/$1,$2$3/; print; my $x= 0+zero;

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found