Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:08:43+00:00 2026-06-17T05:08:43+00:00

I am defining a method with an optional argument. Typically I would use something

  • 0

I am defining a method with an optional argument. Typically I would use something like this:

sub foo {
    ($self, $optarg) = @_;
    $optarg ||= 1;
}

I would like the optional argument to be treated as boolean, with the default being true. This obviously won’t work with the above, since $optarg will evaluate to false if the argument is not passed in.

Is there a way to differentiate the absence of an argument being passed and a value that evaluates to false?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:08:45+00:00Added an answer on June 17, 2026 at 5:08 am

    This scales well to multiple optional arguments:

    my $self = shift;
    my $optarg = @_ ? shift : 1;
    

    So does this, but it handles multiple required arguments better. Unfortunately, it violates the least-redundancy-possible principle.

    my ($self, $optarg) = @_;
    $optarg = 1 if @_ < 2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When defining a method on a class in Python, it looks something like this:
I have an interface defining a method like this : public List<IA> myMethod(); with
Given the following two ways of defining a class method in Ruby: class Foo
I'm dynamically defining a method in a module, and I'd like to check that
I hope i'm missing something obvious, but I'm having some troubles defining a method
What's the correct method for defining multi-line comments in an emacs mode (like C's
I understand that in this code: class Foo { public static void method() {
How would one go about defining a method in a super class but leave
Whats the difference between defining the method area as a property of this instead
ActionLinks This Image shows type-safe method of defining the Actionlinks ,how to define it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.