Odd Double Colon Parsing in Perl

I came across something weird with parsing strings in Perl. Here is an 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 a double colon after a variable in a string, it (suppose) thinks you're trying to use the package name. I assume that he is trying to load "Hello :: test" and does not find anything - hence the early end of the line.

This is normal? I find this rather controversial. I was able to get around this by slipping away from the first colon like this:

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

Is this a mistake, or am I absolutely not seeing anything obvious?

+4
source share
3 answers

, , :

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

perldata:

, - ( ). , :

$who = "Larry";
print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n";
print "We use ${who}speak when ${who} here.\n";

Perl $whospeak, a $who::0 a $who's. $0 $s () who.

+8

, , , $word::test . , :: , $word, ( ).

Oesor - - ${identifier}. , , -, (.. ::).

+3

Perl , Perl, Perl :: ', , (, ). :

use strict;
use warnings;    # You're using these? Aren't you?

my $foo = "bar"
print "The magic word is $foo_for_you\n";

, Perl , , $foo_for_you, $foo _for_you ​​ . , ? :

print "The magic word is $foobar\n";   # Whoops! my variable is $foo.

, , $foo - :

print "The magic word is " . $foo . "_for_you\n";
printf "The magic word is %s_for_you\n", $foo;
print "The magic word is ${foo}_for_you\n";

, $foo::for::you ( $foo'for'you). Perl $you foo::for. , :

print "The magic word is " . $foo . "::for::you\n";
printf "The magic word is %d::for::you\n", $foo;
print "The magic word is ${foo}::for::you\n";

Namespaces are used to help keep variables in Perl modules from changing variables in your program. Imagine that you are calling a function in a Perl package and unexpectedly discover that the variable that you used in your program has changed.

Take a look at File :: Find and you will see the variables with the package namespace attached to them ( $File::Find::nameand $File::Find::dirtwo examples).

+1
source

Source: https://habr.com/ru/post/1545703/


All Articles