title: what is the best way to hanlde optional url arguments in a Catalyst controller?
body: <p>For example:</p>
  
  <p>I know how to match <strong>www.domain.com/foo/21</strong></p>
  
  <pre><code>sub foo : Path('/foo') Args(1) {
    my ( $self, $c, $foo_id ) = @_;
    # do stuff with foo
  }
  </code></pre>
  
  <p>But how can I match <strong>www.domain.com/foo/21</strong> OR <strong>www.domain.com/foo/21/bar/56</strong> ?</p>
  
  <pre><code>sub foo : &lt;?&gt; {
    my ( $self, $c, $foo_id, $bar_id ) = @_;
    # do stuff with foo, and maybe do some things with bar if present
  }
  </code></pre>
  
  <p>Thanks</p>
  
  <p><strong>Update:</strong>
  Following Daxim's suggestion, I tried to use :Regex</p>
  
  <pre><code>sub foo : Regex('foo/(.+?)(?:/bar/(.+))?') {
     my ( $self, $c ) = @_;
     my ( $foo_id, $bar_id ) = @{ $c-&gt;req-&gt;captures };
  }
  </code></pre>
  
  <p>But this doesn't seem to work; the url is matched, but $bar_id is always undef. If I remove the optional opperator from the end of the regex then it does capture $bar_id correctly, but then both foo and bar must be present to get a url match. I'm not sure if this is a perl regex issue, or a Catalyst issue. Any ideas?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>As Daxim points out, its a regex issue. I can't see why the above regex doesn't work, but I did manage to find one that does:</p>
  
  <pre><code>sub foo : Regex('foo/([^/]+)(?:/bar/([^/]+))?') {
     my ( $self, $c ) = @_;
     my ( $foo_id, $bar_id ) = @{ $c-&gt;req-&gt;captures };
  }
  </code></pre>
  
  <p>(I didn't use \d+ in the captures like Daxim did as my ids might not be numeric)</p>
  
  <p>Thanks all for the help and suggestions, I learnt a lot about handling urls in Catalyst :D</p>