Hello,
At our recent Boston.pm social meeting, I mentioned this particular feature of Perl regex's to a few people, and they didn't know about it. I learned about it somewhere in a book. (My recollection is that it is not documented under regex (or regexp?). A pattern I use mostly is to embed other commands into bourne/korn/bash shell functions. (Typically I write a ~/.bc_bashrc file which I source to get my favorite commands into my environment.) Here is a shell function which shows using Perl (in i's "awk" (-n) mode) to write all the matching text between tow patterns.:

[EMAIL PROTECTED] bclancy]$ showfunc showfunc
showfunc ()
{
    set | perl -ne 'print if m#^'$*' \(\)# .. m#^}$#'
}
[EMAIL PROTECTED] bclancy]$

Until I saw this Perl pattern shown above, I used to use awk, and set a variable upon matching the first regex, and clearing a variable upon seeing the second regex, then printing lines when the variable was set. For example:

[EMAIL PROTECTED] CTS4.run.2008-06-27T13_58_45,500]$ showfunc awkfunc
awkfunc ()
{
    set | awk 'BEGIN {p=0}; /^'$*' ()/{p=1}; (p); /^}$/{p=0}'
}
[EMAIL PROTECTED] CTS4.run.2008-06-27T13_58_45,500]$

For me, the Perl version seemed much simpler. For those curious, I use the bash "set" command because it pretty prints the set functions in a consistent manner that I can count on, and because many files might have been sourced to arrive at the functions stored in the shell, and using the output of the set command allows me to capture them all. (In fact, I don't have an "awkfunc()" function in my ~/.bc_bashrc file. I just wrote it on the command line before running the second command above.)

I hope sharing the above pattern is useful to soneone else. I'm also curious whether any of you regularly do things like this, and if you have variants on the above pattern that you find practical and that you use regularly.

regards.
--
Bob

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to