Skip to content

Instantly share code, notes, and snippets.

#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: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
@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: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: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:1816344
Created February 13, 2012 12:07
Example of AnyEvent::Filesys::Notify
#!/usr/bin/env perl
use common::sense;
use AnyEvent::Filesys::Notify;
use AnyEvent;
my $src = "/my/proj/path/htdocs";
my $dst = "/var/www/html";
my $cv = AnyEvent->condvar;
@sugar84
sugar84 / gist:1918805
Created February 26, 2012 20:19
some psgi app
#!/usr/bin/env perl
use strict;
use warnings;
use 5.12.3;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use DB::Connect;
use HTML::Template;
use Plack::Request;
@sugar84
sugar84 / gist:2570008
Created May 1, 2012 17:49
proxy, anyevent::http::socks
#!/usr/bin/env perl
use strict;
use warnings;
use DB::Local;
use AnyEvent::HTTP::Socks;
use constant CHECK_TIME => 15 * 60;
use constant TIMEOUT => 10;
use constant MAX_GETS => 60;
@sugar84
sugar84 / gist:2767755
Created May 22, 2012 09:12
delayed response through psgi
use common::sense;
use AnyEvent::HTTP;
my $app = sub {
my $env = shift;
return sub {
my $respond = shift;
fetch_content(sub {
@sugar84
sugar84 / gist:2949008
Created June 18, 2012 15:45
search module
#!/usr/bin/env perl
use common::sense;
my $module = $ARGV[0];
$| = 1;
die "wrong module name: $module" if $module !~ /^[A-Za-z0-9:]+$/;
my @parts = split "::", $module;
$parts[$#parts] .= ".pm";