Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: (tye)Re: Regular expression

by Adam (Vicar)
on Jun 08, 2001 at 01:37 UTC ( #86767=note: print w/replies, xml ) Need Help??

in reply to (tye)Re: Regular expression
in thread Regular expression

Don't forget, "0 but true"

Replies are listed 'Best First'.
(tye)Re2: Regular expression
by tye (Sage) on Jun 11, 2001 at 21:58 UTC

    #!/usr/bin/perl -w $_= "193287461528"; "0 but true" while s/(\d)(\d\d\d)(\D|$)/$1,$2$3/; print;
    Useless use of a constant in void context at 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;
    $ perl Useless use of a constant in void context at line 9. 193,287,461,528 $ perl 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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2021-01-26 16:09 GMT
Find Nodes?
    Voting Booth?