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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:58:33+00:00 2026-06-06T02:58:33+00:00

In Perl, if I want to use named parameters in an object constructor, my

  • 0

In Perl, if I want to use named parameters in an object constructor, my code seems a bit clumsy if I wish to have some validation.

sub new {

   my $class = shift;
   my $self = {};

   my %args = @_;
   foreach my $argname (keys %args) {
     if    ($argname eq 'FOO') { $self->{$argname} = $args{$argname}; }
     elsif ($argname eq 'BAR') { $self->{$argname} = $args{$argname}; }
     elsif ($argname eq 'BAZ') { $self->{$argname} = $args{$argname}; }
     …
     else                      { die "illegal argument $argname\n"; }
   }

   bless $self;
   return $self;
}

Firstly it seems a bit clumsy to have a temporary hash (%args). Secondly the whole if chain seems verbose and tedious.

The latter can be simplified to

  if ('-FOO-BAR-BAZ-'=~m/-$argname-/) { $self->{$argname} = $args{$argname} }
  else { die "..."; }

but I imagine this can be improved.

If I need to check values, the if … elsif chain is still necessary?

I’ve searched a little but cannot find a better idiom. Is there one (other than using a Perl OO framework of some sort)

  • 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-06T02:58:34+00:00Added an answer on June 6, 2026 at 2:58 am

    I found myself constantly writing unnecessary code which checked the given parameters. But then I discovered Params::Validate. It is easy to use and if the validation fails it provides very clear and user-friendly error messages. Covering all possible combinations of parameters and their error messages is a tedious task. I prefer this way instead:

    use Params::Validate qw/:all/;
    sub new {
        my $pkg = shift;
        validate(
            @_, {
                foo => { type => SCALAR | ARRAYREF },
                bar => { type => SCALAR, optional => 1},
                baz => { type => ARRAYREF, default => ['value'] },
                quux => { isa => 'CGI' }
            }
        );
    
        return bless { @_ }, $pkg;
    }
    

    And later this code

    MyApp::Something->new(
        foo => 123,
        bbr => 'typo',
        quux => CGI->new()
    );
    

    becomes:

    The following parameter was passed in the call to MyApp::Something::new but was not listed in the validation options: bbr
     at test.pl line 14.
        MyApp::Something::new(undef, 'foo', 123, 'bbr', 'typo', 'quux', 'CGI=HASH(0x7fd4fa1857e0)') called at test.pl line 27
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Perl code which has file glob operation. $file1 = @ARGV[0]; @res1
Have some Perl code which is using the DBI module - (the code is
I want to use Perl's sprintf to zerofill a variable. sprintf(%08d, $var); But I
If you want to learn how to use Perl interpreter threads, there's good documentation
I have an array in Perl I want to print with space delimiters between
I've created some business classes using OO Perl and I want to make sure
I want to convert a code from perl to c++, and my problem is
I want to do permutation in Perl. For example I have three arrays: [big,
I want to use files that are inside cgi-bin folder in perl, I want
Let's say I have a perl module file and I want to include and

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.