fixed Local in MyApp.pm
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 41a5049..d09d2d9 100644 (file)
@@ -116,16 +116,28 @@ sub dispatch {
     my $results = $c->get_action( $action, $default );
     $namespace ||= '/';
     if ( @{$results} ) {
-        for my $begin ( @{ $c->get_action( 'begin', $namespace ) } ) {
+
+        # Execute last begin
+        $c->state(1);
+        if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
             $c->execute( @{ $begin->[0] } );
         }
-        if ( my $action = $c->req->action ) {
-            for my $result ( @{ $c->get_action( $action, $default ) }[-1] ) {
+
+        # Execute the auto chain
+        for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
+            $c->execute( @{ $auto->[0] } );
+            last unless $c->state;
+        }
+
+        # Execute the action or last default
+        if ( ( my $action = $c->req->action ) && $c->state ) {
+            if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
                 $c->execute( @{ $result->[0] } );
-                last unless $default;
             }
         }
-        for my $end ( reverse @{ $c->get_action( 'end', $namespace ) } ) {
+
+        # Execute last end
+        if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
             $c->execute( @{ $end->[0] } );
         }
     }
@@ -775,7 +787,7 @@ sub set_action {
             $absolute = 1;
         }
         $absolute = 1 if $flags{global};
-        my $name = $absolute ? $path : "$prefix/$path";
+        my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
         $c->actions->{plain}->{$name} = [ $namespace, $code ];
     }
     if ( my $regex = $flags{regex} ) {