From: clkao Date: Mon, 5 May 2008 08:37:19 +0000 (+0000) Subject: Make devel::declare parse the part between prototype and X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fd2ced7d81b4edbaf7aeed71e2b6873f91b64851;p=p5sagit%2FDevel-Declare.git Make devel::declare parse the part between prototype and sub body as traits and pass them to the callback. git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-Declare@4317 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/Declare.xs b/Declare.xs index 4b66c31..db6437b 100644 --- a/Declare.xs +++ b/Declare.xs @@ -8,6 +8,7 @@ #include #include +#define DD_HAS_TRAITS #if 0 #define DD_DEBUG #endif @@ -42,7 +43,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { char* save_s; char tmpbuf[sizeof PL_tokenbuf]; char found_name[sizeof PL_tokenbuf]; - char* found_proto = NULL; + char* found_proto = NULL, *found_traits = NULL; STRLEN len = 0; HV *stash; HV* is_declarator; @@ -156,6 +157,23 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { if (*s == '(') { /* found a prototype-ish thing */ save_s = s; s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */ +#ifdef DD_HAS_TRAITS + { + char *traitstart = s = skipspace(s); + + while (*s && *s != '{') ++s; + if (*s) { + int tlen = s - traitstart; + Newx(found_traits, tlen+1, char); + Copy(traitstart, found_traits, tlen, char); + found_traits[tlen] = 0; +#ifdef DD_DEBUG + printf("found traits..... (%s)\n", found_traits); +#endif + } + } +#endif + if (SvPOK(PL_lex_stuff)) { #ifdef DD_DEBUG printf("Found proto %s\n", SvPVX(PL_lex_stuff)); @@ -187,7 +205,8 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { cb_args[2] = HvNAME(PL_curstash); cb_args[3] = found_name; cb_args[4] = found_proto; - cb_args[5] = NULL; + cb_args[5] = found_traits; + cb_args[6] = NULL; if (len && found_proto) in_declare = 2; @@ -211,6 +230,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { const int old_len = SvCUR(PL_linestr); #ifdef DD_DEBUG printf("Got string %s\n", retstr); + printf("retstr len: %d, old_len %d\n", strlen(retstr), old_len); #endif SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr))); memmove(s+strlen(retstr), s, (PL_bufend - s)+1); diff --git a/lib/Devel/Declare.pm b/lib/Devel/Declare.pm index 96d3666..e3782ee 100644 --- a/lib/Devel/Declare.pm +++ b/lib/Devel/Declare.pm @@ -70,10 +70,10 @@ my $temp_name; my $temp_save; sub init_declare { - my ($usepack, $use, $inpack, $name, $proto) = @_; + my ($usepack, $use, $inpack, $name, $proto, $traits) = @_; my ($name_h, $XX_h, $extra_code) = $declarator_handlers{$usepack}{$use}->( - $usepack, $use, $inpack, $name, $proto, defined(wantarray) + $usepack, $use, $inpack, $name, $proto, defined(wantarray), $traits ); ($temp_name, $temp_save) = ([], []); if ($name) { @@ -165,8 +165,8 @@ sub setup_declarators { $setup_for_args{$name} = [ $flags, sub { - my ($usepack, $use, $inpack, $name, $proto, $shift_hashref) = @_; - my $extra_code = $compile->($name, $proto); + my ($usepack, $use, $inpack, $name, $proto, $shift_hashref, $traits) = @_; + my $extra_code = $compile->($name, $proto, $traits); my $main_handler = sub { shift if $shift_hashref; ("DONE", $run->($name, $proto, @_)); };