Pater matching and backreference

J

Josef Moellers

Hi,

I am trying to escape spaces but apparently get into a naming conflict:

#! /usr/bin/perl

use warnings;
use strict;

open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
while (<OGG>) {
last if /^User comments section follows/;
}
while (<OGG>) {
chomp;
if (/^\s+title=(.*)/) {
my $title = $1;
$title =~ s/\s/\\$1/g;
print " --tt $title";
next;
}
if (/^\s+artist=(.*)/) {
my $artist = $1;
$artist =~ s/\s/\\$1/g;
print " --ta $artist";
next;
}
}
close OGG;
exit 0;

If the artist or title name contains spaces, I get the error message
Use of uninitialized value in concatenation (.) or string at ./xinfo
line 14, <OGG> line 14.
once for each space.
I assume this is due to the fact that $1 is defined when the substitute
is compiled.

How can I do the substiture?

Josef
 
A

Anno Siegel

Josef Moellers said:
Hi,

I am trying to escape spaces but apparently get into a naming conflict:

#! /usr/bin/perl

use warnings;
use strict;

open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
while (<OGG>) {
last if /^User comments section follows/;
}
while (<OGG>) {
chomp;
if (/^\s+title=(.*)/) {
my $title = $1;
$title =~ s/\s/\\$1/g;

$title =~ s/(\s)/\\$1/g;

You forgot to capture.
print " --tt $title";
next;
}
}
close OGG;
exit 0;

If the artist or title name contains spaces, I get the error message
Use of uninitialized value in concatenation (.) or string at ./xinfo
line 14, <OGG> line 14.
once for each space.

Anno
 
J

Josef Moellers

Anno said:
$title =~ s/(\s)/\\$1/g;

You forgot to capture.

Indeed, thanks. I apparently mixed $& with $1:
$title =~ s/\s/\\$&/g;
would have worked, too (TMTOWTDI).

Thanks again,

Josef
 
T

Tassilo v. Parseval

Also sprach Josef Moellers:
I am trying to escape spaces but apparently get into a naming conflict:

#! /usr/bin/perl

use warnings;
use strict;

open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
while (<OGG>) {
last if /^User comments section follows/;
}
while (<OGG>) {
chomp;
if (/^\s+title=(.*)/) {
my $title = $1;
$title =~ s/\s/\\$1/g;

During this match the previous value of $1 (the one you saved in $title)
is being reset because the match succeeds. But since you don't capture
any of the matches, you end up with undef in $1. As I understand you,
you want to escape all spaces in $title. In order to refer to the found
spaces with $1, you have to capture them:

$title =~ s/(\s)/\\$1/g;

[...]
If the artist or title name contains spaces, I get the error message
Use of uninitialized value in concatenation (.) or string at ./xinfo
line 14, <OGG> line 14.
once for each space.
I assume this is due to the fact that $1 is defined when the substitute
is compiled.

No, $1 is defined during matching time. Once a pattern match succeeds
previous values of ${1|2|...} are lost. They remain their old value only
if the new match fails. This happens regardless of whether your second
match is capturing or not.

Tassilo
 
B

Bart Lateur

Josef said:
Indeed, thanks. I apparently mixed $& with $1:
$title =~ s/\s/\\$&/g;
would have worked, too (TMTOWTDI).

Except use of $& would slow *every* regex in your script, and in all
modules, down.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top