Dear PerlMonks; I recently created a bug (which I have corrected). During the course of fixing the bug I wanted to go down the most Righteous path of Self Learning. I found multiple answers. The problem is that after looking at this, I realize that I really don't understand the low level mechanics of how Perl is parsing and interpreting the code. To me, the results were unexpected. I implore you to shine your wisdom on this so I can be become a better programmer.

N.B. I started using a different syntax after reading the split section of the Functions Chapter in my most holy bible, Programming Perl 3rd edition, by Larry Wall. I picked up this syntax on page 18 of the same when I first learned Perl and used it ever since until recently.

I was writing a Nagios check that I wanted to test the timestamps of specific files to make sure log archiving successfully occurred. I have several files, per machine so I was using associative arrays to store the values based on machinename and file name e.g. machine1.messages, machine1.audit and so on. Naturally I wanted to split on the dot so that I could use them in the hashed arrays.

As I mentioned, I found the solution, but when I wrote a program to get insight into the mechanics of how this works, I came across results that were unexpected. At least they were to me.

Here is the code and the output.

#!/usr/bin/perl $foo="data1.txt"; (my $first, my $second)=split(".",$foo); printf("Using a double quoted . the answer is first $first, second $se +cond\n"); (my $first, my $second)=split("\.",$foo); printf("Using an escaped . the answer is first $first, second $second +\n"); (my $first, my $second)=split("\\.",$foo); printf("Using a double quoted, double escaped . the answer is first $f +irst, second $second\n"); (my $first, my $second)=split('.',$foo); printf("Using a single quoted . the answer is first $first, second $se +cond\n"); (my $first, my $second)=split('\.',$foo); printf("Using a single quote escaped . the answer is first $first, sec +ond $second\n"); (my $first, my $second)=split('\\.',$foo); printf("Using a single quote double escaped . the answer is first $fir +st, second $second\n"); (my $first, my $second)=split /./, $foo; printf("Using /./ in split the answer is first $first, second $second +\n"); (my $first, my $second)=split /\./, $foo; printf("Using /\\./ in split the answer is first $first, second $seco +nd\n"); (my $first, my $second)=split /\\./, $foo; printf("Using /\\\\./ in split the answer is first $first, second $se +cond\n");

Here is the output

Using a double quoted . the answer is first , second Using an escaped . the answer is first , second Using a double quoted, double escaped . the answer is first data1, sec +ond txt Using a single quoted . the answer is first , second Using a single quote escaped . the answer is first data1, second txt Using a single quote double escaped . the answer is first data1, secon +d txt Using /./ in split the answer is first , second Using /\./ in split the answer is first data1, second txt Using /\\./ in split the answer is first data1.txt, second

In short, my questions are:

1) Why doesn't split split on any printable character? In particular it looks like a dot should be passed in the examples one and two of the double quote examples.

2) While it makes sense that the double escaped dot will work for both the single and double quote versions. Why does the single escaped dot work for the single quote, but not the double quote?

3) What exactly is split splitting on in the last example?

Needless to say, there are some fundamental concepts that are eluding me. It has been a few years since I had compiler construction, but I will do my best to keep up with what answer you provide.

Again, thanks so much for your help. I really do want to understand what is happening here.


In reply to double quote vs single quote oddities. I need enlightenment by lyapunov

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.