3 problems with perl.

N

nimrodna

1. , I'm having problem with balanced parenthesis. I found this code in
the net:
my $paren = qr/\(([^()]+|(??{ $paren }))*\)/x;
This code supposes to handle my problem.
I'm working with perl 5.8, and it won't compile.
The error message is: "Global symbol "$paren" requires explicit package
name at (re_eval 1) line 2.".

2. How can I Use constants and variables inside a substitute operator
(s///).

3. Manipulate matched chars. When matched a number I need to add the
same value to each matched chararcter.
 
B

Brian McCauley

nimrodna said:
Subject: 3 problems with perl.

Please put the subject of your post in the Subject of your post.

If you have 3 separate subjects, make 3 separate posts.
1. , I'm having problem with balanced parenthesis.

How is that relevant?
I found this code in
the net:
my $paren = qr/\(([^()]+|(??{ $paren }))*\)/x;
This code supposes to handle my problem.

Appart from the suprious my() this same code can be found in "perlre"
the section of the Perl on-line documentation that covers regular
expressions.
I'm working with perl 5.8, and it won't compile.
The error message is: "Global symbol "$paren" requires explicit package
name at (re_eval 1) line 2.".

That's right. Lexically scoped variable declarations apply from the
point after declaration statement to the end of the lexical scope. You
cannot refer to the variable _within_ the statement in which it is
declared.

Note also that (??{ ... }) blocks suffer from the same problem as
nested subroutines with respect to lexical variables. (See perldiag's
explaination of the "will not remain shared" warning).

Avoid this by using only package variables when communicating with
(??{...}) blocks.

local our $paren;
$paren = qr/\(([^()]+|(??{ $paren }))*\)/x;
2. How can I Use constants and variables inside a substitute operator
(s///).

Variables interpolate just like they do in double-qouted strings.

Constants are functions and can be interpolated as per the FAQ "How do
I expand function calls in a string?". Hmmm perhaps it's about time to
try again submitting a doc patch to change the unhelpful word "expand"
to "interpolate".
3. Manipulate matched chars. When matched a number I need to add the
same value to each matched chararcter.

Sorry, I can't understand what you mean by add a value to a character.

In Perl regular expressions you can use () to capture stuff into
special variables and you can then use those variables in expressions
to calculate values.
 
B

Brian McCauley

Brian said:
How is that relevant?

Sorry that's a bit confused. What I meant was that you were having a
problem with variable declarations and recursive pattern matches. The
problem that you were trying to solve with the recursive pattern is not
actually part of the problem.

Consider, if you will, an analogy. if your car breaks down on the way
to work you have a problem getting to work. But this is not relevant to
how you fix the car.
..
Correctly partitioning your problem is a very important first step to
soving it.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top