From Sebastian Riedel

perl -Mojo -E'say g("mojolicio.us")->dom("*")->pluck("type")->uniq'

perl -Mojo -E'g("metacpan.org/search?q=mojo")->dom("big a")
->pluck("text")->join("\n")->spurt("m.txt")'

perl -Mojo -E'say g("reddit.com")->dom("*")->pluck(attrs => "href")
->grep(qr/^http/)->uniq'

perl -Mojo -E'say r g("search.twitter.com/search.json?q=perl")->json'

perl -Mojo -E'a({json => {foo => ["bar"]}})->start' get / /foo/0

perl -Mojo -E'g("mojolicio.us")->dom("h1, h2, h3")->map(sub { $_->text })
->shuffle->join("\n")->say'

mojo get www.reddit.com/r/perl/ 'p.title > a.title' text

perl -Mojo -E'say a->text_field("foo", value => "bar")'

perl -Mojo -E'a("/:name" => {inline => "Hi !"})->start' get -v /foo

perl -Mojo -E 'a(sub { shift->render(json => {time => time}) })
->start' daemon

(See mojolicio.us/perldoc/ojo for more, and also the mailing list archives.)

Advertisements

When debugging code that uses attributes, as provided by Mojo::Base, it sometimes helps to visualise what its getter/setter actually looks like.

A getter/setter with defined default.

{
package MyClass;
sub my_attr {
  if (@_ == 1) {
    return $_[0]{'my_attr'} if exists $_[0]{'my_attr'};
    return $_[0]{'my_attr'} = $default->($_[0]);
  }
  $_[0]{'my_attr'} = $_[1];
  $_[0];
}
1;
}

A getter/setter without default.

{
package MyClass;
sub my_attr {
  if (@_ == 1) {
    return $_[0]{'my_attr'};
  }
  $_[0]{'my_attr'} = $_[1];
  $_[0];
}
1;
}

You can see your own attribute definitions via

export MOJO_BASE_DEBUG=1
./my_script eval 1
unset MOJO_BASE_DEBUG

Obviously there is a lot of advocacy at mojocasts and mojolicio.us, but there are a lot of useful posts outside those channels, for example in PerlMonks and StackOverflow.

Security

Beginner apps with database

 

 

When a director gives you a line reading that doesn’t feel right for your character, nod and agree with him or her, then do it the way you know your character would do it. If you’re an actor, always be true to your character; if you’re not an actor, have character, and always be true to yourself… Whatever you do, wherever you go, do something real, make a real product, provide a real service, do something of value. Create something of beauty. — Robert De Niro at Bates

This is one of my favourite pieces of knowledge I get to share today. Those clever people at heroku.com have made the business of publishing (modern) web applications exceedingly slick and easy. However, on the face of it (and I did hunt for a bit of time) they don’t support perl. Fret not; if you dig around under the bonnet it turns out they support any language/platform as long as someone provides a compatible ‘buildpack’. And it just so happens that Magnus Holm (a fine geek from Norway) has worked out the mechanics of getting your app publishable and provided a buildpack so it all happens behind the scenes.

(If you’re not writing your apps on Mojolicious your platform may not be modern enough — you’ll have to look at Magnus’s notes and code to see if you can become compatible. There are other buildpacks on the buildpack page.)

I will be deploying my web application from a debian server, so it made sense to create a chroot jail for it. Inside there I would be able to install third-party packages without worry of them invading my main diskspace. Wherever you deploy from, you’ll need to have root/superuser access so you can install the heroku tools.

mkdir -p /opt/jail/heroku
debootstrap squeeze /opt/jail/heroku
chroot /opt/jail/heroku
adduser --ingroup www-data --disabled-password --gecos 'app,,,' app
su - app
  • First set up your account via the web page
    • Sign up for free account
    • Give email and password
    • Acknowledge email
    • Login and go to Dev Center (for reading later)
  • Second install the heroku toolbelt
ssh-keygen
wget -qO- https://toolbelt.heroku.com/install.sh >/tmp/toolbelt.sh
view /tmp/toolbelt.sh
sh /tmp/toolbelt.sh
heroku login
heroku apps:create myapp --stack cedar \
  --buildpack http://github.com/judofyr/perloku.git

That works for debian and ubuntu — there are also installs for other platforms.
You can change the name later, but it’s slightly less trouble to get it right at the start.
(You can have multiple applications, just treat each one the same as the first.)
Application names need to be unique across heroku, so don’t be surprised if obvious names have already gone.
If you create an application without a buildpack (as I did) don’t worry, you can add a buildpack with

heroku config:add BUILDPACK_URL=http://github.com/judofyr/perloku.git
  • Third set up your development directory
    • Create a directory [in fact I created a chroot jail and a special user but that’s only if you want complete isolation] and go into it
mkdir myapp
cd myapp
git init
vi Perloku ...
git add .
git commit -m 'Initial'
git push heroku master
  • Fifth try out your new application

Referenced links