Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Perl script

  • 24-05-2011 9:37am
    #1
    Closed Accounts Posts: 4,235 ✭✭✭


    Hi all,

    Just wondering are there any perl people out there who can help with this. Basically this script is a subversion pre-commit hook script that I want to use to require / force users to put the product version into the comment field so we can trace what commits were used for each version etc...

    Anyway I've gotten it to a point where it requires the user to put in a comment of at least 7 characters (all our product versions are 7 chars) however I'm not certain how to force them to have it in a particular format. At present with this script they can put in any random 7 digits and commit. How do I change this script so that it requires them to put it into ABC-123 format.

    Any ideas?

    Script
    #!/usr/bin/perl

    # config section
    $minchars = 7;
    $svnlook = '/usr/bin/svnlook';

    #
    $repos = $ARGV[0];
    $txn = $ARGV[1];
    $comment = `$svnlook log -t "$txn" "$repos"`;
    chomp($comment);

    if ( length($comment) == 0 ) {
    print STDERR "Your commit has been blocked as you did not input a Product version id!";
    exit(1);
    }
    elsif ( length($comment) < $minchars ) {
    print STDERR "Comment must be in the format of ABC-123.";
    exit(1);
    }

    exit(0);
    ~


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Regular expressions! These are what Perl does best.

    http://perldoc.perl.org/perlretut.html


  • Closed Accounts Posts: 2,930 ✭✭✭COYW


    Years since I have written Perl but a regex will do the job alright.


  • Closed Accounts Posts: 4,235 ✭✭✭iregk


    Ok thanks lads.

    Not that I'm asking you to do it for me but for someone who is very inexperienced in perl (coding in general) can you point me in the right direction on this. I'm reading through the url posted up but most of it is latin to me!


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    OK, a regular expression is basically a little language all of it's own that allows to to describe a pattern. You can then compare the pattern that you've specified to any string variable in Perl and get a result telling you that it does or doesn't match, or providing an array of all of the matches.
    The pattern is also known as regular expression or typically as a regexp, for short.

    Think about it in terms of doing a search on a webpage. If I open to the search box in my browser and type in "boards.ie", it will show me everywhere on the page that "boards.ie" appears, by highlighting each occurence. If it doesn't appear, it tells me so. That's the essence of regular expressions - "Here is an expression, tell me whether it appears in the text and/or show me everywhere that it appears".

    Of course, you need pattern-matching to be more flexible than just taking in a static piece of text. If you're checking a variable to make sure it's an email address, for example, you have no way of checking the address against all possible addresses in the world. Instead you need to be able to specify what they text should look like. In this way, regexps use special symbols to denote groups of characters, as well as how often they appear.

    So for the email example, I would say that, "An email address looks like this: it starts with at least one alphanumeric character, and is followed by any number of other alphanumeric characters. Then it has an "@ symbol. Then it is followed again by at least one alphanumeric character, followed by any number of alphanumeric characters, followed by a dot .", followed by an alphanumeric domain name which is between 2 and 4 characters long.
    (Note this isn't entirely correct, as the name can also have things like dots and dashes in it, and technically speaking has a maximum length, just simplifying the example!)

    Regular Expressions allow us to dictate patterns, like the above, to the program so that it can check to see whether they match.

    In your example, you need to be able to tell your program, "A version number starts with any three uppercase characters, followed by a dash, followed by three numbers". Then you can ask it to check the piece of text and say yay or nay.

    I did have a look at that perldoc after I posted it and it's not exactly in the simplest of languages. But try reading it again now with the above in mind. When reading the code, the operator "=~" means "Looks like". And similarly "!~" means "does not look like".

    So in the first example, read this
    "Hello World" =~ "/World/"';
    
    As, 'Hello World' looks like 'World' (Yes it does)

    Try the wiki page as well to get a better understanding of them. http://en.wikipedia.org/wiki/Regular_Expressions

    Regular expressions are daunting and confusing at first, but ridiculously powerful when you learn how to use them.


  • Closed Accounts Posts: 8,199 ✭✭✭G-Money


    Bit of a perl newbie here myself. My first piece of advice would be to avoid using perl in the first place :)

    If not, then I think a regular expression should do the trick, as others have mentioned. I'm not sure about your script and whether it re-formats stuff that users have typed in, but if it does, then a regex is right up your street.

    They are incredibly hard to read especially at the start, but once you get the hang of them, they are grand. Just remember that in perl, to specify that you want to specify certain special characters (full stops, forward slashes, hyphon's, backslashes etc), you need to add a blacklash in front of it. Very confusing, especially when the character you want to remove is a backslash :)

    The perldocs are ok but I find that they come across as written by a computer, or someone who speaks in perl as well as programs in it.

    If you Google regex's, you will find the info. Once you get the syntax of how to construct a regular expression, it will slot into almost any language as far as I know. So if you don't find what you are looking for on perldoc, keep searching.

    My advice would be to start out with a simple example. Like removing the "abc" part of your string, just to get the hang of creating a basic regex. Then build on that.

    As I say, I'm a newbie too and I have no particularl love of perl (to put it mildly). However when I wanted to learn regex's, I just started with something simple like removing the leading backslashes from a file path and progressed from there.

    Good luck.


  • Advertisement
  • Closed Accounts Posts: 4,235 ✭✭✭iregk


    Thanks lads, I'll give it a good crack.


  • Registered Users, Registered Users 2 Posts: 297 ✭✭stesh


    Even though Perl's expressions are slightly-less-than-regular...


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    # config section
    my $minchars = 7;
    my $svnlook = '/usr/bin/svnlook';
    my $format=qr(^[A-Za-z]{3}-\d{3}$); # use one of these two lines
    #my $format=qr(^[A-Z]{3}-\d{3}$); #If alphabetic characters should be upper case
    #--------------------------------------------
    my $repos = $ARGV[0];
    ny $txn = $ARGV[1];
    my $comment = `$svnlook log -t "$txn" "$repos"`;
    chomp($comment);
    
    if ( length($comment) == 0 ) {
      print STDERR "Your commit has been blocked as you did not input a Product version id!";
      exit(1);
      }
    elsif ( ( length($comment) < $minchars )  || ($comment!~/$format/) ){
      print STDERR "Comment must be in the format  \"ABC-123\".";
      exit(1);
      }
    
    exit(0);
    


  • Closed Accounts Posts: 4,235 ✭✭✭iregk


    Super stuff thanks Skrynesaver.

    I got a very quick work around by putting in a grep statement in my $comment line.


Advertisement