Skip to content

Instantly share code, notes, and snippets.

@sugar84
sugar84 / gist:1391022
Created November 24, 2011 10:08
Mojo::UserAgent utf8
#!/usr/bin/env perl
use Mojolicious::Lite;
use utf8;
use Encode qw/encode_utf8 decode_utf8/;
post '/' => sub {
my $self = shift;
my $param = $self->req->body;
@sugar84
sugar84 / gist:1366936
Created November 15, 2011 12:15
fork and filehandles in perl
perl -E 'open my $x, "<", "test.tmp" or die; seek $x, 10, 0; if (my $child = fork) { waitpid $child, 0; say "Parent: child exited, pos: " . tell($x) } else { ; say "Child: after read from file, pos: " . tell($x) }'
@sugar84
sugar84 / gist:1305887
Created October 22, 2011 11:21
asynchronous timer
#!/usr/bin/env perl
use common::sense;
use AnyEvent;
use AnyEvent::Util qw/fork_call/;
my $cond = AnyEvent->condvar;
my $timer = AnyEvent->timer(
interval => 3,
@sugar84
sugar84 / gist:1301195
Created October 20, 2011 13:54
useful padre commands to remind
svn co http://svn.perlide.org/padre/trunk/ padre_trunk
svn co http://svn.perlide.org/padre/trunk/Padre padre_trunk
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@sugar84
sugar84 / gist:1264816
Created October 5, 2011 16:00
upload bug?
#!/usr/bin/env perl
use Mojolicious::Lite;
# create file for testing
my $file = "yet_another_file";
qx(touch $file; echo "hello" > $file);
any '/' => sub {
my $self = shift;
[sugar@comp mojoxrun]$ PERL5LIB="lib" prove
t/00-load.t ................. 1/1 # Testing MojoX::Run 0.15, Perl 5.012004, /usr/bin/perl5.12.4
t/00-load.t ................. ok
t/boilerplate.t ............. ok
t/manifest.t ................ skipped: Author tests not required for installation
t/mojox-run-complexsub.t .... [Wed Oct 5 00:17:45 2011] [debug] Not installig any signal handler, because IOWatcher implementation Mojo::IOWatcher::EV implements better CHLD signal handling.
t/mojox-run-complexsub.t .... 1/5 Write failed: Can't call method "_connecting" on an undefined value at /usr/local/share/perl/5.12/Mojo/IOLoop/Client.pm line 129.
t/mojox-run-complexsub.t .... 2/5 Write failed: Can't call method "_connecting" on an undefined value at /usr/local/share/perl/5.12/Mojo/IOLoop/Client.pm line 129.
t/mojox-run-complexsub.t .... ok
t/mojox-run-long-running.t .. [Wed Oct 5 00:17:46 2011] [debug] Not installig any signal handler, because IOWatcher implementation Mojo::IOWatcher::EV implements better CHLD
@sugar84
sugar84 / gist:1234627
Created September 22, 2011 12:06
separate script
#!/usr/bin/env perl
use common::sense;
use lib qw(./lib);
use MojoX::Run;
use Data::Dumper;
my $run = MojoX::Run->new;
my $all = { sda => 11, sdb => 22, sdc => 11, sdd => 22, sde => 11, sdf => 22, sdg => 33 };
@sugar84
sugar84 / gist:1234591
Created September 22, 2011 11:41
mojox_run error?
my $count = scalar keys %$all;
my (@shas, $i);
for my $disk (keys %$all) {
my $part = $all->{$disk}->{partition};
my $pid = $run->spawn(
cmd => sub {
# my @shas =
# map {/^([0-9a-f]+)/}
# qx(find $dest/$part -type f | grep -Fv "lost+found" | sort | xargs shasum)
# ;
@sugar84
sugar84 / gist:1230440
Created September 20, 2011 21:30
mojox_run hangs
#!/usr/bin/env perl
use common::sense;
use Data::Dumper;
use MojoX::Run;
use Mojolicious;
say $Mojolicious::VERSION;
my $sub = sub {
return "hello", "world";