Skip to content

Instantly share code, notes, and snippets.

View jjn1056's full-sized avatar

John Napiorkowski jjn1056

View GitHub Profile
@jjn1056
jjn1056 / perl.pm
Last active March 3, 2025 19:09
another form builder model example
Sketch to see if we can just use AUTOLOAD to inject into the
display object methods as needed ad hoc
# in this approach you have some named methods like $args
# and other methods use AUTOLOAD, defautl behavior is to call
# resolve_method_for_proxy($method) or if that doesn't exist
# dfaults to $obj->method.
# if you pass args to an AUTOLOADED method, that sets that
# method to have that value.
use Future::AsyncAwait;
# Define an ASGI-like application as an anonymous async sub.
# This app checks the request scope and sends a response.
my $asgi_app = async sub ($scope, $receive, $send) {
if ($scope->{type} eq 'http') {
# Send the response start message.
await $send->({
type => "http.response.start",
#!/usr/bin/env perl
use strictures;
use PSGI::Syntax; #(signatures, etc. for example code)
use Future::AsyncAwait;
use Future;
### Basic ASGI App Implementation
async sub app($scope, $receive, $send) {
if ($scope->{type} eq 'http') {
import asyncio
# A simple logging middleware
class LoggingMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
# Only handle HTTP requests for this example.
if scope["type"] == "http":
package MyApp::Model::Components::Counter;
use Moose;
extends 'CatalystX::Components:::DisplayModel';
has count => (is=>'rw', required=>1, default=>0, live=>1); #lifecycle=>'request|session'
sub increment($self) {
my $current = $self->count;
$self->count($current++);

Enhancing Perl Method Signatures with Attributes

Extending Perl to support attributes in method signatures, similar to frameworks like Java Spring, offers numerous benefits. This document details three well-detailed examples illustrating the advantages and use cases for this syntax extension, compares it to Spring's approach, and contrasts the two methodologies. Additionally, we include curl commands to demonstrate example requests and what the Perl code would do in response.

Improved Code Readability and Maintainability

Example: Automatically Binding Path Parameters

Current Approach in Perl: In frameworks like Mojolicious, routing and parameter extraction are typically separate.

package Example::Web::Controller::TodoList;
use Moose;
use Example::Syntax;
use Modulo::PageController; # to get attributes, etc
has 'people' => (is=>'rw', context=>1);
has 'person' => (is=>'rw');
@jjn1056
jjn1056 / chat.pm
Last active November 9, 2023 21:50
first pass server sent event based open AI chat API
my $ua = Mojo::UserAgent->new(max_response_size => 0);
my $tx = $ua->build_tx(
POST => 'https://api.openai.com/v1/chat/completions',
+{
Authorization => "Bearer $ENV{OPENAI_API_KEY}",
},
json => {
model => "gpt-3.5-turbo",
stream => \1,
messages => [
package Example::Schema::ResultSet::Todo;
use Example::Syntax;
use base 'Example::Schema::ResultSet';
sub status($self, $status = undef) {
$self->{attrs}{status} = $status if defined $status;
return $self->{attrs}{status};
}
package Example::Schema::ResultSet::Todo;
use Example::Syntax;
use base 'Example::Schema::ResultSet';
__PACKAGE__->mk_group_accessors('simple' => qw/status/);
sub available($self) {
return $self->search_rs({status=>{'!='=>'archived'}});
}