lyapunov has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: double quote vs single quote oddities. I need enlightenment
by kennethk (Abbot) on Jul 08, 2010 at 16:31 UTC | |
by lyapunov (Novice) on Jul 08, 2010 at 17:20 UTC | |
by kennethk (Abbot) on Jul 08, 2010 at 17:34 UTC | |
by lyapunov (Novice) on Jul 08, 2010 at 18:05 UTC | |
by furry_marmot (Pilgrim) on Jul 08, 2010 at 18:27 UTC | |
|
Re: double quote vs single quote oddities. I need enlightenment
by moritz (Cardinal) on Jul 08, 2010 at 16:33 UTC | |
by lyapunov (Novice) on Jul 08, 2010 at 17:28 UTC | |
|
Re: double quote vs single quote oddities. I need enlightenment
by JavaFan (Canon) on Jul 08, 2010 at 21:20 UTC |