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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:50:02+00:00 2026-05-16T07:50:02+00:00

I thought I understood map however the following has results that I don’t understand.

  • 0

I thought I understood map however the following has results that I don’t understand. I know why it’s happening I just don’t know how it is happening.

The problem is that the contents of @array are changing because $_ is being reset during _do_stuff_to_file call. so what is printed is here: \nhere:\n when I expect it to be here: donkie\nhere: kong\n.

Note: This is not tested code. It’s just what I remember seeing from lab. Why are the contents of @array changing?

If I set $_ to $f before returning 1 from _some_func. Then the array is still intact.

Here is an example program to illustrate what I am seeing:

my @array = ("donkie", "kong");
map { push @junk, _some_func('blah', $_); } @array;

if (join ('', @junk) !~ /0/)
{   # for example sake this is always true since return 1 from _some_func.
    print map { "here: $_\n"; } @array;
}

sub _some_func
{   # for example sake, lets say $f always exists as a file.
    my $j = shift;
    my $f = shift;
    return 0 if !open(FILE, "< $f");
    close FILE;
    _do_stuff_to_file($f);

    return 1;
}


sub _do_stuff_to_file
{
    my $f = shift;
    open(IN, "< $f");
    open(OUT, "> $f.new");

    while (<IN>)
    {
        print OUT;
    }

    close IN;
    close OUT;
}
  • 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-16T07:50:02+00:00Added an answer on May 16, 2026 at 7:50 am

    Many functions in Perl use the default variable $_. Among these are map and the readline operator <>. Like foreach, map makes the loop variable an alias for each element of the list it processes. What’s happening is that this line:

    while (<IN>)
    

    is assigning to $_ while the aliasing of the map is in effect. This is one of the problems with using $_ (or any other global variable) — strange action at a distance. If you’re going to use $_, localize it first:

    local $_;
    while (<IN>)
    ...
    

    Alternately, use a lexical variable instead:

    while (my $line = <IN>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't claim to know anything about svn, but I thought I understood how
I still don't fully understand how map/reduce works, so I thought I'd give an
I thought I understood sed but I guess not. I have the following two
After I thought that I've understood how they work, I tried this: NSString *str1
I may not understand what the return statement does(I thought it just returned a
I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though,
I don't understand why this keeps happening... An hour ago I had the return
I really thought I understood Python variable referencing, so I'm confused why this code
Being a newbie I thought I understood what to do from a security standpoint
Initially I thought this was going to work, but now I understand it won't

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.