Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does perl translate a tick into a colon?

Tags:

perl

Given this program:

use strict;
use warnings;

my $zx_size = 32;
my $x = "$zx_size'h0";

Perl tells me this:

Name "zx_size::h0" used only once: possible typo at concat.pl line 7.
Use of uninitialized value $zx_size::h0 in string at concat.pl line 7.

Why?

It appears there are multiple ways to specify the namespace of a var. Are there others?

Is it possible to turn off this behavior?

like image 762
mmccoo Avatar asked Jul 01 '13 18:07

mmccoo


3 Answers

The old package delimiter was a single quote ' which was originally used to ease transition to Perl for Ada programmers. It was replaced with :: to be more readable and ease the transition of C++ programmers.

It is still supported for backwards compatibility, but you can wrap your scalar in {..} to have it interpolate correctly.

my $x = "${zx_size}'h0";

or

my $x = "$zx_size\'h0";
like image 121
Hunter McMillen Avatar answered Oct 20 '22 16:10

Hunter McMillen


The single quote character ' was used by earlier Perls to denote package scope in the same way as ::, and it is still supported for backwards compatibility.

To do what you want, you'll need to use the ${} syntax to tell Perl where the identifier ends and the literal begins:

"${zx_size}'h0"

Note that this is the same thing you would have to do if you wanted to the value of $zx_size followed by any other literal character that is legal to appear in an identifier:

"${zx_size}foo"
like image 11
Oktalist Avatar answered Oct 20 '22 16:10

Oktalist


Read perlmod. The very second paragraph. And no, there's no way to turn it off, at least for now. Theoretically, someone could write a no feature pragma for it, but it's been that way for 20 years without causing too many problems...

like image 4
hobbs Avatar answered Oct 20 '22 16:10

hobbs