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 8951905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:45:44+00:00 2026-06-15T13:45:44+00:00

I have a set of methods where I would like the caller to be

  • 0

I have a set of methods where I would like the caller to be able to override a value, or it defaults to the instance variable.

So what I keep trying to do is:

method foo( Str :$blah = $self->blah ) {
    #doStuff
}

which throws a parsing error so I end up doing this everywhere

method foo( Str :$blah? ) {
    $blah = $self->blah unless defined $blah;
    #doStuff
}

Not horrendous, but seems silly when MooseX::Method::Signatures supports the default concept and has fixed all my other standard ‘start of method’ lines.

Invariably when I’m trying to do something like this and I can’t find anyone else on the web who’s run into the same problems, I’m approaching the problem the wrong way. It seems like I might be trying to jam a functional program into a oo layout, rather than actual oo as the methods are more helper functions for calling externally rather than methods operating on the object. So just checking if I’m just defining it incorrectly or if what I’m doing is “dumb” or just not supported by Parse::Method::Signatures.

  • 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-15T13:45:45+00:00Added an answer on June 15, 2026 at 1:45 pm

    AFAIK The Signatures module hooks itself into the Perl parser and injects some code to handle the prototypes. It is remarkable that this works.

    That said, using non-constant values as defaults could have issues. And there may be scenarios where calling some code to prepopulate a value could wreak havoc. Specifically, should the code that prepopulates the value should only be called if no value for the argument is given, or should it be always called? What should we do with side effects? What should the caller be? If I have a global $self object, shouldn’t that receive the method call, as the scoping rules clearly dictate this? (As our $x=5; my $x=$x; is valid, but just my $x=$x is not). Even better, what would happen if the default value population method call would call the same method, but again without a value for the optional parameter?

    You can always fall back to old-school optional parameters:

    sub foo {
      my ($self, $blah) = @_;
      # something like my ($self, $blah) = (@_, $self->blah); wouldn't work, of course.
      $blah //= $self->blah; # nicer than old `unless defined`
      ...;
    }
    

    or, in this case:

    method foo (:$blah?) {
       $blah //= $self->blah
    }
    

    I find this use of the defined-or operator quite enjoyable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set my default-input-method variable to english-dvorak: (custom-set-variables '(default-input-method english-dvorak)) When I launch
I have some NSManagedObjects and I would like to write methods for sorting and
I have a list of Child objects. I would like to be able to
I have a set of methods that do some utility work over SQL connection,
I have set break points on my attached properties SetXXX and GetXXX static methods.
I have a set of unit tests, each with a bunch of methods, each
I have - class A { // contains certain set() and get() methods }
I have a method set up that uses jquery form for a file upload
I have a button and onclick is set to this method which should display
I have a data type that contains a set and a method that expects

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.