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

Interpolating Nursery Rhyme

by Spenser (Friar)
on Apr 25, 2002 at 16:11 UTC ( [id://162015]=poem: print w/replies, xml ) Need Help??

Still in my formative Perl years, I've had difficulty understanding and knowing when to use single quotes around a variable and when to use double quotes.  I finally took the time today to experiment with the concept to be able to learn it truly.  In so doing, one of my co-workers (cerberus) and I came up with a little mnemonic or nursery rhyme to be able to remember when to use which. So, here it is for your benefit and enjoyment:

One quote is Numeric, But Double does for Both.

Well, let me know what you think.  Hopefully we're not missing the point on interpolating--otherwise this will be quite an embarassing posting.

That's Spenser, with an "s" like the detective.

Replies are listed 'Best First'.
Re: Interpolating Nursery Rhyme
by YuckFoo (Abbot) on Apr 25, 2002 at 16:56 UTC
    Simple quotes for simple strings.
    Substitutions like double dings.
    Want a number? Not a thing!

    YuckFoo

Re: Interpolating Nursery Rhyme
by stephen (Priest) on Apr 25, 2002 at 16:25 UTC

    Hmm... you don't use quotes with numbers. '5' and "5" are the same thing-- a string containing the character '5'. If it's in quotes of any sort, it's a string.

    One uses single quotes for strings of literal text. So:

    my $bar = 3; my $foo = 'Bar is $bar'; print $foo, "\n";
    prints "Bar is $bar".

    One uses double quotes for strings that should have variables interpolated. So:

    my $bar = 3; my $foo = "Bar is $bar"; print $foo, "\n";
    prints "Bar is 3".

    So maybe this rhyme can help:

    For literal strings, quote with Single. (Isn't this a silly jingle?) Interpolation uses Double. Forget this rule, you'll be in trouble.
    :)

    stephen

Re: Interpolating Nursery Rhyme
by hatter (Pilgrim) on Apr 27, 2002 at 21:32 UTC
    I fear these replies, it's like someone got the wrong impression about perl poetry. Yuckfoos poem seems most correct to me, perl knows what a number looks like, so no quotes necessary, but it shouldn't take much experimentation if you use the wrong kind of quotes for strings, as escape characters like \n and interpolated variables won't work if you use ' instead of " so if the output has the wrong things in, change it, and before long it'll become second nature.

    the hatter

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-26 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found