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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:31:37+00:00 2026-06-16T23:31:37+00:00

This is my first question on SO, so sorry if it’s silly, but it’s

  • 0

This is my first question on SO, so sorry if it’s silly, but it’s something that really puzzled me when I recently came across it in production code. I’ve boiled my problem down to the two blocks of code, which I expected to do the same thing, namely produce a random number for each iteration:

for my $num (0 .. 5) {
    my $id = int rand 10;
    print "$id\n";    
}

and

for (0 .. 5) {
    my $tmp;
    my $id = $tmp if $tmp;

    $id = int rand 10 unless $id;
    print "$id\n";
}

The first one does what I expect it to do, but the second one gives the same number for any number of iterations. $tmp is always undefined in this simplification, so it’s only there to show the behaviour, as leaving out = $tmp if $tmp produces the result I’d expect.

I’d appreciate any insight into why this happens.

  • 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-16T23:31:38+00:00Added an answer on June 16, 2026 at 11:31 pm

    The reason for the strange behaviour is that you have made the declaration of $id, as well as the assignment to it, conditional on the truth of $tmp, which makes Perl throw a fit. perldoc perlsyn has this to say about it

    NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if … ) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don’t rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

    You can demonstrate this for yourself if you change the code as follows, which works fine.

    for (0 .. 5) {
        my $tmp;
        my $id;
        $id = $tmp if $tmp;
    
        $id = int rand 10 unless $id;
        print "$id\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I am sorry, I wanted to ask this question on meta first, but it
This is my first question on this platform. I am sorry if something is
this is my first question here so sorry if I do something wrong or
this is my first question, sorry if something is wrong Well I'm trying to
I am sorry that they ask this question has been asked many times but
I'm sorry but this is my first question on here. I'm doing my school
Sorry, this's my first time to ask a question here. So, I don't have
First off, sorry if this is a stupid question. I installed code::blocks to try
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
Well, first of all sorry about this question it must be pretty straight forward

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.