Tuesday, 15 June 2010

Odd string parsing with double colons in Perl -



Odd string parsing with double colons in Perl -

i've stumbled upon odd perl string parsing. here's example:

$word = "hello"; $test1 = "colon:values::$word:test"; $test2 = "colon:values::$word::test"; // test1 prints: "colon:values::hello:test" // test2 prints: "colon:values::"

so if perl sees double colon after variable in string, (i assume) think you're trying utilize bundle name. guess it's trying load "hello::test" , not finding - hence termination of string.

is normal? find pretty counter-intuitive. able work around escaping first colon so:

$works = "colon:values::$word\::test";

is bug or missing obvious?

while works, more widespread way handle case disambiguate name of variable braces:

perl -e "my $word = 'red'; qq|colon::values::${word}::test|" colon::values::red::test

this documented in perldata:

as in shells, can enclose variable name in braces disambiguate next alphanumerics (and underscores). must when interpolating variable string separate variable name next double-colon or apostrophe, since these otherwise treated bundle separator:

$who = "larry"; print passwd "${who}::0:0:superuser:/:/bin/perl\n"; print "we utilize ${who}speak when ${who}'s here.\n";

without braces, perl have looked $whospeak, $who::0, , $who's variable. lastly 2 $0 , $s variables in (presumably) non-existent bundle who.

string perl parsing colon

No comments:

Post a Comment