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

Most of your questions seem to stem from an expectation that split works by finding a string literal you pass it. While this expectation is correct for cases like split 'x', 'axbxc';, split actually works based upon regular expressions. Regular expressions use several metacharacters, one of which is . - it a wildcard. You may gain some illumination by testing your expressions in code similar to:

print join "_|_", split /./, $foo;

To answer more on point for your questions:

  1. As previously stated, . is a regular expression metacharacter. To split on literal periods, use the regular expression /\./ or use the \Q \E combo (e.g. split /\Q.\E/, $foo;) to handle your escaping for you - see Quote and Quote like Operators.
  2. Double quotes are interpolated by Perl and single quotes are not - again, see Quote and Quote like Operators. In your case, double quotes apply escaping to backslashed characters. This means "\\." is equivalent to '\.' and "\." is equivalent to '.'. In particular, backslash is an ordinary character in single quotes unless followed by another backslash or by a single quote.
  3. In the last example, the regular expression engine is looking for a literal backslash followed by any character.

If any of this is unclear, I would be happy to expound further.

Replies are listed 'Best First'.
Re^2: double quote vs single quote oddities. I need enlightenment
by lyapunov (Novice) on Jul 08, 2010 at 17:20 UTC

    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

      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!