473,408 Members | 2,405 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,408 software developers and data experts.

reference/alias in perl vs reference/alias in C++

How are references and aliases in perl different than references in
aliases in C++?
Jun 27 '08 #1
9 1707
In article
<8f**********************************@u12g2000prd. googlegroups.com>,
grocery_stocker <cd*****@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?
Some differences (not exhaustive):

Aliases in Perl are like references in C++: a different way of
accessing the same object.

References in Perl are like pointers in C/C++: indirect access to an
object. However, in Perl you cannot do arithmetic on references the way
you can for C/C++ pointers (e.g. *(p++) = getchar();)

In Perl, references are scalars, not separate pointer types as in C/C++.

In C/C++, arrays are really pointers to the first element in a
contiguous set of elements. In Perl, arrays are separate objects. A
reference to an array in Perl is different than a reference to one of
the members of the array.

--
Jim Gibson
Jun 27 '08 #2
On May 23, 12:22 pm, Jim Gibson <jimsgib...@gmail.comwrote:
In article
<8f8a7081-f03a-4063-9999-36ba6dd16...@u12g2000prd.googlegroups.com>,

grocery_stocker <cdal...@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?

Some differences (not exhaustive):

Aliases in Perl are like references in C++: a different way of
accessing the same object.

References in Perl are like pointers in C/C++: indirect access to an
object. However, in Perl you cannot do arithmetic on references the way
you can for C/C++ pointers (e.g. *(p++) = getchar();)

In Perl, references are scalars, not separate pointer types as in C/C++.

In C/C++, arrays are really pointers to the first element in a
contiguous set of elements. In Perl, arrays are separate objects. A
reference to an array in Perl is different than a reference to one of
the members of the array.

--
Jim Gibson
Everytime I ask a question on the newsgroup, i keep on thinking "I'm
sure things would have been a lot easier if I would have taken more
than 6 week of FORTRAN." I don't care what anyone says. Learning to
program on your own. mastering the core concepts without formal
schooling, and then actually making it as a programmer takes a certain
level of skill and internal drive. Not everyone has it.

I think I only know a few people with no more than a high school
education that are doing the same kind of work, for the exact same
pay, as a person with an advanced degree in the sciences.
Jun 27 '08 #3
>On May 23, 12:22 pm, Jim Gibson <jimsgib...@gmail.comwrote:
>grocery_stocker <cdal...@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?
>References in Perl are like pointers in C/C++: indirect access to an
object. However, in Perl you cannot do arithmetic on references the way
you can for C/C++ pointers (e.g. *(p++) = getchar();)
Another very major difference: in C pointers are just (virtual) memory
addresses and the programmer is forced to implement his own memory
management for objects that are addressed by pointers.
In Perl any memory management for objects referred to by references is
done automatically by the system.

jue
Jun 27 '08 #4
On May 23, 12:22 pm, Jim Gibson <jimsgib...@gmail.comwrote:
In C/C++, arrays are really pointers to the first element in a
contiguous set of elements.
That is a common misconception.

An array is the contiguous set of elements that you mention; there is
no extra pointer. True, the name of the array decays to a pointer to
first element in certain contexts; but still, there is no extra
pointer to make an array.
In Perl, arrays are separate objects. A
reference to an array in Perl is different than a reference to one of
the members of the array.
That is exactly the case in C++ as well. Here is a program with a
function that takes an array as a separate object (not as a pointer to
its first element):

#include <assert.h>

typedef int TwoIntArray[2];

void as_array(TwoIntArray & array)
{
// Here, the type is int[2]
assert(sizeof(array) == sizeof(TwoIntArray));
}

void as_pointer(int * p)
{
// Here, the type is int*
assert(sizeof(p) == sizeof(int*));
}

int main()
{
int array[2] = { 7, 42 };

as_array(array);

// Decays to pointer to first member
as_pointer(array);
}

Ali
Jun 27 '08 #5
On May 23, 10:48 am, grocery_stocker <cdal...@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?
And going off on a tanget, given something like

#!/usr/bin/perl -w

# global array definition
my @array = ("a","b","c");

sub print_array {
foreach my $element (@array) {
$element .= "9";
print $element . "\n";
}

}

for ($i = 0; $i < 3; $i++) {
&print_array();
}

