What is the magic behind perl read() function and buffer which is not a ref ?
        Posted  
        
            by alex8657
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by alex8657
        
        
        
        Published on 2010-06-10T04:33:55Z
        Indexed on 
            2010/06/10
            4:42 UTC
        
        
        Read the original article
        Hit count: 305
        
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
© Stack Overflow or respective owner