[dancer-users] About Route Handlers

Andrew Solomon andrew at geekuni.com
Fri Apr 3 19:11:32 BST 2015


Hi Kadir

For test3 I think you were expecting the behaviour of this code

get '/test3/:name' => sub {
    "Hello there, " .
    (defined param('name') ? param('name') : "whoever you are!");
};

but you were getting the behaviour of this code

get '/test3/:name' => sub {
    ("Hello there, " .  defined param('name')) ?
      param('name') : "whoever you are!";
};

The reason for this is that '.' has higher operator precedence than '?:'

http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity

Hope that helps!

Andrew


On Fri, Apr 3, 2015 at 6:59 PM, D Perrett <perrettdl at googlemail.com> wrote:

> In perl, the value of the last statement executed in a code block is it's
> return value, an explicit return is only needed if you want to exit the
> chose block earlier than the last statement. Not sure why your third
> example isn't working for you. Have you restarted your dancer server since
> you edited the code?
>
> Daniel
> On 3 Apr 2015 18:30, "Kadir Beyazlı" <kadirbeyazli at gmail.com> wrote:
>
>> Hi All,
>>
>> I am novice at Dancer, I am sure my question is very easy for you but I
>> decided to ask because I failed at the beginning of my study.
>>
>> I started reading following manual :
>>
>> *https://metacpan.org/pod/Dancer2::Manual
>> <https://metacpan.org/pod/Dancer2::Manual>*
>>
>> I installed Dancer2, placked up it and opened web page from localhost.
>> Everything is OK until here.
>>
>> There is following info at manual:
>>
>> *The code block given to the route handler has to return a string which
>> will be used as the content to render to the client.*
>> It is clear for following example
>>
>>
>>
>>
>> *get '/test1/:name' => sub {    return "Hi there " . params->{name};};*
>> because it returns a string and when I write *http://localhost:5000/test1/kadir
>> <http://localhost:5000/test1/kadir> *to browser I see  *Hi there kadir*
>> which is the string I expect to see
>>
>> But next example is as follow:
>>
>>
>>
>>
>>
>> *get '/test2/:name' => sub {    "Hey ".param('name').", welcome here!";};*
>> It does not return anything. Because there is no *return* keyword
>> *.*
>> Despite this I see   *Hi there kadir  *when I write
>> *http://localhost:5000/test2/kadir <http://localhost:5000/test2/kadir>*
>> But above red background colored sentence says that it must return a value
>> *?*
>>
>> Next example is stranger
>>
>>
>>
>>
>>
>> *:get '/test3/:name' => sub {    "Hello there, " . defined
>> param('name')                    ? param('name')                    :
>> "whoever you are!";};*
>>
>> Again there is no *return* keyword. When I write *http://localhost:5000/test3/kadir
>> <http://localhost:5000/test3/kadir> *I see only kadir. But at test2
>> example, I saw all words despite there is no return keyword. So what is
>> rule?
>> *-- *
>>
>> *Kadir BeyazlıComputer Engineer*
>> *GSM : +90 535 821 50 00 <%2B90%20535%20821%2050%2000>*
>>
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>
>>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>


-- 
Andrew Solomon

Mentor at Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150403/601c7745/attachment.html>


More information about the dancer-users mailing list