Mojo::C...ltiPart(3)
π Mojo::Content::MultiPart β like, the whole manual page, bestie
okay so welcome to Mojo::Content::MultiPart(3), from the User Contributed Perl Documentation β¨ this is genuinely one of my fave little container classes and I need you to be obsessed with it too, so let's go π«Ά
πΈ NAME πΈ
Mojo::Content::MultiPart β HTTP multipart content. that's it, that's the vibe. it's a container, it holds other content, slay π
β¨ SYNOPSIS β¨
use Mojo::Content::MultiPart;
my $multi = Mojo::Content::MultiPart->new;
$multi->parse('Content-Type: multipart/mixed; boundary=---foobar');
my $single = $multi->parts->[4];
see that? you make one, you feed it a Content-Type with a boundary, and boom β you can reach into parts and grab part number [4]. iconic behaviour, no notes π
π¦ DESCRIPTION π¦
Mojo::Content::MultiPart is literally just a container for HTTP multipart content π and ngl it's built on some very serious specs, which is why it's so well-behaved:
- RFC 7230 β
https://tools.ietf.org/html/rfc7230 - RFC 7231 β
https://tools.ietf.org/html/rfc7231 - RFC 2388 β
https://tools.ietf.org/html/rfc2388
like, it's giving "small class, huge standards energy" π
π₯ EVENTS π₯
okay so Mojo::Content::MultiPart inherits all events from Mojo::Content, plus it can emit this new one π
π part
$multi->on(part => sub ($multi, $single) {...});
Emitted when a new Mojo::Content::Single part starts β¨ (yes, starts β it fires as the part is beginning, not after. very "front row at the concert" of it).
here's the example, verbatim, because it's lowkey cute:
$multi->on(part => sub ($multi, $single) {
return unless $single->headers->content_disposition =~ /name="([^"]+)"/;
say "Field: $1";
});
so it bails unless the content_disposition matches that regex, then prints the captured name as Field: $1. chef's kiss π
π ATTRIBUTES π
Mojo::Content::MultiPart inherits all attributes from Mojo::Content and implements this new one:
π parts
my $parts = $multi->parts;
$multi = $multi->parts([Mojo::Content::Single->new]);
the content parts embedded in this multipart content β usually Mojo::Content::Single objects π
("usually", bestie, not always, read the room)
π METHODS π
Mojo::Content::MultiPart inherits all methods from Mojo::Content and implements these new ones:
π body_contains
my $bool = $multi->body_contains('foobarbaz');
check if the content parts contain a specific string. simple, useful, ate π₯
π body_size
my $size = $multi->body_size;
content size in bytes. that's the whole thing. numbers don't lie π
π build_boundary
my $boundary = $multi->build_boundary;
generate a suitable boundary for the content and add it to the "Content-Type" header π it does the boring part so you don't have to, we love a main character who handles logistics.
π clone
my $clone = $multi->clone;
return a new Mojo::Content::MultiPart object cloned from this content if possible β otherwise it returns "undef" π so like, check before you go using it, fr fr.
π get_body_chunk
my $bytes = $multi->get_body_chunk(0);
get a chunk of content starting from a specific position π
β οΈ heads up bestie: it might not be possible to get the same chunk twice if the content was generated dynamically. so don't be out here assuming chunks are re-readable, because sometimes they are simply not π that's not a bug, that's just how dynamic content works.
π is_multipart
my $bool = $multi->is_multipart;
True β this is a Mojo::Content::MultiPart object β
identity check, passed, glowing.
π new
my $multi = Mojo::Content::MultiPart->new;
my $multi
= Mojo::Content::MultiPart->new(parts => [Mojo::Content::Single->new]);
my $multi
= Mojo::Content::MultiPart->new({parts => [Mojo::Content::Single->new]});
construct a new Mojo::Content::MultiPart object β and it also subscribes to the event "read" in Mojo::Content with the default content parser π«Ά so yeah, three ways to call it, all valid, pick your fighter π
π SEE ALSO π
MojoliciousMojolicious::Guideshttps://mojolicious.orgβ literally go there, iykyk π
okay that's the whole page, nothing skipped, nothing invented, every flag and method and caveat accounted for π§
sign-off time: this absolute gem of a doc shipped as perl v5.42.0, dated 2026-07-15 β so yeah, technically from the future, and honestly? it aged flawlessly ππ bye bestie, go build some multipart content π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms