Last active
October 2, 2015 17:48
-
-
Save worr/2288417 to your computer and use it in GitHub Desktop.
Moose example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use 5.010; | |
| use Net::AFP::DSI::Attention; | |
| my $dsi_attn = Net::AFP::DSI::Attention->new( | |
| flags => $Net::AFP::DSI::REPLY, | |
| error_or_offset => 106, | |
| request_id => 1234, | |
| payload => 'butts', | |
| ); | |
| say $dsi_attn->pack |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Net::AFP::DSI; | |
| use Const::Fast; | |
| use Moose; | |
| const our $REQ => 0x0; | |
| const our $REPLY => 0x1; | |
| const our $CLOSE => 0x1; | |
| const our $CMD => 0x2; | |
| const our $STAT => 0x3; | |
| const our $OPEN => 0x4; | |
| const our $TICKLE => 0x5; | |
| const our $WRITE => 0x7; | |
| const our $ATTN => 0x8; | |
| has 'flags' => ( | |
| is => 'rw', | |
| isa => 'Int', | |
| ); | |
| has 'command' => ( | |
| is => 'rw', | |
| isa => 'Int', | |
| ); | |
| has 'request_id' => ( | |
| is => 'rw', | |
| isa => 'Int', | |
| ); | |
| has 'error_or_offset' => ( | |
| is => 'rw', | |
| isa => 'Int', | |
| ); | |
| has 'data_length' => ( | |
| is => 'ro', | |
| isa => 'Int', | |
| default => 'payload_length', | |
| lazy => 1, | |
| ); | |
| sub has_payload { | |
| my $self = shift; | |
| return $self->meta->has_attribute('payload'); | |
| } | |
| sub payload_length { | |
| my $self = shift; | |
| return length $self->payload if $self->has_payload; | |
| return 0; | |
| } | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Net::AFP::DSI::Attention; | |
| use Moose; | |
| extends 'Net::AFP::DSI'; | |
| with 'Net::AFP::HasPayload'; | |
| with 'Net::AFP::Packer'; | |
| use constant pack_string => 'C2nN3a*'; | |
| sub BUILD { | |
| my $self = shift; | |
| $self->command($Net::AFP::DSI::ATTN); | |
| } | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Net::AFP::DSI::HasPayload; | |
| use Moose::Role; | |
| has 'payload' => ( | |
| is => 'rw', | |
| isa => 'Str', | |
| trigger => sub { | |
| my $self = shift; | |
| $self->meta->find_attribute_by_name('data_length')->set_value($self, $self->payload_length); | |
| }, | |
| ); | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Net::AFP::Packer; | |
| use Moose::Role; | |
| use 5.010; | |
| requires 'pack_string'; | |
| sub pack { | |
| my $self = shift; | |
| my @attrs; | |
| foreach my $attr ($self->meta->get_all_attributes) { | |
| $attrs[$attr->insertion_order] = $attr->get_value($self); | |
| } | |
| push @attrs, 0; # filler | |
| if ($self->has_payload) { | |
| push @attrs, $self->payload; | |
| } | |
| return pack $self->pack_string, @attrs; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment