On 12/19/07, Jin Zhisong <[EMAIL PROTECTED]> wrote:

>    next  unless /\s+BCV\s+N/Grp/;  it works

Oh, I hope it doesn't work. The number of forward slashes is all wrong.

>   next  unless /\s+BCV\s+N\/Grp/;  it didn't work

But this one might actually parse. If it doesn't match when it's
supposed to, and fail when it's supposed to, that's another problem.
(Does it matter that no line in your sample data has both "BCV" and
"N/Grp"?)

> 1.  what is the proper way to use \ within regex?

If you need to use the quoting character ("/" in your pattern above)
as an actual literal character in the pattern, you need to backslash
it, as you did in your second pattern above. That tells Perl that this
isn't the end-of-pattern delimiter, this is a literal character. But
because patterns like this:

    /^\/usr\/bin\//

... are hard to read and write, Perl lets you choose a different
punctuation mark than the forward slash as a quoting character. In
exchange, you have to warn Perl that you want a pattern match by
putting the letter m in front of your pattern. That ugly pattern above
might thus become:

    m#^/usr/bin/#

... if you choose the #-sign as a delimiter.

On the other hand, the backslash character is always special in Perl.
If you mean a literal backslash character within a string or a
pattern, you always need to use two of them.

> 2.  can I use $match within regex , sun as / $match ../, should I quote
> ' or "" for $match ?

I'm not completely sure what you're asking, but I think you want to
know about building a pattern at run-time by interpolating a variable
into a pattern. Yes, you can interpolate a variable such as $match
into a pattern; you'll generally want the qr// operator to quote the
pieces you use to make $match.

    http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators

> 3.  can I use ${match} to make it more readiable?

If you think it's more readable, then you should be able to use it.
But maybe you really want to add the /x flag to your pattern, and
write it so that it's _really_ readable.

While you're still developing your program and trying to get your
pattern to match, you don't need to keep running your external
program. Run it once, save the output to a file, then have your
program read that file. That makes it easier to step through your code
with the debugger, and it's better for testing because the data
doesn't change from one run to the next.

In fact, it would be arguably better to design your program so that it
can work on either a given data file or the external program's output.
That would allow you to easily make a test program that checks that
your program behaves correctly when given known test data, so you
could have more confidence in its behavior when its data comes from
the external program.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to