How would I prevent $elment from modifying @array?
Jun 27 '08 #6
grocery_stocker <cd*****@gmail.comwrote in news:25dad38d-0e82-4c19-
b9***************@q24g2000prf.googlegroups.com:
On May 23, 10:48 am, grocery_stocker <cdal...@gmail.comwrote:
>How are references and aliases in perl different than references in
aliases in C++?

And going off on a tanget, given something like

#!/usr/bin/perl -w
use warnings;

is in general preferable to -w.

You forgot:

use strict;
# global array definition
Useless comment (it is also wrong).
my @array = ("a","b","c");
my @array = qw( a b c ); # easier on the eyes
sub print_array {
foreach my $element (@array) {
$element .= "9";
print $element . "\n";
}

}

for ($i = 0; $i < 3; $i++) {
&print_array();
}
First off, omit the & unless you know and desire its specific effect in
this case.
How would I prevent $elment from modifying @array?
$element is not modifying anything. $element is an alias to the current
element of @array. The statement you wrote

$element .= "9";

is modifying the contents of @array in each iteration of the loop.

The easiest way to avoid modifying the contents of @array would be for
you not to modify the contents of the array.

The name print_array is simply bad. The subroutine does not just print
the contents of the array but prints some modification of the elements
of the array.

Depending on what you actually want to do, there are many different ways
of writing such a subroutine (each of which is infinitely better than
what you wrote).

Some examples below. Some of these are sillier than the others.

#!/usr/bin/perl

use strict;
use warnings;

my @array = qw( a b c d e f g h i j k l );
my %hash = @array;

print "${_}9\n" for @array;

print map { "${_}9\n" } @array;

print_with_suffix( 9 =\@array );

print_with_suffix2( 9 =@array, \@array, \%hash );

sub print_with_suffix {
my ($suffix, $array_ref) = @_;
return unless ref $array_ref eq 'ARRAY';
print "${_}$suffix\n" for @$array_ref;
}

sub print_with_suffix2 {
my $suffix = shift;

for my $arg ( @_ ) {
if ( ref $arg eq 'ARRAY' ) {
print_with_suffix( $suffix, $arg );
}
elsif ( ref $arg eq 'HASH' ) {
print_with_suffix( $suffix, [ values %$arg ] );
}
else {
print "$arg$suffix\n";
}
}
}

