1

So I have two variables

$x = q(foo);
$y = q(bar);

My goal is to use them in a third variable with an underscore between them i.e. foo_bar. There are lots of ways to do this, but I wanted to use qq

so

$z = qq($x_$y);

This gives the following error

Global symbol "$x_" requires explicit package name at test.pl line 45.
Execution of C:\test.pl aborted due to compilation errors.

So I had to use curly brackets with the variable x to make it work

$z = qq(${x}_$y);

Why does underscore not work with qq? Why do I need curly brackets in this case?

ontherocks
  • 1,747
  • 5
  • 26
  • 43
  • `qq($x_$y)` is exactly the same as `$x_ . $y`. You need curlies because that's not what you want. – ikegami Jun 23 '16 at 06:09
  • 1
    Re: `Why does underscore not work with qq?` It's not just `qq()`, the same issue you'll have with `"$x_$y";` for reason explained in answers. – mpapec Jun 23 '16 at 07:29

5 Answers5

7

That's because _ counts as a letter in identifiers (such as variable names).

When you write "$x_$y", Perl thinks you're trying to interpolate two variables, $x_ and $y. Similarly, when you write "$foo$bar", Perl thinks you're trying to interpolate $foo and $bar (not $f . 'oo' . $bar or $fo . 'o' . $bar or any other combination).

The general rule is: Names extend as far to the right as possible (that is, Perl chooses the longest possible interpretation for identifiers).

melpomene
  • 84,125
  • 8
  • 85
  • 148
4

The underscore _ is a legitimate character to use in variable names, so when you say "$x_$y" you are referring to (evaluating) a variable $x_, which clearly has not been defined.

See, for example, the accepted answer in this post for this and a lot more of this kind.

Community
  • 1
  • 1
zdim
  • 64,580
  • 5
  • 52
  • 81
  • 1
    Seriously, you are among the best of us here, and I will be pleased when your "score" matches your ability – Borodin Jun 23 '16 at 21:17
  • 1
    @Borodin Oh, uh, that is way too kind of you to say, thank you. (Now _that_ is embarrassing.) Now I need to work really, really hard for it! So that perhaps some years down the line it's somewhat true :). Thank you for helping all along the way. I'll tidy up now, all of this is personal, so OT – zdim Jun 23 '16 at 21:21
3

Because underscore may be part of the identifier. Otherwise, how parser should distinct $x from $x_ ?

So braces says that only x is identifer, not x_

2

If you want it your way, then you should just escape the underscore character.

use strict;
use warnings;

my $x =qq(foo);
my $y=qq(bar);
my $z = qq($x\_$y);

print $z;

Ofcourse I would like to suggest that this is not the better way. The curly brace way is the most better approach.

Nagaraju
  • 1,853
  • 2
  • 27
  • 46
1

According to perl variable naming rull 'underscore' can be a part of the variable name. ref : http://sivasakthikumar.blogspot.in/2009/03/variable-names-can-start-with-letter_16.html

That's why its not able understand your variable name properly.So you need brackets.

Arijit Panda
  • 1,581
  • 2
  • 17
  • 36