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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:43:35+00:00 2026-05-19T06:43:35+00:00

I have a set of fields with each field having different set of validation

  • 0

I have a set of fields with each field having different set of validation rules.

I have placed the subroutine reference for validating a hash-ref.

Currently its in my constructor, but I want to take it out of my constructor in a private sub.

I have done it as below

sub new {
my $class = shift;
my $self  = {@_};

$class = (ref($class)) ? ref $class : $class;
bless($self, $class);

$self->{Validations} = {
  Field1 => {name => sub{$self->checkField1(@_);},args => [qw(a b c)]}
  Field2 => {name => sub{$self->checkField2(@_);},args => {key1, val1}}
..
..
..
..
};

return $self;
}

Now I want to take out all this validation rules out of my constructor and want to do some thing like below, so that I have some better control over my validation rules based on types fields.(Say some rules are common in one set of fields and I can overwrite rules for other rules just by overwriting the values of fields.)

bless($self, $class);

  $self->{Validations} = $self->_getValidation($self->{type});

  return $self;
}
sub _getValidation{
     my ($self,$type) = @_;
     my $validation = {
     Field1  => {name => sub {$self->checkField1(@_);}, args => {key1 => val1}},};

     return $validation;
}

But I am getting Can't use string ("") as a subroutine ref while "strict refs" in use at... Can anybody tell me why is this behavior with sub ref. If I check my name key, its coming to be null or sub {DUMMY};

  • 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-05-19T06:43:36+00:00Added an answer on May 19, 2026 at 6:43 am

    It looks to me like you are getting close to reinventing Moose poorly. Consider using Moose instead of building something similar, but less useful.

    The error message means that you are passing in a string in a place where your code expects a code reference. Get a stack trace to figure out where the error is coming from.

    You can do this by using Carp::Always, overriding the $SIG{__DIE__} handler to generate a stack trace, or inserting a Carp::confess into your code.

    Here’s a sigdie solution, stick this in your code where it will run before your module initialization:

    $SIG{__DIE__} = sub { Carp::confess(@_) };
    

    You may need to put it in a BEGIN block.

    I’d really like to discourage you from taking this approach to building objects. You happily bless any random crap passed in to the constructor as part of your object! You blithely reach into your object internals. Field validation rules *do not belong in the constructor–they belong in the attribute mutators.

    If you must use a DIY object, clean up your practices:

    # Here's a bunch of validators.
    # I set them up so that each attribute supports:
    #   Multiple validators per attribute
    #   Distinct error message per attribute
    my %VALIDATORS = (
    
        some_attribute  => [
            [ sub { 'foo1' }, 'Foo 1 is bad thing' ],
            [ sub { 'foo2' }, 'Foo 2 is bad thing' ],
            [ sub { 'foo3' }, 'Foo 3 is bad thing' ],
        ],
        other_attribute => [ [ sub { 'bar' }, 'Bar is bad thing' ] ],
    
    );
    
    
    sub new {
        my $class = shift;  # Get the invocant
        my %args = @_;      # Get named arguments
    
        # Do NOT make this a clone method as well   
    
        my $self = {};
        bless $class, $self;
    
        # Initialize the object;
        for my $arg ( keys %args ) {
    
            # Make sure we have a sane error message on a bad argument.
            croak "Bogus argument $arg not allowed in $class\n"
                unless $class->can( $arg );
    
            $self->$arg( $args{$arg} );
        }
    
        return $self;
    }
    
    # Here's an example getter/setter method combined in one.
    # You may prefer to separate get and set behavior.
    
    sub some_attribute {
        my $self = shift;
    
        if( @_ ){
            my $val = shift;
    
            # Do any validation for the field
            $_->[0]->($val) or croak $_->[1]
                for @{ $VALIDATORS{some_attribute} || [] };
    
            $self->{some_attribute} = $val;
        }
    
        return $self->{some_attribute};
    
    }
    

    All this code is very nice, but you have to repeat your attribute code for every attribute. This means a lot of error-prone boilerplate code. You can get around this issue by learning to use closures or string eval to dynamically create your methods, or you can use one of Perl’s many class generation libraries such as Class::Accessor, Class::Struct, Accessor::Tiny and so forth.

    Or you can learn [Moose][3]. Moose is the new(ish) object library that has been taking over Perl OOP practice. It provides a powerful set of features and dramatically reduces boilerplate over classical Perl OOP:

    use Moose;
    
    type 'Foo'
        => as 'Int'
        => where {
            $_ > 23 and $_ < 42
        }
        => message 'Monkeys flew out my butt';
    
    has 'some_attribute' => (
        is  => 'rw',
        isa => 'Foo',
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Firstname and Lastname fields, I have set rules to 'required' but
I have some markup for a set of fields within a form: <ul> <li>
I have a set of data that contains garbled text fields because of encoding
I have an MVC application which has a set of fields for contact details
I have a set of data, and while the number of fields and tables
I have a gridivew with auto-generated fields, whenever there is a new set of
I have a MySQL table & fields that are all set to UTF-8. The
I have a mysql table field set as time type which stores data in
I have a packet having fields : Root Level : CHOICE Level 1 :
I have 2 different 2d Arrays set up in lua. The first loop bubbleMat

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.