l0b0 l0b0 - 1 year ago 79
Perl Question

Return only matches from substitution in Perl 5.8.8 (was: Perl "p" regex modifier equivalent)

I've got a script (source) to parse

svn info
to create a suitable string for Bash's
. Unfortunately this doesn't work on one system I'm using which is running Perl 5.8.8 - It outputs all lines instead of only the matches. What would be the Perl 5.8.8 equivalent to the following?

local result=$(
svn info 2>/dev/null | \
perl -pe 's;^URL: .*?/((trunk)|(branches|tags)/([^/]*)).*;\2\4 ;p')
if [ -n "$result" ]
printf "${1:- (%s)}" $result

The output from Perl 5.10 contains only a space, parenthesis, one of branch name, tag name or
, and the end parenthesis. The output from Perl 5.8.8 (without the final
) contains this plus a parenthesized version of each space-separated part of the
svn info

A possible workaround involves a simple
grep '^URL: '
between the
commands, but I was hoping to avoid that since this will be executed for each Bash prompt.

Answer Source

If you only want output from a line that matches, don't use the -p command-line switch. It prints the value of $_ at the end of each loop. You might want something with the -n command-line switch:

 perl -ne 'print if s/.../.../'

I'd do it in the same way for Perl v5.8 and v5.10. I'm not sure what you think the /p modifier is doing since you don't use the $`, $&, or $' variables or their per-match equivalents.

You can read about the command-line switches in perlrun.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download