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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:09:40+00:00 2026-05-31T05:09:40+00:00

I have the following script: use 5.12.4; use strict; use warnings; say Enter a

  • 0

I have the following script:

use 5.12.4;
use strict;
use warnings;

say "Enter a functionality:";
while (<>) {
    if (/add/) {
        say "Enter your numbers:";
        my @a = (<>);
        my $sum += $_ for @a;
        say $sum;
    }
}

When I run this program, it prompts:

Enter a functionality:

I enter add and it says:

Enter your numbers:

I enter several numbers on separate lines of input follow by the [ctrl]Z and get the following error:

Use of uninitialized value $sum in say at C:\myperl\Math-Master\math-master.pl l
ine 11, <> line 9.

Why doesn’t my code add all of the input? Why does this error come?

  • 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-31T05:09:40+00:00Added an answer on May 31, 2026 at 5:09 am

    You cannot use a postscript loop on a declaration statement. The variable $sum is supposed to be incremented each loop, which it cannot be in the same statement it is being declared. You must first declare it, then assign to it with a postscript loop:

    my $sum;
    $sum += $_ for @a;
    

    You might consider using List::Util for this, and skipping the temp variable @a. And moving the say inside the while loop:

    use List::Util qw(sum);
    
    say "Enter a functionality:";
    while (<>) {
        if (/add/) {
            say "Enter your numbers:";
            say "Sum: ", sum(<>);
        }
        say "Enter a functionality:";
    }
    

    But that is a bit clunky. Why not:

    while (<>) {
        if (/add/) {
            say "Enter your numbers, separated by space: ";
            say "Sum: ", sum(split " ", <>);
        }
    }
    

    That way, you do not have to press ctrl-Z (ctrl-D) to stop the input.

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

Sidebar

Related Questions

I have the following, simplest Perl CGI script: use strict; use warnings; use CGI();
I have the following script: #!/usr/bin/perl use warnings; use strict; my $count = 0;
Consider the following nonsense script as an example: use strict; use warnings; my $uninitialisedValue;
I am using the following script: #!/usr/local/bin/perl -wT use strict; use warnings; print Content-type:
I have an image uploading script in which i use the following setup to
I have the following script that I run as a custom build step in
I have written the following Perl script- use HTML::TreeBuilder; my $html = HTML::TreeBuilder->new_from_content(<<END_HTML); <span
I have following script: #!/usr/bin/python while True: x = raw_input() print x[::-1] I am
The following is the script for finding consecutive substrings in strings. use strict; use
I have the following Perl script. I am trying to run it in Windows

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.