in reply to Re: double quote vs single quote oddities. I need enlightenment
in thread double quote vs single quote oddities. I need enlightenment

kennethk, thank you for the reply.

I do realize that . is a metacharacter. I did not know about the interpolation. So what you said makes sense for me up until the single quote doubled escaped . e.g. \\. The split function does split the string into data1 and txt Shouldn't it work like the last example where the first variable is the complete filename since the literal \. was not encountered?

Also, as . is any single printable character, why doesn't split bust the expression apart into NULL and ata1.txt? I did your trick with the join (thanks for that by the way) but split is apparently not returning anything

  • Comment on Re^2: double quote vs single quote oddities. I need enlightenment

Replies are listed 'Best First'.
Re^3: double quote vs single quote oddities. I need enlightenment
by kennethk (Abbot) on Jul 08, 2010 at 17:34 UTC
    So what you said makes sense for me up until the single quote doubled escaped

    For the expression split "\\.", 'data.txt', first Perl interpolates "\\." to the literal string [\.]. This string is then fed to the regular expression engine, making split "\\.", 'data.txt' equivalent to split /\./, 'data.txt'. The input is split on literal periods and then result is then ('data','txt').

    why doesn't split bust the expression apart into NULL and ata1.txt

    You would get your expected result if you include a limit on the expression, i.e. split /./, $foo, 2;. Unless an explicit limit is included, split will operate on every match. Therefore, the initial result of the split is a list of nine empty strings. As documented in split,

    empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)

    Therefore, it returns an empty list.

      Thanks again, I catch the second part now. I poorly phrased my first question. Let me take another stab at it.

      Both split('\.',$foo) and split('\\.',$foo) return two parts "data1" and "txt". Since they are not interpolated the first one working really make sense. The second is still elusive. I would think it would be equivalent then to split /\\./, $foo but that is split into data1.txt and nothing as the literal [\.] is not encountered e.g. a real backslash following by any printable character and not an escaped dot getting passed to the regex engine.

      Again, I am really trying not be a pest, but there is some subtlety that I am missing. I have gotten my head around everything except for this. Thanks!

        In non-interpolated strings, a double-backslash is interpreted as a single back slash.

        print '\\.', "\n", '\.', "\n"; ------ \. \.

        If you look at the docs for split, it specifically says it's expecting a /PATTERN/. So it interprets both '\\.' and '\.' as /\./.

        --marmot