473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Does Perl Always Pass Argument By Reference?

Hello,

I read on one web page that Perl always passes its arguments by reference.
But on another web page, it was said that Perl can pass its argument by
value :
Expand|Select|Wrap|Line Numbers
  1.    printHash(%hash);
  2.  
Could someone please tell me if the second web page is correct?

Thank you - Akino
Jan 24 '15 #1

✓ answered by Kelicula

If you pass an anonymous hash it will be treated as a reference.
eg:
$hash = { name => 'Jim', age => '29'};

5 1767
Kelicula
176 Expert 100+
You need to be clearer as to where you are talking about perl passing things to. Do you mean mean passing arguments to a subroutine? A hash in perl is really an array, (not really but when it comes to passing things) if you pass a named hash to a subroutine (in certain context) it will be "flattened" into an array where the first element is the "key" and the second is the "value" and so on... as in:
%hash = ( name => 'Jim', age => '29');
will become:
(name, Jim, age, 29);
When passed...
Jan 26 '15 #2
Kelicula
176 Expert 100+
If you pass an anonymous hash it will be treated as a reference.
eg:
$hash = { name => 'Jim', age => '29'};
Jan 26 '15 #3
Hi Kelicula,

Thank you very much for your reply.

Akino
Jan 26 '15 #4
Kelicula
176 Expert 100+
When passing to subroutines, the convention is; if there are more than 3-4 things you are passing you should probably be using an anonymous hash, otherwise you can just pass the value directly.
eg:
Expand|Select|Wrap|Line Numbers
  1. sub passing_things {
  2.     my $thing = shift;
  3.     do stuff with thing passed...
  4. }
  5. # Then later...
  6. passing_things($variable);
  7.  
  8.  
The value in $variable will be passed to the subroutine.
However if you find you are sending a lot of things you can just use an anonymous hash:

Expand|Select|Wrap|Line Numbers
  1. sub passing_more_things {
  2.     my $things = shift;
  3.     do many things with lots of stuff...
  4. }
  5. # then later...
  6. passing_more_things({ one => $var, two => $var2, three => $var3, four => $var4});
  7.  
  8. # If you want to you can pass different kinds of things explicitly:
  9. passing_more_things($variable, { one => $var, two => $var2}, @array);
  10.  
  11.  
In that case all that stuff will be "flattened" into an anonymous array "@_" inside the sub:
Expand|Select|Wrap|Line Numbers
  1. sub big_passing {
  2.     my @all_the_stuff = @_;
  3. }
  4. # Then later
  5. my $var = 'hello';
  6. my @array = ('perl', 'is', 'very', 'flexible');
  7.  
  8. big_passing($var, @array, { kelicula => 'loves', all => 'things_perl'});
the contents of @all_the_stuff would be:
hello, perl, is, very, flexible, kelicula, loves, all, things_perl.

Get it?
Jan 26 '15 #5
Yes, I understand it now. Thank you.
Feb 1 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Randell D. | last post by:
Folks, I've asked this before but never got any response but its important and I thought I'd pitch it again (hopefully a bit clearer)... One can pass data from one function to another either...
14
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual techinical stuff.....i have had a lot of hot debate...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: Morgan | last post by:
I have read much posts on the argument but no one clearly says if this operation is possible or not. Simply I have a routine which reads from a text file some integer arrays (1 or 2D). The...
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
21
by: jerico | last post by:
Hi.Does C support pass by reference or only pass by value?If we pass the address of a variable and take it in the function definition with a pointer, then will it be pass by reference?Thanks for...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.