Hi all, am I the only one who struggles with this? Maurice package dancer_test; use Dancer ':syntax'; our $VERSION = '0.1'; get '/1' => sub { #You put all the code here, #collect all the output and then just return it #via template or as html etc. return "I wanna hold your hand"; #works }; get '/2' => sub { #you don't even have to write a return "I wanna hold your hand"; #works too #let's call it a "magic return" since there is no return #seems to be a feature of perl not of dancer }; get '/3' => sub { #works only as long as you don't have anything after the return value "I wanna hold your hand"; #does NOT work my $i; }; get '/4' => sub { #sometimes Dancer displays the last variable (magic return) my $out="I wanna hold your hand"; }; get '/5' => sub { #return values from a sub five(); #I find this one a bit spooky }; sub five{ #this outputs the content of $out although no return anywhere #so we might call it "magic return in a sub" my $out="I wanna hold your hand"; } get '/6' => sub { six(); #returns 1 for success, not the content of $out }; sub six{ #this outputs the content of $out although no return anywhere my $out="I wanna hold your hand"; debug "I get here of course"; } get '/7' => sub { seven(); }; sub seven { #this seems more proper than six my $out="I wanna hold your hand"; return $out; debug "I never get here of course"; } get '/8' => sub { eight(); # does not output anything eight(); # does output once #I find this one a bit unintuitive }; sub eight{ #same as no. 5 my $out="I wanna hold your hand"; } get '/81' => sub { eight(); # does not output anything eight(); # does not output anything debug "Does output a one for success"; #I find this one a bit unintuitive }; get '/9' => sub { #if you actually want the return value twice in your output my $r=nine(); $r.=nine(); }; sub nine { #I consider this good style return "testing seven"; } get '/10' => sub { #works just as 9 my $r=ten(); $r.=ten(); }; sub ten { #as 9 but with magic return value "testing seven"; } get '/11' => sub { #i want to stop if any errors are returned eleven_one(); #does not display anything eleven_two(); #displays "I don't ever want to get here"; }; sub eleven_zero{} sub eleven_one{ #do this and that #if I encounter an error #if I don't encounter an error I return #nothing like in eleven_zero my $error="code 101"; return $error; } sub eleven_two{ return "I don't ever want to get here"; } get '/11b' => sub { #result is as in 11 eleven_three(); #does not display anything eleven_two(); #displays "I don't ever want to get here"; }; sub eleven_three{ #Dancer keyword halt does not work in this sub return halt "I don't ever want to get here"; #halt works only in a filter like before } get '/11c' => sub { #result is different from 11, but never returns eleven_two even if first #one returns nothing (see 11d) return eleven_one(); #displays "code 101" eleven_two(); #never gets there }; get '/11d' => sub { #result is different from 11 return eleven_zero(); #returns nothing eleven_two(); #never gets there }; get '/11e' => sub { #gets to eleven_two only if no error returned from eleven_one #see 11f my $ret=eleven_one(); return $ret if $ret; $ret=eleven_two(); return $ret if $ret; }; get '/11f' => sub { #gets to eleven_two only if no error returned from eleven_zero my $ret=eleven_zero(); return $ret if $ret; $ret=eleven_two(); return "I do want to get here now" if $ret; }; get '/11g' => sub { #returns emptys string my $ret=eleven_zero(); return $ret; $ret=eleven_two(); return "I do want to get here now"; }; #how does it look if we use OO? true;
Quote from perlsub: The return value of the subroutine is the value of the last expression evaluated. Alternatively, a return statement may be used to exit the subroutine, optionally specifying the returned value, which will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the subroutine call. If you specify no return value, the subroutine will return an empty list in a list context, an undefined value in a scalar context, or nothing in a void context. If you return one or more arrays and/or hashes, these will be flattened together into one large indistinguishable list. My opinion: Good practice would suggest that you always, always, always use a return statement. It guarantees that even if you change the order of the calls, the same, intended result is returned. On Wed, 2010-08-25 at 00:50 -0400, Maurice Mengel wrote:
Hi all,
am I the only one who struggles with this?
Maurice
package dancer_test; use Dancer ':syntax';
our $VERSION = '0.1';
get '/1' => sub { #You put all the code here, #collect all the output and then just return it #via template or as html etc. return "I wanna hold your hand"; #works };
get '/2' => sub { #you don't even have to write a return "I wanna hold your hand"; #works too #let's call it a "magic return" since there is no return #seems to be a feature of perl not of dancer };
get '/3' => sub { #works only as long as you don't have anything after the return value "I wanna hold your hand"; #does NOT work my $i; };
get '/4' => sub { #sometimes Dancer displays the last variable (magic return) my $out="I wanna hold your hand"; };
get '/5' => sub { #return values from a sub five(); #I find this one a bit spooky };
sub five{ #this outputs the content of $out although no return anywhere #so we might call it "magic return in a sub" my $out="I wanna hold your hand"; }
get '/6' => sub { six(); #returns 1 for success, not the content of $out };
sub six{ #this outputs the content of $out although no return anywhere my $out="I wanna hold your hand"; debug "I get here of course"; }
get '/7' => sub { seven(); };
sub seven { #this seems more proper than six my $out="I wanna hold your hand"; return $out; debug "I never get here of course"; }
get '/8' => sub { eight(); # does not output anything eight(); # does output once #I find this one a bit unintuitive };
sub eight{ #same as no. 5 my $out="I wanna hold your hand"; }
get '/81' => sub { eight(); # does not output anything eight(); # does not output anything debug "Does output a one for success"; #I find this one a bit unintuitive };
get '/9' => sub { #if you actually want the return value twice in your output my $r=nine(); $r.=nine(); };
sub nine { #I consider this good style return "testing seven"; }
get '/10' => sub { #works just as 9 my $r=ten(); $r.=ten(); };
sub ten { #as 9 but with magic return value "testing seven"; }
get '/11' => sub { #i want to stop if any errors are returned eleven_one(); #does not display anything eleven_two(); #displays "I don't ever want to get here"; };
sub eleven_zero{}
sub eleven_one{ #do this and that #if I encounter an error #if I don't encounter an error I return #nothing like in eleven_zero my $error="code 101"; return $error; }
sub eleven_two{ return "I don't ever want to get here"; }
get '/11b' => sub { #result is as in 11 eleven_three(); #does not display anything eleven_two(); #displays "I don't ever want to get here"; };
sub eleven_three{ #Dancer keyword halt does not work in this sub return halt "I don't ever want to get here"; #halt works only in a filter like before }
get '/11c' => sub { #result is different from 11, but never returns eleven_two even if first #one returns nothing (see 11d) return eleven_one(); #displays "code 101" eleven_two(); #never gets there };
get '/11d' => sub { #result is different from 11 return eleven_zero(); #returns nothing eleven_two(); #never gets there };
get '/11e' => sub { #gets to eleven_two only if no error returned from eleven_one #see 11f my $ret=eleven_one(); return $ret if $ret; $ret=eleven_two(); return $ret if $ret; };
get '/11f' => sub { #gets to eleven_two only if no error returned from eleven_zero my $ret=eleven_zero(); return $ret if $ret; $ret=eleven_two(); return "I do want to get here now" if $ret; };
get '/11g' => sub { #returns emptys string my $ret=eleven_zero(); return $ret; $ret=eleven_two(); return "I do want to get here now"; };
#how does it look if we use OO?
true; _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
participants (3)
-
Alexis Sukrieh -
Maurice Mengel -
Peter Gordon