Skip to content

Instantly share code, notes, and snippets.

View jjn1056's full-sized avatar

John Napiorkowski jjn1056

View GitHub Profile

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'}});
}
@jjn1056
jjn1056 / gist:675c5ce7318218a57c48c1f9f17c7a72
Created September 24, 2023 17:34
sketches for how form builder creates action urls
form_for 'contact', +{
url=>sub($self, $contact) { $contact->in_storage ? path('create') : path('update', [$contact]) }
}, sub ($self, $fb, $contact) {
form_for 'contact', +{
create_url => sub($self, $contact) { path('create') },
update_url => sub($self, $contact) { path('update', [$contact]) },
}, sub ($self, $fb, $contact) {
{
package MyClass::Moose;
use Moose;
has 'attribute1' => (is => 'ro');
has 'attribute2' => (is => 'ro');
has 'attribute3' => (is => 'ro');
MyClass::Moose->meta->make_immutable;
}
use Moose;
use MooseX::MethodAttributes;
use Example::Syntax;
class Example::Controller::Account :isa(Example::Controller) {
sub root :At('$path_end/...') Via('../protected') ($self, $c, $user) {
$c->action->next($user->account);
}
@jjn1056
jjn1056 / build_embeddings.pl
Last active July 24, 2023 11:45
a small Perl script to get vector embeddings for a list of meals via openAI and put them into a database using pg_vector
use warnings;
use strict;
use DBI;
use AI::Embedding;
my $dbh = DBI->connect(
'DBI:Pg:dbname=[DB]',
'[USER]',
'[PASSWORD',
@jjn1056
jjn1056 / colors.sql
Created July 23, 2023 17:19
an example using pg_vector to store information about colors via their RGB values
-- Assuming you already have a database named 'my_database'
CREATE TABLE colors (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
color_vector VECTOR(3)
);
INSERT INTO colors (name, color_vector) VALUES
-- Primary colors