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

  • Home
  • SEARCH
  • 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 579401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:21:47+00:00 2026-05-13T14:21:47+00:00

With warnings enabled, perl usually prints Use of uninitialized value $foo if $foo is

  • 0

With warnings enabled, perl usually prints Use of uninitialized value $foo if $foo is used in an expression and hasn’t been assigned a value, but in some cases it’s OK, and the variable is treated as false, 0, or '' without a warning.

What are the cases where an uninitialized/undefined variable can be used without a warning?

  • 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-13T14:21:48+00:00Added an answer on May 13, 2026 at 2:21 pm

    Summary

    • Boolean tests
    • Incrementing or decrementing an undefined value
    • Appending to an undefined value
    • Autovivification
    • Other mutators

    Boolean tests

    According to the perlsyn documentation,

    The number 0, the strings '0' and '', the empty list (), and undef are all false in a boolean context. All other values are true.

    Because the undefined value is false, the following program

    #! /usr/bin/perl
    
    use warnings;
    
    my $var;
    print "A\n" if $var;
    $var && print "B\n";
    $var and print "C\n";
    print "D\n" if !$var;
    print "E\n" if not $var;
    $var or print "F\n";
    $var || print "G\n";
    

    outputs D through G with no warnings.

    Incrementing or decrementing an undefined value

    There’s no need to explicitly initialize a scalar to zero if your code will increment or decrement it at least once:

    #! /usr/bin/perl
    
    use warnings;
    
    my $i;
    ++$i while "aaba" =~ /a/g;
    print $i, "\n";
    

    The code above outputs 3 with no warnings.

    Appending to an undefined value

    Similar to the implicit zero, there’s no need to explicitly initialize scalars to the empty string if you’ll append to it at least once:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    my $str;
    for (<*>) {
      $str .= substr $_, 0, 1;
    }
    print $str, "\n";
    

    Autovivification

    One example is “autovivification.” From the Wikipedia article:

    Autovivification is a distinguishing feature of the Perl programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. In other words, Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.

    For example:

    #! /usr/bin/perl
    
    use warnings;
    
    my %foo;
    ++$foo{bar}{baz}{quux};
    
    use Data::Dumper;
    $Data::Dumper::Indent = 1;
    print Dumper \%foo;
    

    Even though we don’t explicitly initialize the intermediate keys, Perl takes care of the scaffolding:

    $VAR1 = {
      'bar' => {
        'baz' => {
          'quux' => '1'
        }
      }
    };

    Without autovivification, the code would require more boilerplate:

    my %foo;
    $foo{bar} = {};
    $foo{bar}{baz} = {};
    ++$foo{bar}{baz}{quux};  # finally!
    

    Don’t confuse autovivification with the undefined values it can produce. For example with

    #! /usr/bin/perl
    
    use warnings;
    
    my %foo;
    print $foo{bar}{baz}{quux}, "\n";
    use Data::Dumper;
    $Data::Dumper::Indent = 1;
    print Dumper \%foo;
    

    we get

    Use of uninitialized value in print at ./prog.pl line 6.
    
    $VAR1 = {
      'bar' => {
        'baz' => {}
      }
    };

    Notice that the intermediate keys autovivified.

    Other examples of autovivification:

    • reference to array

      my $a;
      push @$a => "foo";
      
    • reference to scalar

      my $s;
      ++$$s;
      
    • reference to hash

      my $h;
      $h->{foo} = "bar";
      

    Sadly, Perl does not (yet!) autovivify the following:

    my $code;
    $code->("Do what I need please!");
    

    Other mutators

    In an answer to a similar question, ysth reports

    Certain operators deliberately omit the “uninitialized” warning for your convenience because they are commonly used in situations where a 0 or “” default value for the left or only operand makes sense.

    These are: ++ and -- (either pre- or post-), +=, -=, .=, |=, ^=, &&=, ||=.

    Being “defined-or,” //= happily mutates an undefined value without warning.

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

Sidebar

Related Questions

In GCC, certain warnings require optimization to be enabled. For example: int foo() {
I'm using C++, code::blocks (with warnings enabled and I checked to make sure) and
I understand that CDT 7 will have a regular expression error parser included, but
LLVM 2.1 has an option that enables warnings for missing function prototypes. When enabled,
I'm getting a curious warning when pattern matching, but only when OverloadedStrings is enabled...
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir
Despite warnings to drop my present course of action, I currently see no better
I have turned on warnings as errors and now i need to XML comment
I can inhibit many warnings in Visual Studio 2005 SP1 in the C/C++ Advanced
Possible Duplicate: Custom Compiler Warnings Duplicate: Custom Compiler Warnings I'm new to writing my

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.