Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

#!/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")

In reply to (tye)Re2: Regular expression by tye
in thread Regular expression by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-29 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found