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

As far as I understand, an URL consists of the folowing fields: Protocol (http,
I would like to understand the programmatic way of finding out(logging) IP address &
I want to understand how to set the parameters of properties (accessors). I took
Saw the following code snippet and I have problems to understand how it works.
I have a problem about the management of dates with milliseconds. I understand the
This is a tricky one to explain (and very weird), so bear with me.
I am looking for a way to do a fuzzy match using regular expressions.
I have 2 different web servers on a Debian Lenny machine. One is running
I have been through these related questions: How to convert numbers between hexadecimal and
I am checking some old SQL Statements for the purpose of documenting them and

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.