But I suppose you don't want the sub missing() to be available as method the way ->foo() is?

That doesn't harm at all. Calling $obj->missing just returns false, which is correct :-)

However, your objection discovered a flaw in my approach. The missing sub needs to be prototyped to go one step further: multiple optional arguments. And then slurping starts hurting :-)

This leads to:

#!/usr/bin/perl use v5.16; use warnings FATAL => 'all'; package Foo; use experimental 'signatures'; use constant MISSING => bless {}, __PACKAGE__ . '::__missing__'; sub missing :prototype(;$) { @_ ? $_[0] && $_[0] eq MISSING : MISSING; } sub new ($class) { bless {}, $class; } sub foo ($self, $foo=missing, $bar=missing) { say "foo is missing" if missing $foo; say "bar is missing" if missing $bar; } package main; my $foo = Foo->new; say "none:"; $foo->foo; say "one:"; $foo->foo(1); say "two:"; $foo->foo(1, 2); print "\n"; say 'object foo is not missing' unless $foo->missing; __DATA__ none: foo is missing bar is missing one: bar is missing two: object foo is not missing

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

In reply to Re^3: Distinguish between missing and undefined arguments with subroutine signatures by jo37
in thread Distinguish between missing and undefined arguments with subroutine signatures by jo37

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.