tags:

views:

58

answers:

3

I am writing Perl regex code to separate out PL/SQL procedure from the package.

Each procedure starts with the PROCEDURE key word and ends with END, But the END is for BEGIN, IF, or LOOP. There may be many BEGIN|IF|LOOP.

Below is the kind of input, I want to separate each procedure. How can I do this?

PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
BEGIN
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END;
END;




PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF....
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END IF
END;
+1  A: 

The easiest way to do this is to logically separate the text based on the occurrence of PROCEDURE:

undef $/;
my $line = <DATA>;
my @proc = split /(?=^PROCEDURE )/m, $line;
use Data::Dumper;
die Dumper \@proc;
Evan Carroll
A: 

For a quick and dirty solution looking for the token PROCEDURE could be sufficient for separating the code blocks. But if you want this to be a mature solution you should really try to find (or write) a parser for PL/SQL and use this instead. Otherwise you can't handle occurences of the token PROCEDURE inside of strings or comments.

tangens
+1  A: 

Try this :

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

undef $/;
my $line = <DATA>;
my @procedures = split /(?=PROCEDURE)/, $line;
print Dumper(\@procedures);

__DATA__
PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
BEGIN
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END;
END;




PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF....
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END IF
END;

Output:

$VAR1 = [
          'PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
BEGIN
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END;
END;




',
          'PROCEDURE LOG_ECS_MSG( MD_CURR  CHAR,
                     MSG_TIME DATE ) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF....
INSERT INTO INFO_SERV_ECS_LOG
VALUES ( MD_CURR, MD, SVTY, ACT_DATE, ACCT, MSG_TXT, MSG_TIME);
COMMIT;
END IF
END;'
        ];
M42
I've never done this before.. but you're six hours late to the *exactly* the same response that I've given.. Seriously..
Evan Carroll
That's true only if "exactly" means "similar but different". M42 keeps the PROCEDURE with the text because he uses a lookahead. This is much better than your solution.
brian d foy
@brain d foy, Very minor point easy to switch if needed. Troll somewhere else. You know very well that something so minor should have been a comment. And, now his remains bugged in that in it catches any PROCEDURE token even if it isn't the first word on the line.
Evan Carroll
@Evan Carroll: may be it's similar to yours, but i give a sample output that you couldn't do with your first response because it didn't work.
M42
It worked fine... It just cut PROCEDURE out.. not a big deal. By any means, that is why you can comment on my answer. Without submitting a totally different answer that you started by copying the code, and adding the 3 characters for look-ahead. The question asked something different, it was my idea to use the PROCEDURE token. Furthermore, you do this a lot.
Evan Carroll
I see now Evan has silently changed his solution to use what M42 did. That's what you get when different people work together instead of worrying about stupid, non-redeemable status points.
brian d foy
[Here is an example of him doing damn near the same thing to you!](http://stackoverflow.com/questions/3608201/regular-expression-for-nested-c-structs) Wow, he can join regexes!
Evan Carroll
How nice behaviour, how old are you?
M42
M42 didn't copy my answer. Our answers look similar now, but if anything, my edited answer looks more like his than his looked like mine. Between the time I answered and M42 answered, the OP changed the question. M42 answered with a correct solution for the new question, and I modified my answer much later. Check the edits. Facts are a bitch.
brian d foy