Skip to content

Instantly share code, notes, and snippets.

open($fh, "<", $path1.$file )
# if path1 fails, try path2 and set missing boolean to 1
or $missing = 1 and open($fh, "<", $path2.$file )
or warn "can't open $file in $path1, $path2;
@bimmerlabs
bimmerlabs / tekla_cloud_utility.pl
Last active November 28, 2018 06:27
Renames Tekla Structures pointcloud files to be human readable
#!/usr/bin/perl
use Mojo::Base -base;
use Mojo::Util qw(getopt);
use Mojo::File;
use Mojo::DOM;
getopt 'p|path=s' => \my $path;
sub main {
# look in xml elements for laserscans that have hashes for names, then rename.
@bimmerlabs
bimmerlabs / myapp_broken.pl
Last active July 13, 2018 18:25
Subprocess error: Connection already closed when using a line like "if (defined $self->param('foo') { ... }" and the param is not defined
#!/usr/bin/env perl
use Mojolicious::Lite;
# it turns out a race condition was being caused, that accidentally 'won' every time before -
# but the updated Mojolicious made the broken code apparent. This no longer works:
get '/' => sub {
my $self = shift;
$self->render_later;
Mojo::IOLoop->subprocess(
@bimmerlabs
bimmerlabs / deployment.bat
Created February 28, 2018 14:48
Mojolicious server batch file for Windows - runs on startup so you don't need a command prompt open all the time. Put a shortcut to myapp.vbs in the 'startup' folder, the actual myapp.vbs / deployment.bat files go in your app folder.
@echo off
perl C:\Users\yournamehere\myapp\myapp.pl daemon -m production
@bimmerlabs
bimmerlabs / Downloads.pm
Created February 14, 2018 14:42
stream Mojo::Asset::Memory object
# streams $zipfile created in memory (not saved anywhere)
# should work similar with Excel::Writer::XLSX
# (I generate and return the zip file in a Mojo::IOLoop Subprocess)
$self->res->headers->content_disposition(
'attachment; filename=' . $filename . '.zip;');
$self->res->headers->content_type('application/zip');
$self->reply->asset(Mojo::Asset::Memory->new->add_chunk($zipfile));
@bimmerlabs
bimmerlabs / myapp.pl
Last active January 11, 2018 16:54
A Mojolicious::Lite forZip file streaming with Mojo::Asset::Memory
#!/usr/bin/env perl
use Mojolicious::Lite;
use Archive::Zip::SimpleZip qw($SimpleZipError);
get '/#file' => sub {
my $c = shift;
# read a file from disk
my $file1 =
Mojo::Asset::File->new(
@bimmerlabs
bimmerlabs / myapp.pl
Created October 27, 2017 14:39
If I comment out the line with 'test2' it works - but otherwise, it times out. What am I missing? taken from http://bit.ly/2zJHEK1
use Mojolicious::Lite;
use Mojo::IOLoop;
get '/' => sub {
my $c = shift;
$c->delay(
sub { say 'test1'; Mojo::IOLoop->timer(1 => shift->begin); },
sub { say 'test2'; my $test = 'test'; },
sub { say 'test3'; $c->render(text => 'Hi'); },
);
@bimmerlabs
bimmerlabs / DBAdmin.pm
Created June 6, 2017 16:38
Mojo:PG migration - I'm having some trouble and it's because I don't quite understand what it's doing.
package MyApp::Model::DBAdmin;
use Mojo::Base -base;
has 'database';
sub new { bless {}, shift }
sub create_table {
my ($self, $tablename) = @_;
$tablename = "test";