Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

verbatim, non-interpolated assignment

by toro (Beadle)
on Sep 13, 2011 at 22:47 UTC ( [id://925780]=perlquestion: print w/replies, xml ) Need Help??

toro has asked for the wisdom of the Perl Monks concerning the following question:

Bonjour, moines.

I want to assign strings with arbitrary characters to a variable.

First I tried double quotes: my $tweet = "RT @peteyorn: @Starbucks Thanks for putting the Break Up album for sale in your stores. \\ It's a great album! Nice work!"

That interpolates @peteyorn as an array.

I wisened up and tried single quotes. But the assignment doesn't terminate; Perl runs through the ending ' on later lines. Next I tried q{  TEXT  }:

my $tweet = q{RT @peteyorn: @Starbucks Thanks for putting the Break Up album for sale in your stores. \\ It's a great album! Nice work!}

That prints \\ as \.

Is there a way to really, truly, literally, verbatim assign what's enclosed, no questions asked and no thoughts thunk on perl's part?

Salutations distinguées.
taureau

de plus: my $tweet is just a particularly pesky example so I am coding against it. In the final application I will be piping a .csv through @ARGV, so will this problem just go away? (Some of the proposed solutions make me think so.)

Replies are listed 'Best First'.
Re: verbatim, non-interpolated assignment
by chromatic (Archbishop) on Sep 13, 2011 at 23:33 UTC
    That interpolates @peteyorn as an array.

    In which version of Perl? I get an error with 5.14 as I expect, because the single-quote in It's looks like the end of string quote marker.

    Is there a way to really, truly, literally, verbatim assign what's enclosed, no questions asked and no thoughts thunk on perl's part?

    No. That's a semi-predicate problem and is unsolveable. You can read text via IO though.


    Improve your skills with Modern Perl: the free book.

      It's

      Oh. That was foolish of me not to notice. Thank you.

      That's a semi-predicate problem and is unsolveable.

      What is semi-predicate? It sounds theoretical.

      In which version of Perl?

      This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi

      You can read text via IO though.

      Is that what onelesd is talking about? Is it in man perlio?

        What is semi-predicate? It sounds theoretical.

        What is google? semi-predicate -> http://en.wikipedia.org/wiki/Semipredicate_problem

        Simply it means return undef to signal failure, but it can return undef as valid value, so you can't differentiate between failure or just a valid value

        It applies here because perl expects perl syntax, so you can't inline arbitrary text verbatim as part of a perl program, it could be a perl program, so you have to quote/escape, like

        my $asCloseAsYouCanGet = <<'__TO_VERBATIM__'; other stuff not that is not __TO_VERBATIM__ on a single line __TO_VERBATIM__

        The text in $asCloseAsYouCanGet cannot contain __TO_VERBATIM__ on a single line as input, because __TO_VERBATIM__ on a single line is like the closing quote, it signals the end of the input, so it can't be valid input

        In other words, the semi-predicate problem, it can't be both valid input and closing quote, it has to be one or the other, not both

        Its like pictorial depictions of numbers, the symbol for the number one, 1, cannot be the same symbol for the number two, 1 , because you cannot tell when it means one or it means two

        1 + 1 + 1 = 4? See the first 1 is really one, the second 1 is really two, and the last one is really 1 , which adds up to four

        :)

Re: verbatim, non-interpolated assignment
by onelesd (Pilgrim) on Sep 14, 2011 at 00:14 UTC

    Perhaps you can do it this way:

    use strict ; use warnings ; print $_ while (<DATA>) ; __DATA__ RT @peteyorn: @Starbucks Thanks for putting the Break Up album for sal +e in your stores. \\ It's a great album! Nice work!
      Thank you. Is the + something I should type in, or something incidental that came off your terminal?
        Is the + something I should type in, or something incidental that came off your terminal?

        That + is related to your Display Settings, specifically code wrapping. Do not retype code, or even copy & paste from the node itself. Use the download link, which is there for that very reason.

        HTH,

        planetscape
Re: verbatim, non-interpolated assignment
by Marshall (Canon) on Sep 14, 2011 at 02:49 UTC
    Perl has a lot of quoting mechanisms...
    See Quote-and-Quote-like-Operators

    q{} is same as '' which means literal, no interpolation.

    Update: Oh, I see what is going on....
    If you use ' for the quoting character to delimit the string, then you have to escape the ' characters within the string.
    The backslash character has a special meaning within q{}, but not within single quotes like: 'xxx'.
    Normally you will have to "escape" the \ or the ' characters. That is because these characters have meaning as to when the TEXT starts and stops.

    The alphabet of what is possible within the string is all possible characters. But the compiler has to know when the string starts and ends. In the program text (source code) there is no single character that definitively means: text starts and text stops, because all possible text characters can be part of the string.

    There is a defined syntax to write a program literal in the source code. If you are not happy with those requirements (have to "escape" some characters), then I would make a config file and put the strings in there. Then they can be read as simply data. At the end of the day, a program statement is different than a line in a data file.

    It is possible to use the HERE-DOC with a blank line terminator:

    my $tweet2 =<<''; RT @peteyorn: @Starbucks sale in your stores. \\ It's a great album! +Nice work!;<p> print "before print\n"; print $tweet2; print "after print\n";
    The RT @... @Starbuck... line appears verbatim, but you see even with that, we've made a special case, we stop when there is blank line. As an updated thought here...it is weird that: my $tweet2 =<<''; means stop on a blank line and I had to play around to verify what it did...<<'' does not mean "stop on no character".

    There are syntax limitations when writing program code.
    There are no limitations at all for characters in a data file.
    If you cannot abide by the syntax limitations for program code, then put the data in a data file. And use a program to read that data.

    Update as per OP's update:

    de plus: my $tweet is just a particularly pesky example so I am coding against it. In the final application I will be piping a .csv through @ARGV, so will this problem just go away? (Some of the proposed solutions make me think so.)

    Yes. Writing source code is different than reading data from a file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found