<div dir="ltr"><div><div><div><div><div>Hi Perrett && Andrew,<br><br></div>Thanks for your reply,<br><br></div>@Perrett,<br><br><span style="background-color:rgb(255,255,255)"><i>In perl, the value of the last statement executed in a code block is it's return value</i><br><br></span></div>I surprised very much.<br></div>At following code, I thought it would print '3', but it writes '4' which means that you are right.<br></div><br><div><span style="background-color:rgb(255,0,0)"></span><span style="background-color:rgb(255,255,0)"><span style="background-color:rgb(255,0,0)"><i><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">my $var = '3';<br>$var = &test; <br><br>print $var; # prints 3 not 4 although there is no return at sub test<br> <br>sub test {<br> "4";<br>}</span></span><br></i></span></span><br><span style="background-color:rgb(255,255,0)"><span style="background-color:rgb(255,255,255)">Althouh I worked with Perl CGI for 7 years I realized now, this is bad for me!<span style> Until today, I always wrote 'return 4'</span><span style></span> for following example thinking that Perl behaves lile C code</span><br><br></span></div><div><span style="background-color:rgb(255,255,0)"><span style="background-color:rgb(255,255,255)">@Andrew</span><br><br></span></div><div><span style="background-color:rgb(255,255,255)">You are right too. But I did not realize that there is '.' instead of '?'<br><br></span></div><div><span style="background-color:rgb(255,255,0)"><span style="background-color:rgb(255,255,255)">But I think most people reading manual may not realize this because code is from manual so if it was written by mistake, I think it should be corrected<br></span><i><br></i></span><pre><div class=""><div class=""><i><code class="">get </code><code class="">'/hello/:name?'</code> <code class="">=> </code><code class="">sub</code> <code class="">{</code></i></div><div class=""><i><code class=""> </code><code class="">"Hello there, "</code> <code class="">. </code><code class="">defined</code> <code class="">param(</code><code class="">'name'</code><code class="">)</code></i></div><div class=""><i><code class=""> </code><code class="">? param(</code><code class="">'name'</code><code class="">)</code></i></div><div class=""><i><code class=""> </code><code class="">: </code><code class="">"whoever you are!"</code><code class="">;</code></i></div><div class=""><i><code class="">};</code></i></div></div></pre><br></div><div><br><div><div><div><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 3, 2015 at 9:11 PM, Andrew Solomon <span dir="ltr"><<a href="mailto:andrew@geekuni.com" target="_blank">andrew@geekuni.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Kadir<div><br></div><div>For test3 I think you were expecting the behaviour of this code</div><div><br></div><div><span class=""><div>get '/test3/:name' => sub {</div><div> "Hello there, " .</div></span><div> (defined param('name') ? param('name') : "whoever you are!");</div><div>};</div><div><br></div><div>but you were getting the behaviour of this code</div><span class=""><div><br></div><div>get '/test3/:name' => sub {</div></span><div> ("Hello there, " . defined param('name')) ?</div><span class=""><div> param('name') : "whoever you are!";</div><div>};</div><div><br></div></span></div><div>The reason for this is that '.' has higher operator precedence than '?:'</div><div><br></div><div><a href="http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity" target="_blank">http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity</a><br></div><div><br></div><div>Hope that helps!</div><div><br></div><div>Andrew</div><div><br></div></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On Fri, Apr 3, 2015 at 6:59 PM, D Perrett <span dir="ltr"><<a href="mailto:perrettdl@googlemail.com" target="_blank">perrettdl@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">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?</p>
<p dir="ltr">Daniel</p>
<div class="gmail_quote"><div><div>On 3 Apr 2015 18:30, "Kadir Beyazlı" <<a href="mailto:kadirbeyazli@gmail.com" target="_blank">kadirbeyazli@gmail.com</a>> wrote:<br type="attribution"></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div><div>Hi All,<br><br></div>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.<br><br></div>I started reading following manual :<br clear="all"><div><div><div><br><i><a href="https://metacpan.org/pod/Dancer2::Manual" target="_blank">https://metacpan.org/pod/Dancer2::Manual</a></i><br><br></div><div>I installed Dancer2, placked up it and opened web page from localhost.<br></div><div>Everything is OK until here.<br><br></div><div>There is following info at manual:<i><br></i><p><span style="background-color:rgb(255,255,0)"><i>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.</i></span></p>It is clear for following example <br><br><i>get '/test1/:name' => sub {<br> return "Hi there " . params->{name};<br>};<br></i><br>because it returns a string and when I write <i><a href="http://localhost:5000/test1/kadir" target="_blank">http://localhost:5000/test1/kadir</a> </i>to browser I see <i>Hi there kadir</i> which is the string I expect to see<br><br></div><div>But next example is as follow:<br><br><i>get '/test2/:name' => sub {<br> "Hey ".param('name').", welcome here!";<br>};<br><br></i></div><div>It does not return anything. Because there is no <i>return</i> keyword<i>.<br></i></div><div>Despite this I see <i>Hi there kadir </i>when I write <i><a href="http://localhost:5000/test2/kadir" target="_blank">http://localhost:5000/test2/kadir</a><br></i></div><div>But above red background colored sentence says that it must return a value<i>?<br></i></div><div><i><br></i></div><div>Next example is stranger<i>:<br><br>get '/test3/:name' => sub {<br> "Hello there, " . defined param('name')<br> ? param('name')<br> : "whoever you are!";<br>};</i><i><br></i></div><div><i><br></i></div><div>Again there is no <i>return</i> keyword. When I write <i><a href="http://localhost:5000/test3/kadir" target="_blank">http://localhost:5000/test3/kadir</a> </i>I see only kadir. But at test2 example, I saw all words despite there is no return keyword. So what is rule?<i><br></i></div><div><i>-- </i><br><div><div dir="ltr"><div><b><i>Kadir Beyazlı<br>Computer Engineer</i></b></div><div><b><i>GSM : <a href="tel:%2B90%20535%20821%2050%2000" value="+905358215000" target="_blank">+90 535 821 50 00</a></i></b><br></div></div></div>
</div></div></div></div>
<br></div></div>_______________________________________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm" target="_blank">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a><br>
<br></blockquote></div>
<br>_______________________________________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm" target="_blank">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div><div dir="ltr">Andrew Solomon<div><br></div><div>Mentor@Geekuni <a href="http://geekuni.com/" target="_blank">http://geekuni.com/</a></div><div><a href="http://www.linkedin.com/in/asolomon" target="_blank">http://www.linkedin.com/in/asolomon</a><br></div></div></div>
</font></span></div>
<br>_______________________________________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><b><i>Kadir Beyazlı<br>Computer Engineer</i></b></div><div><b><i>GSM : +90 535 821 50 00</i></b><br></div></div></div>
</div>