Okay, I've encountered one of those little code tidbits that make me want to just crawl under my desk and weep. Instead, I'm deferring to you guys in the hopes that one of you can explain this to me, since I really don't feel like taking my usual 2-3 days of extensive debugging and isolationism 'til I figure it out.
This is with perl 5.005_03.
my $data = "first line
second line
third line
last line";
my $data2 = $data;
print "Case 1:\n\n";
foreach (1..8){
my $line = undef; #lexical variable!
$line = $1 if $data2 =~ s/^([^\n]*)\n//; #assign to the lexica
+l
if (! defined $line && $data2 !~ /\n/){
$line = $data2;
$data2 = undef;
};
print "SPEC: ($line)($data2)\n";
};
print "\n=======\n\nCase 2:\n\n";
foreach (1..8){
my $line = $1 if $data =~ s/^([^\n]*)\n//; #make a lexical and
+ assign to it
if (! defined $line && $data !~ /\n/){
$line = $data;
$data = undef;
};
print "SPEC: ($line)($data)\n";
};
And here's the output:
Case 1:
SPEC: (first line)(second line
third line
last line)
SPEC: (second line)(third line
last line)
SPEC: (third line)(last line)
SPEC: (last line)()
SPEC: ()()
SPEC: ()()
SPEC: ()()
SPEC: ()()
=======
Case 2:
SPEC: (first line)(second line
third line
last line)
SPEC: (second line)(third line
last line)
SPEC: (third line)(last line)
SPEC: (last line)()
SPEC: (last line)()
SPEC: (last line)()
SPEC: (last line)()
SPEC: (last line)()
The first case is the "correct" one, meaning it works the way I want to/think it should. The second case is the "incorrect" one, meaning it doesn't work how I want it to, and I can't quite figure out why. The only difference between the two is where the variable $line is lexicalized.
Any ideas?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.