Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Using grep on windows

by rogermills (Initiate)
on Oct 27, 2009 at 16:56 UTC ( #803470=perlquestion: print w/replies, xml ) Need Help??
rogermills has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I am new to perl so bear with me. I am writing a subversion hook script in perl. The script will check the log message at commit and get the issue ID (e.g. CNR12345) from the message, if the issue ID is not found in the log message it will block the commit else move on. Where i am stuck is how to grep for the issue ID (using perl on Windows). Even in the log message when there is a ID the grep fails to capture it.
use strict; use warnings; use POSIX qw( strftime ); ## Global variable my $svnlook = "D:\\Subversion\\bin\\svnlook.exe"; my $repo_path = $ARGV[ 0 ]; my $txn_name = $ARGV[ 1 ]; my $argNum = 0; my $committer = ""; my $commitlog = ""; my @log = ""; my $cnr_number = ""; # get committer $committer = "$svnlook author $txn_name $repo_path" or die "Unable to +get committer with svnlook.\n"; chomp($committer); #Get commit log $commitlog = "$svnlook log $txn_name $repo_path" or die "Unable to get + log message with svnlook.\n"; chomp($commitlog); # get CNR number @log = ($commitlog); $cnr_number = (grep(/"^CNR[0-9]"/, @log)); if ($cnr_number eq "") { print "You must enter a valid CNR"; exit1; }
Thanks a lot Roger

Replies are listed 'Best First'.
Re: Using grep on windows
by ikegami (Pope) on Oct 27, 2009 at 17:18 UTC

    You've got a number of major problems.

    • You never execute snvlook, for starters.

    • @log will even only contain one string, but I suspect you want it to contain a number of strings.

    • grep returns the number of matches in scalar context, not the list of matched items.

    • If you did evaluate grep in list context, it would return the whole item that matched (the whole line, I presume), not just the CNR part.

    • It's very hard to match a quote (") before the start of the string (^).

    • You say your local variables are global. Expanding the scope of your variables is backwards. You want to take steps to limit the scope of your variable (by postponing declaring them until you need them, for starters).


    use strict; use warnings; my ($repo_path, $txn_name) = @ARGV; my $svnlook = "D:\\Subversion\\bin\\svnlook.exe"; my $committer = `$svnlook author $txn_name $repo_path` or die("Unable to get committer with svnlook.\n"); chomp($committer); my $commitlog = `$svnlook log $txn_name $repo_path` or die("Unable to get log message with svnlook.\n"); my ($cnr_number) = $commitlog =~ /^(CNR[0-9]+)/m or die("You must enter a valid CNR\n");
      Well now i know i have a long way to go..... Now the problem is all commits are getting rejecting even those which have a valid issue number at the start of the log message. Like CNR12345
        Is "CNR12345" at the very start of a line? That's what my regex pattern checks for.
Re: Using grep on windows
by Fletch (Chancellor) on Oct 27, 2009 at 17:09 UTC

    Your regexp "^CNR[0-9]" can never match as-is since you'll never have a beginning-of-line immediately following a ". You probably want to either drop the quotes (if the token always begins at the beginning of a line), or drop the caret (if the token always occurs in quotes). Not to mention \d is more concise than [0-9]

    Update: Pedantry about [0-9] vice \d noted; suggestion withdrawn.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Not to mention \d is more concise than [0-9]
      And . is even more concise.

      \d and [0-9] are not equivalent. The latter matches 10 characters, the former several hundreds.

        In general, yes, \d can match many more than 10 characters. In the above program, no. Even if it did, it wouldn't be a biggie.

        However, since it's being used for validation here, [0-9] is definitely way to go in my opinion.

        \d and 0-9 are not equivalent - you sure of that ?

        From Using character classes we see:

        \d is a digit and represents 1. 0-9

        A user level that continues to overstate my experience :-))
Re: Using grep on windows
by Bloodnok (Vicar) on Oct 27, 2009 at 17:27 UTC
    Further to Fletchs most pertinent observations, you may want to swap the " & ^ chars i.e. leading to regex: /^"CNR\d"/ - but that wouldn't make sense for numbers > 9, so I suspect the regex ought to be /^"CNR\d+"/ assuming the 'issue' appears (when reported) to be of the form CNR99....

    Moreover, I'll bet that perl doesn't like exit1; - I think you meant exit 1;.

    Also, I've just noticed that both $committer & $commitlog only define strings - neither causes a command to run in the shell - you need backticks or qx to do that hence $committer should be set using

    $committer = `$svnlook author $txn_name $repo_path` or die "Unable to +get committer with svnlook.\n";
    Similarly with setting of $commitlog...

    A user level that continues to overstate my experience :-))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803470]
Approved by Perlbotics
Discipulus thanks back and passes around a weis bier in the berghastof hummelei celebrative glass

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-05-25 17:01 GMT
Find Nodes?
    Voting Booth?