Call::Context(3pm)

πŸ“¦
version 0.05 Β· 6 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Call::Context β€” bestie, it's here to sanity-check your calling context πŸ’…

Like, it's a Perl module (Call::Context, from the User Contributed Perl Documentation fam) and its whole personality is making sure nobody calls your function in a context it wasn't built for. Iconic behaviour honestly. πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

use Call::Context;

sub gives_a_list {

    # Will die() if the context is not list.
    Call::Context::must_be_list();

    return (1, 2, 3);
}

gives_a_list();             # die()s: incorrect context (void)

my $v = gives_a_list();     # die()s: incorrect context (scalar)

my @list = gives_a_list();  # lives

#----------------------------------------------------------------------

sub scalar_is_bad {

    # Will die() if the context is scalar.
    Call::Context::must_not_be_scalar();

    return (1, 2, 3);
}

scalar_is_bad();            # die()s: incorrect context (void)

my $v = scalar_is_bad();    # die()s: incorrect context (scalar)

my @list = scalar_is_bad(); # lives

not me reading this and going "ohhhh so that's the vibe" 😭


πŸ’– DESCRIPTION πŸ’–

okay so picture this: your function only expects to return a list. Which means if somebody calls it in some other context, that's β€” by definition β€” an error. Periodt. 🚫

The ✨problem✨ is that depending on how the function is written, it might actually do something that looks totally expected during testing… and then in production it acts completely differently. πŸ’€ The betrayal. The audacity. That's the whole reason this module exists, bestie.


🌸 FUNCTIONS 🌸

πŸ¦‹ must_be_list()

die()s if the calling function is itself called outside list context. (Peep the SYNOPSIS for examples, they're right there. πŸ‘€)

πŸ¦‹ must_not_be_scalar()

die()s if the calling function is itself called in scalar context. (Again β€” SYNOPSIS. Examples. Go look. πŸ’–)


😭 EXCEPTIONS 😭

This module throws instances of Call::Context::X. And Call::Context::X is overloaded to stringify β€” cute, right? πŸŽ€

But here's the tea: to keep memory usage low, overload is not loaded until instantiation. So it's not doing that work upfront, it waits. Lowkey efficient. Slay. ✨


🫢 REPOSITORY 🫢

https://github.com/FGasper/p5-Call-Context

go star it, bestie, it's giving main character energy ⭐


πŸ‘‘ LICENSE πŸ‘‘

This module is licensed under the MIT License. πŸŽ€


ngl the whole point is: don't let your function get called wrong and then act brand new about it. Context is everything. iykyk πŸ’…

and that's a wrap!! πŸ¦‹ this page comes to you from perl v5.38.2 and it's dated 2024-05-15 β€” Call::Context, periodt. love u, go write some context-safe subs πŸ’–πŸŒΈβœ¨

πŸ“¦ where this page comes from