Business::EDI(3pm)

πŸ“¦
version 0.05 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble

πŸ’– Business::EDI πŸ’–

okay bestie, so this is Business::EDI β€” a Perl module, and it's the top level class for generating U.N. EDI interchange objects and subobjects. That's the whole vibe. U.N. EDI. Interchange objects. Subobjects. We're doing serious international-standards business and we're doing it in Perl, periodt. ✨


🌸 SYNOPSIS 🌸

use Business::EDI;

my $edi = Business::EDI-new('d09b');      # set the EDI spec version
my $rtc = $edi->codelist('ResponseTypeCode', $json) or die "Unrecognized code!";
printf "EDI response type: %s - %s (%s)\n", $rtc->code, $rtc->label, $rtc->value;

my $msg = Business::EDI::Message->new($ordrsp) or die "Failed Message constructor";
foreach ($msg->xpath('line_detail/all_LIN') {
    ($_->part(7143) || '') eq 'EN' or next;
    print $_->part(7140)->value, "\n";    # print all the 13-digit (EN) ISBNs
}

like... look at her go 🫢 she's grabbing a d09b spec version, pulling a codelist out of some JSON, and then walking a whole message to print every 13-digit (EN) ISBN. iconic behavior tbh.


πŸ¦‹ DESCRIPTION πŸ¦‹

So the whole point of this module β€” the focus of functionality β€” is giving you object based access to EDI messages and subelements. πŸ’…

And here's the tea: right now, the EDI input that Business::EDI objects process is JSON from the edi4r ruby library. That's it. That's the input. And there is no EDI output beyond the perl objects themselves. No cap. So don't come here looking for it to hand you back a nicely serialized EDI file, because she does not do that (yet, anyway). πŸ™ˆ


πŸ’œ NAMESPACE πŸ’œ

the moment you do this:

use Business::EDI;

these package namespaces also get loaded, automatically, no asking:

Business::EDI::Segment_group
Business::EDI::Message

and THAT, bestie, is why the example message constructor up in SYNOPSIS would succeed even though nobody ever did a use Business::EDI::Message; β€” she was already loaded. main character energy fr fr. πŸ‘‘


πŸŽ€ EDI Structure πŸŽ€

Everything depends on the spec. like, everything. Which means you have to have declared a spec version before you can create or parse a given chunk of data. That's the rule. 😀

The one exception: a whole EDI message, because each message declares its spec version internally. She carries her own spec around with her. self-sufficient queen.

EDI has a hierachical specification defining data. from top to bottom, it goes:

  • Communication β€” containing one or more messages (not yet modeled here)
  • Message β€” containing segment groups and segments
  • Segment Group β€” containing segments
  • Segment β€” containing composites, codelists and data elements
  • Composite β€” containing multiple codelists and/or data elements
  • Codelist β€” enumerated value from a spec-defined set
  • Data Element β€” unenumerated value
heads up: this module handles messages and everything below, but not (yet) communications. so if you came for the Communication layer, she's not there yet. πŸ’€

⭐ CLASS FUNCTIONS ⭐

Much more documentation needed here... (her words, not mine, but honestly? same.)

🌈 new()

Constructor. that's the whole description. she's a constructor. slay. ✨


🌸 OBJECT METHODS (General) 🌸

πŸ’– value()

Get/set accessor for the value of the field. classic.

πŸ’… code()

The string code designating this node's type. This code is what the spec uses to refer to the object's definition. Like, for example: a composite C504, segment RFF, data element 7140, etc. πŸ“‹

okay so do NOT get confused when you're dealing with CodeList objects, bestie, because this trips people up: calling code() gets you the 4-character code of the CodeList field, NOT what that CodeList is currently set to. For that, you want value(). two different things. keep them straight. 🧚

πŸ¦‹ desc()

English description of the element. short, sweet, helpful.


πŸ“ METHODS (for Traversal) πŸ“

✨ part_keys()

This method returns strings that can be fed to part() like:

foreach ($x->part_keys) { something($x->part($_)) }

This is similar to doing:

foreach (keys %x) { something($x{$_}) }

and the point of this is that an object can be exhaustively, recursively parsed without further knowledge of it. you don't have to know anything about the thing to walk the entire thing. it's giving omniscient. πŸ”₯

πŸ’œ part($key)

Returns subelement(s) of the object. The key can reference any subobject allowed by the spec. πŸ’«

If the subobject is repeatable, then prepending all_ to the key will return an array of all such subobjects. This is the safest and most comprehensive approach β€” do this one, bestie, I'm begging. πŸ™

Using part($key) without all_ to retrieve when there is only one $key subobject will succeed. βœ…

Using part($key) without all_ to retrieve when there are multiple $key subobjects will FAIL. ❌

And since that difference is only dependent on data, you should always use all_ when dealing with a repeatable field (or xpath, see below). like, always. no exceptions. the data will betray you.

Examples:

my $qty  = $detail->part('QTY');      # FAILURE PRONE!
my @qtys = $detail->part('all_QTY');  # OK!
see that # FAILURE PRONE! comment? that's not a joke, that's a warning label. πŸ’€

🌈 xpath($path)

$path can traverse multiple depths in representation via one call. For example:

$message->xpath('all_SG26/all_QTY/6063')

is like this function foo():

sub foo {
    my @x;
    for my $sg ($message->part->('all_SG26') {
        for ($sg->part('all_QTY') {
            push @x, $->part('6063');
        }
    }
    return @x;
}

The xpath version is much nicer! πŸŽ€

However β€” and this is important β€” this is nowhere near as fully featured as W3C xpath for XML. It's more like a multple-depth part(). so manage your expectations accordingly, she's a vibe not a standard.

Examples:

my @obj_1154 = $message->xpath('line_detail/SG31/RFF/C506/1154');

🧚 xpath_value($path)

Returns value(s) instead of object(s). same energy, just the values this time. ✨

Examples:

'ORDRSP' eq $ordrsp->xpath_value('UNH/S009/0065') or die "Wrong Message Type!";

πŸ’€ WARNINGS πŸ’€

This code is experimental. EDI is a big spec with many revisions. she's doing her best. 😭

At the lower levels, all data elements, codelists, composites and segments from the most recent spec (D09B) are present. so like, the bottom of the stack is fully stocked. πŸ’…


πŸ‘‘ SEE ALSO πŸ‘‘


🫢 AUTHOR 🫢

Joe Atzberger


and that's the whole page, bestie!! 🌟 this was perl v5.34.0, dated 2022-06-30, and it's the Business::EDI(3pm) page from User Contributed Perl Documentation. love her, read her, use all_ on repeatable fields. mwah. πŸ’–πŸŽ€πŸ¦‹πŸŒΈβœ¨

πŸ“¦ where this page comes from