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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:58:04+00:00 2026-05-18T02:58:04+00:00

I have a Perl script where variables must be initialized before the script can

  • 0

I have a Perl script where variables must be initialized before the script can proceed. A lengthy if statement where I check each variable is the obvious choice. But maybe there is a more elegant or concise way to check several variables.

Edit:
I don’t need to check for “defined”, they are always defined with an empty string, I need to check that all are non-empty.

Example:

my ($a, $b, $c) = ("", "", "");

# If-clauses for setting the variables here

if( !$a || !$b || !$c) {
  print "Init failed\n";
}
  • 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-18T02:58:04+00:00Added an answer on May 18, 2026 at 2:58 am

    I am assuming that empty means the empty string, not just any false value. That is, if 0 or "0" are ever valid values post-initialization, the currently accepted answer will give you the wrong result:

    use strict; use warnings;
    
    my ($x, $y, $z) = ('0') x 3;
    # my ($x, $y, $z) = ('') x 3;
    
    for my $var ($x, $y, $z) {
        die "Not properly initialized\n" unless defined($var) and length $var;
    }
    

    Now, this is pretty useless as a validation, because, more than likely, you would like to know which variable was not properly initialized if this situation occurs.

    You would be better served by keeping your configuration parameters in a hash so you can easily check which ones were properly initialized.

    use strict; use warnings;
    
    my %params = (
        x => 0,
        y => '',
        z => undef,
    );
    
    while ( my ($k, $v) = each %params ) {
        validate_nonempty($v)
            or die "'$k' was not properly initialized\n";
    }
    
    sub validate_nonempty {
        my ($v) = @_;
        defined($v) and length $v;
    }
    

    Or, if you want to list all that were not properly initialized:

    my @invalid = grep is_not_initialized($params{$_}), keys %params;
    die "Not properly initialized: @invalid\n" if @invalid;
    
    sub is_not_initialized {
        my ($v) = @_;
        not ( defined($v) and length $v );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that sets up variables near the top for directories
I have Perl script and need to determine the full path and filename of
I have a Perl script that I'm attempting to set up using Perl Threads
I have a Perl script where I maintain a very simple cache using a
I have this Perl script with many defined constants of configuration files. For example:
I have a Perl script that I'd like to run on Windows, using either
I have a Perl script that pops up a message box when its work
My problem is: I have a perl script which uses lot of memory (expected
I have a little Perl script (On Windows) that checks some files for me
I have an existing Perl script that uses the FTP object to send a

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.