Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

perl interpolation

by pavunkumar (Scribe)
on Jan 24, 2011 at 10:29 UTC ( #883899=perlquestion: print w/ replies, xml ) Need Help??
pavunkumar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

How perl interpolates '00' . when i give following things , it gets succeed .

if ( '00' ) { print "Yes"; }
Thanks

Comment on perl interpolation
Download Code
Re: perl interpolation
by JavaFan (Canon) on Jan 24, 2011 at 10:35 UTC
    It's not interpolation.

    But '00' is true. The only strings that are false are the empty string, and the string '0'.

Re: perl interpolation
by cdarke (Prior) on Jan 24, 2011 at 10:37 UTC
    Interpolation occurs between double quotes or qq and is not involved here (and would not affect the outcome if it was).

    Placing quotes around characters makes it a text string, and for false we would need an empty string or a single zero: anything else is true. Numeric zero (with no quotes) is false:
    use strict; use warnings; if ( '00' ) { print "Yes\n" } if ( 00 ) { print "Yes\n" } else { print "No\n" }
    Gives:
    Yes No
    Update: correction to wording of a false string.
      Interpolation occurs between double quotes or qq

      Please read perlop carefully, particularly the Quote-Like Operators section where it says:

          q/STRING/
          'STRING'
              A single-quoted, literal string.  A backslash represents a backslash unless followed by the
              delimiter or another backslash, in which case the delimiter or backslash is interpolated.
      

        Seems like a rather poor choice of word to me. The delimiter or backslash is already present in the string literal, so how can it be interpolation?
        The doc is rather inconsistent, at the start of perlop is the following table:
        Customary Generic Meaning Interpolates '' q{} Literal no "" qq{} Literal yes `` qx{} Command yes* qw{} Word list no // m{} Pattern match yes* qr{} Pattern yes* s{}{} Substitution yes* tr{}{} Transliteration no (but see below) <<EOF here-doc yes* * unless the delimiter is ''.
        Note: it says single quotes do not offer interpolation. So, as in most holy books, it depends on which part of perlop you (carefully) read.
Re: perl interpolation
by Anonymous Monk on Jan 24, 2011 at 15:44 UTC

    Perl does try to “DWIM = Do What I Mean.”   Which is definitely a two-edged sword sometimes.   Just try to write code such that your intentions are perfectly clear ... not so much “to the computer,” but “to you and your fellow humans.”   No surprises, nothing clever, no side-effects.   The rest is merely Golf.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://883899]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-08-30 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls