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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:54:13+00:00 2026-05-15T05:54:13+00:00

I do not get to understand how the Perl read($buf) function is able to

  • 0

I do not get to understand how the Perl read($buf) function is able to modify the content of the $buf variable. $buf is not a reference, so the parameter is given by copy (from my c/c++ knowledge). So how come the $buf variable is modified in the caller ?

Is it a tie variable or something ? The C documentation about setbuf is also quite elusive and unclear to me

# Example 1
$buf=''; # It is a scalar, not a ref
$bytes = $fh->read($buf);
print $buf; # $buf was modified, what is the magic ?

# Example 2
sub read_it {
    my $buf = shift;
    return $fh->read($buf);
}
my $buf;
$bytes = read_it($buf);
print $buf; # As expected, this scope $buf was not modified
  • 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-15T05:54:14+00:00Added an answer on May 15, 2026 at 5:54 am

    No magic is needed — all perl subroutines are call-by-alias, if you will. Quoth perlsub:

    The array @_ is a local array, but its elements are aliases
    for the actual scalar parameters. In particular, if an element $_[0]
    is updated, the corresponding argument is updated (or an error occurs
    if it is not updatable).

    For example:

    sub increment {
      $_[0] += 1;
    }
    
    my $i = 0;
    increment($i);  # now $i == 1
    

    In your “Example 2”, your read_it sub copies the first element of @_ to the lexical $buf, which copy is then modified “in place” by the call to read(). Pass in $_[0] instead of copying, and see what happens:

    sub read_this {
      $fh->read($_[0]);  # will modify caller's variable
    }
    sub read_that {
      $fh->read(shift);  # so will this...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simply setting the SVN_EDITOR variable to mate does not get the job done. It
I have done jQuery and Ajax, but I am not able to get the
Just could not get this one and googling did not help much either.. First
Here is some code I could not get to format properly in markdown, this
Sometimes I get a broken background in Chrome. I do not get this error
I am completely stumped as to why this code does not get any SDL
With LINQ to SQL most likely going to not get as much active development
I want to write Html format, but I can not even get a simple
I would like to stop images from loading, as in not even get a
i get a Keyword not supported: '192.168.1.1;initial catalog'. error when trying to do this

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.