3 problems with perl.

Discussion in 'Perl Misc' started by nimrodna, Sep 2, 2006.

  1. nimrodna

    nimrodna Guest

    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.
     
    nimrodna, Sep 2, 2006
    #1
    1. Advertising

  2. nimrodna wrote:

    > 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.


    >Are you?


    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.
     
    Brian McCauley, Sep 2, 2006
    #2
    1. Advertising

  3. Brian McCauley wrote:
    > nimrodna wrote:
    > > 1. , I'm having problem with balanced parenthesis.

    >
    > >Are you?

    >
    > 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.
     
    Brian McCauley, Sep 2, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,815
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    308
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    693
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    301
    PerlFAQ Server
    Feb 27, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    697
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page