__END__

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
Jun 27 '08 #7
grocery_stocker <cd*****@gmail.comwrote:
[...]
>my @array = ("a","b","c");
[...]
foreach my $element (@array) {
$element .= "9";
[...]
>How would I prevent $elment from modifying @array?
$element doesn't modify anything. If at all it is modified by the
assignment and because it is an alias to to each @array element those
are modified, too.

Solution: just create a copy:
my @array = ("a","b","c");
[...]
foreach (@array) {\
my $element = $_ # create copy instead of aliasing
$element .= "9";

jue
Jun 27 '08 #8
grocery_stocker <cd*****@gmail.comwrote in
news:7e**********************************@q27g2000 prf.googlegroups.com:
On May 23, 12:22 pm, Jim Gibson <jimsgib...@gmail.comwrote:
>In article
<8f8a7081-f03a-4063-9999-36ba6dd16...@u12g2000prd.googlegroups.com>,

grocery_stocker <cdal...@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?
[ snip Jim's explanation ]
>--
Jim Gibson
[ Do *NOT* quote sigs ]
Everytime I ask a question on the newsgroup, i keep on thinking "I'm
sure things would have been a lot easier if I would have taken more
than 6 week of FORTRAN."
I did do some FORTRAN programming almost 20 years ago. I am not sure
what you are getting at though.
I don't care what anyone says.
Well, I am reminded of

http://www.catb.org/~esr/faqs/hacker....html#believe5
Learning to program on your own.
http://en.wikipedia.org/wiki/Sentence_%28linguistics%29
mastering the core concepts without formal
schooling, and then actually making it as a programmer takes a certain
level of skill and internal drive.
Are you referring to yourself here?

I am not sure what "making it as a programmer" means above. On the other
hand, almost everyday at work is an opportunity for me to run into
someone who thinks he/she has made it as a programmer. I am not sure I
agree with those people's self-assessments.
Not everyone has it.
True.
I think I only know a few people with no more than a high school
education that are doing the same kind of work, for the exact same
pay, as a person with an advanced degree in the sciences.
The only thing that shows me is that the person with the advanced degree
in the sciences has chosen not to work in the field in which he/she
earned the degree.

Clearly, once one has a certain mental capability, whether one chooses
to invest time in an advanced degree is a matter of preference. Another
person with even superior mental capacity may choose not to "waste" five
to seven years toiling on a project which is of interest to only a few
people and which, as a norm, do not generate huge monetary returns on
that investment. This is why I do not put much stock in letters before
or after a person's name.

Achieving that goal also takes a certain level of skill and drive.

If I were you, I would not be so quick to pat myself on the back for
this particular reason until I were able to compete with Physics Ph.D.'s
in the fields in which they earned their degrees.

You can be proud of your achievements without resorting to this silly
argument.

Switching back to discussing Perl ... now.

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
Jun 27 '08 #9
On May 23, 3:44*pm, "A. Sinan Unur" <1...@llenroc.ude.invalidwrote:
grocery_stocker <cdal...@gmail.comwrote innews:7e**********************************@q27g20 00prf.googlegroups.com:
On May 23, 12:22 pm, Jim Gibson <jimsgib...@gmail.comwrote:
In article
<8f8a7081-f03a-4063-9999-36ba6dd16...@u12g2000prd.googlegroups.com>,
grocery_stocker <cdal...@gmail.comwrote:
How are references and aliases in perl different than references in
aliases in C++?

[ snip Jim's explanation ]
--
Jim Gibson

[ Do *NOT* quote sigs ]
Everytime I ask a question on the newsgroup, i keep on thinking "I'm
sure things would have been a lot easier if I would have taken more
than 6 week of FORTRAN."

I did do some FORTRAN programming almost 20 years ago. I am not sure
what you are getting at though.
Well, FORTRAN was my first formal introduction to structured
programming. Is 6 weeks enough to actually learn how to think
logically?
>
I don't care what anyone says.

Well, I am reminded of

http://www.catb.org/~esr/faqs/hacker....html#believe5
Learning to program on your own.

http://en.wikipedia.org/wiki/Sentence_%28linguistics%29
mastering the core concepts without formal
schooling, and then actually making it as a programmer takes a certain
level of skill and internal drive.

Are you referring to yourself here?
No. I think some people that come mind are certain former Netscape and
FreeBSD engineers.
I am not sure what "making it as a programmer" means above. On the other
hand, almost everyday at work is an opportunity for me to run into
someone who thinks he/she has made it as a programmer. I am not sure I
agree with those people's self-assessments.
Not everyone has it.

True.
I think I only know a few people with no more than a high school
education that are doing the same kind of work, for the exact same
pay, as a person with an advanced degree in the sciences.

The only thing that shows me is that the person with the advanced degree
in the sciences has chosen not to work in the field in which he/she
earned the degree.

Clearly, once one has a certain mental capability, whether one chooses
to invest time in an advanced degree is a matter of preference. Another
person with even superior mental capacity may choose not to "waste" five
to seven years toiling on a project which is of interest to only a few
people and which, as a norm, do not generate huge monetary returns on
that investment. This is why I do not put much stock in letters before
or after a person's name.

Achieving that goal also takes a certain level of skill and drive.

If I were you, I would not be so quick to pat myself on the back for
this particular reason until I were able to compete with Physics Ph.D.'s
in the fields in which they earned their degrees.

You can be proud of your achievements without resorting to this silly
argument.

Switching back to discussing Perl ... now.
Yes. We now go back to our regular discussion on Perl.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
4
by: mexican_equivalent | last post by:
X-no-archive: yes Newbie C++ programmer reads the following statement from the C++ 'Deitel & Deitel' textbook: "Reference variables can be used as local aliases within a function. They must...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
8
by: junw2000 | last post by:
Hi, My textbook says that: "Once defined, a reference cannot be reassigned because it is an alias to its target. What happens when you try to reassign a reference turns out to be the assignment...
10
by: Summercool | last post by:
so many places, including the book PHP in a Nutshell, p. 80, it says: $a =& $b # set $a to reference $b if $a reference $b, then while you can say $b =1, you can't really say $a = 1. you...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.