Disappointed by perl..

E

Emmanuel Florac

Le Mon, 08 May 2006 20:23:10 +0200, chacallot a écrit :
???
That's completely dumb.
There is no way you could do the kind of thing I talk about in the
"Variables scope. Am lost..." thread in C, C++ or Java.

I couldn't guess what exactly your problems are, could I?
 
C

chacallot

Emmanuel said:
Le Mon, 08 May 2006 20:23:10 +0200, chacallot a écrit :


I couldn't guess what exactly your problems are, could I?

Sure you can if you know what you're talking about.
But can you really give an example of an equivalent code with the same
result as the example I gave in the thread am talking about but in C,
C++ or Java (this thread is in ocmp.lang.perl.misc, subject is
"Variables scope. Am lost..."?

Rgds,
Chacallot.
 
A

A. Sinan Unur

Sure you can if you know what you're talking about.

But can you really give an example of an equivalent code with the same
result as the example I gave in the thread am talking about but in C,
C++ or Java (this thread is in ocmp.lang.perl.misc, subject is
"Variables scope. Am lost..."?

You don't know how to program in any language do you?

D:\Home\asu1\UseNet\clpmisc> cat scoping.c
#include <stdio.h>

int toto;

void titi(void) {
++toto;
printf("in sub toto=%d\n", toto);
}

int main(void) {
for(int i = 0; i < 3; ++i) {
int toto = i;
printf("before sub toto=%d\n", toto);
titi();
printf("after sub toto=%d\n", toto);
}
return 0;
}

D:\Home\asu1\UseNet\clpmisc> gcc -std=c99 scoping.c -o scoping.exe

D:\Home\asu1\UseNet\clpmisc> scoping
before sub toto=0
in sub toto=1
after sub toto=0
before sub toto=1
in sub toto=2
after sub toto=1
before sub toto=2
in sub toto=3
after sub toto=2

There you go. Now, *PLONK*

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
C

chacallot

A. Sinan Unur said:
You don't know how to program in any language do you?

D:\Home\asu1\UseNet\clpmisc> cat scoping.c
#include <stdio.h>

int toto;

void titi(void) {
++toto;
printf("in sub toto=%d\n", toto);
}

int main(void) {
for(int i = 0; i < 3; ++i) {
int toto = i;
printf("before sub toto=%d\n", toto);
titi();
printf("after sub toto=%d\n", toto);
}
return 0;
}

D:\Home\asu1\UseNet\clpmisc> gcc -std=c99 scoping.c -o scoping.exe

D:\Home\asu1\UseNet\clpmisc> scoping
before sub toto=0
in sub toto=1
after sub toto=0
before sub toto=1
in sub toto=2
after sub toto=1
before sub toto=2
in sub toto=3
after sub toto=2

There you go. Now, *PLONK*

Sinan


Hi!

The only thing you're proving is that you havent read the thread am
talking about, or you didnt understand it.

In your example, at each iteration the in sub value is equal to the out
sub value +1.
In my example, this is only true after the second iteration.

Rgds,
Chacallot.
 
M

Mirco Wahab

Hi chacallot
The only thing you're proving is that you havent read the thread am
talking about, or you didnt understand it.

Of course, your "scoping problem" was very interesting (to me),
but why would you have to dive into these arguments ...
In your example, at each iteration the in sub
value is equal to the out sub value +1.
In my example, this is only true after the second iteration.

This is correct like you said.

There is not the slightest chance in C/C++ to
get the desires 'Pseudo-Closure'-behavior of
your former example.

The scoping problem narrows down to an
_initialisation artifact_ of a named
subroutine inside a block.

You can see this clearly if you display
the pointers to the variables which Perl touches:

for (my $i=1;$i<4;$i++) {
my $toto;

sub titi {
my $r0 =\$toto;
$toto++;
print "--> in sub toto=$toto \t($r0)\n";
}

$toto=$i;

my $r1 =\$toto;
print "< before sub toto=$toto \t($r1)\n";

titi;

my $r2 =\$toto;
print ">after sub toto=$toto \t($r2)\n";
}

Prints:

< before sub toto=1 (SCALAR(0x225e98))
--> in sub toto=2 (SCALAR(0x225e98))
after sub toto=2 (SCALAR(0x225e98))
< before sub toto=2 (SCALAR(0x225178)) <--- new iteration
--> in sub toto=3 (SCALAR(0x225e98))
after sub toto=2 (SCALAR(0x225178)) <--- gets destroyed
< before sub toto=3 (SCALAR(0x225178)) <--- new iteration (but same address)
--> in sub toto=4 (SCALAR(0x225e98))
after sub toto=3 (SCALAR(0x225178)) <--- gets destroyed

As you can see, you have closure-like
behavior, the sub alwais hangs on the
initial variable $toto (which gets
reinitialized each iteration) and keeps
that for its lifetime.

Regards

Mirco
 
A

A. Sinan Unur

[ Please do not snip attributions. Below, >>> are comments made by me,
and >> are chacallot's responses. ]
Of course, your "scoping problem" was very interesting (to me),
but why would you have to dive into these arguments ...

I misunderstood chacallot's post because:

1) I had to keep going back and forth between messages to try and
understand what he/she was talking about.

2) The code he/she posted was not indented, making it hard to see that
sub titi was defined in the body of the for loop.

So, apologies for the misunderstanding.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
M

Michele Dondi

I have looked back at the manuals and I didnt find anyplace where I
could have been warned about that. Specially in perlintro about Arrays,

OTOH there's a faq entry for that:
perldoc -q "difference between a list and an array"
"There are two major contexts: list and scalar. Certain operations
return list values in contexts wanting a list, and scalar values otherwise"

Even worse, the function to determine if a sub (or eval()) is being
called in list context is wantarray(), not wantlist()... you just have
to live with that! Now you know!


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top