http://www.perlmonks.org?node_id=1031547

walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

I am following the webcasts and really enjoying them. However, I have hit a point of frustration. When I run the code that is presented in episode 3 (unless my eyes deceive me, it is exactly as presented in the video), I do not get the same results. When the presenter enters the wrong username and password, he gets "denied," while the correct username and password yeilds "Welcome to Momcorp!" For me, no matter what I enter with the code below, I get "Welcome to Momcorp!" Am I missing something here?
#!/usr/bin/env perl use Mojolicious::Lite; # Helpers = in app extensions helper auth => sub { my $self = shift; return 1 if $self->param('username') eq 'bender' and $self->param('password') eq 'rodriguez'; }; get '/login' => sub { shift->render('login') }; post '/momcorp' => sub { my $self = shift; $self->render(text => 'denied') if !$self->auth; $self->render(text => 'Welcome to Momcorp!'); }; post '/momcorp/carol' => sub { my $self=shift; $self->render(text => 'denied') if !$self->auth; $self->render(text => 'Welcome, Carol!'); }; app->start; __DATA__ @@ login.html.ep <h1>Login</h1> <form method="post" action="/momcorp"> Username: <input type="text" name="username" /> Password: <input type="text" name="password" /> <input type="submit" value="Log In" /> </form>

Version Information

CORE
  Perl        (v5.16.3, linux)
  Mojolicious (3.97, Rainbow)

OPTIONAL
  EV 4.0+               (not installed)
  IO::Socket::IP 0.16+  (not installed)
  IO::Socket::SSL 1.75+ (not installed)

This version is up to date, have fun!

Later in the video we modify the code to use under and tag helpers. Oddly, this code does work:
#!/usr/bin/env perl use Mojolicious::Lite; # Helpers = in app extensions helper auth => sub { my $self = shift; return 1 if $self->param('username') eq 'bender' and $self->param('password') eq 'rodriguez'; }; get '/login' => sub { shift->render('login') }; # Under: protects actions listed underneath the under # Modify behavior of any action under... # if returns true actions under can be matched # false, they are inaccessible # removes duplicate code :) under sub { my $self = shift; return 1 if $self->auth; $self->render(text => 'denied'); return; }; post '/momcorp' => sub { shift->render(text => 'Welcome to Momco +rp!') }; post '/momcorp/carol' => sub { shift->render(text => 'Welcome, Carol!' +) }; app->start; __DATA__ @@ login.html.ep %= t h1 => 'Login' %= form_for '/momcorp' => (method => 'post') => begin Username: <%= text_field 'username' %> Password: <%= text_field 'password' %> %= submit_button 'Log In' %= end

Replies are listed 'Best First'.
Re: Strange issue with Mojolicious
by Anonymous Monk on May 01, 2013 at 11:34 UTC

    When you go to http://mojocasts.com/e3 turn on javascript so you can see the comments

    solution is add return

    post '/momcorp' => sub { my $self = shift; return $self->render(text => 'denied') if !$self->auth; return $self->render(text => 'Welcome to Momcorp!'); };

    Think of it like this, its like having a $person object, and you do  $person->name('bender') if ! $self->auth; $person->name('rodriguez');

    Doesn't matter if the conditional is true, name is always changed afterwards :)

      For some reason in ArchLinux with FireFox 20.0.1 and Shockwave Flash 11.2 r202, I am unable to actually play the video in the browser, or view the comments, even with JavaScript enabled. I've just been downloading the OGV files. Actually, I don't even see a comment section ;) I appreciate the help. That actually fixed the code. I wish I had seen the comments so I didn't look like a dork posting here.

        For some reason in ArchLinux with FireFox 20.0.1 and Shockwave Flash 11.2 r202, I am unable to actually play the video in the browser,

        Me neither, I watch the video on vimeo

        or view the comments, even with JavaScript enabled.

        Well, its disqus, so you have to enable javascript for disqus too -- firefox -safe-mode does a good job of enabling all javascripts and showing the comments :)

        That actually fixed the code. I wish I had seen the comments so I didn't look like a dork posting here.

        I don't think you did :) I think you looked like me :) I had to go out of my way to discover the comments section ; videos are great for showing whats possible, but they're practically never corrected for typos.

        Don't feel bad, i was bitten by the same issue. I often wondered if the cast was release before some changes in the under laying code.
        -Pizentios