The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::Buffer - A Simple In-Memory Buffer

SYNOPSIS

    use Mojo::Buffer;

    my $buffer = Mojo::Buffer->new('foo');
    $buffer->add_chunk('bar');
    my $foo = $buffer->remove(3);
    my $bar = $buffer->empty;

DESCRIPTION

Mojo::Buffer is a simple in-memory buffer. Functionality includes keeping track of the cumulative raw character length that has been in the buffer. Content may removed from the buffer by line or character count.

ATTRIBUTES

length

    my $length = $buffer->length;

raw_length

    my $raw_length = $buffer->raw_length;

Returns the cumulative length of the buffer. It never decreases.

METHODS

Mojo::Buffer inherits all methods from Mojo::Base and implements the following new ones.

new

    my $buffer = Mojo::Buffer->new;
    my $buffer = Mojo::Buffer->new('foobarbaz');

Returns a new Mojo::Buffer object, and possibly adds content to it.

add_chunk

    $buffer = $buffer->add_chunk('foo');

Returns the invocant and adds additional content to the buffer.

empty

    my $string = $buffer->empty;

Returns the whole content of the buffer and empties it.

get_line

   my $line = $buffer->get_line;

Returns a whole line if a newline is present in the buffer or undef, even if there is content in the buffer.

remove

    my $string = $buffer->remove(4);

Returns and removes a specific number of bytes from the buffer.

to_string

    my $string = $buffer->to_string;

Returns the whole buffer content at once.