Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
reference/alias in perl vs reference/alias in C++ grocery_stocker 05-23-2008
Posted by grocery_stocker on May 24, 2008, 12:32 am
Please log in for more thread options
8c93a5d12ea@q27g2000prf.googlegroups.com:
>
> >> In article
>
> >> > 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-howto.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.

Posted by acehreli on May 23, 2008, 5:14 pm
Please log in for more thread options

> 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

Posted by grocery_stocker on May 23, 2008, 5:17 pm
Please log in for more thread options
> 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?

Posted by A. Sinan Unur on May 23, 2008, 6:13 pm
Please log in for more thread options
b9c2-f6a198a2be35@q24g2000prf.googlegroups.com:

>> 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__



--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by grocery_stocker on May 23, 2008, 6:22 pm
Please log in for more thread options
> b9c2-f6a198a2b...@q24g2000prf.googlegroups.com:
>
> >> 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__
>
> --
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/

I see. Thanks

Similar ThreadsPosted
PERL to mean what 'perldoc perl' says is wrong? (was: Re: perl should be improved and perl6) April 14, 2008, 11:37 pm
FAQ 3.0: What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? October 31, 2004, 12:03 pm
FAQ 3.0 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? February 2, 2005, 12:03 pm
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? March 28, 2005, 12:03 am
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? June 12, 2005, 11:03 am
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? August 29, 2005, 10:03 pm
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? October 28, 2005, 10:03 pm
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? December 25, 2005, 5:03 am
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? January 8, 2006, 11:03 pm
FAQ 2.18 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? January 16, 2006, 11:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap