-2

I have the following regex:

my ($pkg) = ( $xmldirname =~ /$db(.*)^/ );

What it should do is: check if the xmldirname starts ($) with "db" and take the rest of the string into $pkg.

What it does is: "Global symbol $db requires explicit package name".

How do I do this? Using qr// didn't work, either.

Yes, ok, I see. $ is end, not start. I'm sorry....

jackthehipster
  • 978
  • 8
  • 26

3 Answers3

2

You can escape $ using backslash (\) so that it loses its special meaning:

my ($pkg) = ( $xmldirname =~ /\$db(.*)^/ );

Alternatively, you can specify arbitrary delimiter for m (in fact, any quote-like operator) operator, and using single quote (') disables string interpolation:

my ($pkg) = ( $xmldirname =~ m'$db(.*)^' );

See Quote and Quote-like Operators

1
  • You can escape the $

  • Add the anchor ^ at the begining of the regex and not at the end

The code can be

my ($pkg) = ( $xmldirname =~ /^\$db(.*)/ );

Test

$xmldirname = '$dbhello';
my ($pkg) = ( $xmldirname =~ /^\$db(.*)/ );
print $pkg;

will give output as

hello
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0

It seems to me that you misused the anchor.

What it should do is: check if the xmldirname starts ($) with "db" and take the rest of the string into $pkg.

Use this:

my ($pkg) = ( $xmldirname =~ /^db(.*)$/ );

You could also remove the terminal $:

my ($pkg) = ( $xmldirname =~ /^db(.*)/ );
Toto
  • 89,455
  • 62
  • 89
  • 125