Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created April 28, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/f1f8057c58dc728d1597e877dd277a9f to your computer and use it in GitHub Desktop.
Save zoffixznet/f1f8057c58dc728d1597e877dd277a9f to your computer and use it in GitHub Desktop.
sub with-optional($arg?) { # <--- $arg defaults to `Any` type object, if no argument is given
say "bob"; # <--- output string `bob`; return True; discard return value
$arg; # <--- return value used for `with-optional` sub
}
with-optional; # no arg given, so `with-optional`'s argument default to `Any`. It's used as return value, which is discarded here
with-optional(1); # arg is 1; it's used as return value, so return value is `1`, which is discarded here
say with-optional; # no arg = `Any` return value from `with-optional`, which is given as argument to `say,
# which outputs it as a string (after some processing). The `say` returns True, which is then discarded
say with-optional(1); # 1 as arg = 1 as return value from `with-optional`, which is given as argument to `say,
# which outputs it as a string (after some processing). The `say` returns True, which is then discarded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment