Why `for local $var (LIST) ...' not allowed?

T

Todd

Why can't we explicit using local loop variable in the code snippet
below?

#! /bin/perl

for local $a (1..2) {
print $a;
}

__END__
syntax error at - line 3, near "for local"
Execution of - aborted due to compilation errors.

You see here `my'/`our' are allowed in the code snippet below, why not
`local'?

#! /bin/perl

for my $a (1..2) {
print $a;
}

__END__

12

#! /bin/perl

for our $a (1..2) {
print $a;
}

__END__
12


-Todd
 
J

John W. Krahn

Todd said:
Why can't we explicit using local loop variable in the code snippet
below?

Because it is a syntax error.
#! /bin/perl

for local $a (1..2) {
print $a;
}

__END__
syntax error at - line 3, near "for local"
Execution of - aborted due to compilation errors.

You see here `my'/`our' are allowed in the code snippet below, why not
`local'?

Because that is the way perl works.



John
 
U

Uri Guttman

T> Why can't we explicit using local loop variable in the code snippet
T> below?

T> #! /bin/perl

T> for local $a (1..2) {
T> print $a;
T> }

T> __END__
T> syntax error at - line 3, near "for local"
T> Execution of - aborted due to compilation errors.

T> You see here `my'/`our' are allowed in the code snippet below, why not
T> `local'?

why would you want local? do you know the difference between local and
my? do you know you can localize an our variable? do you also know not
to use $a (even in examples) as it is special cased with strict? this is
important as you are playing with scoped variables but i don't see
strict enabled.

uri
 
T

Todd

Uri said:
why would you want local? do you know the difference between local and
my? do you know you can localize an our variable? do you also know not
to use $a (even in examples) as it is special cased with strict? this is
important as you are playing with scoped variables but i don't see
strict enabled.

It's only an example. BTW, you seems like showing yourself as `guru'
rather than answer the question. Or did you answer the question?

-Todd
 
T

Tad J McClellan

Todd said:
It's only an example.


Don't use $a for anything other than sorting, even in examples.



You should have resisted the urge to get smartass.

Off to the killfile you go...

you seems like showing yourself as `guru'
rather than answer the question.


Do you want to learn from a guru or from another newbie?

Or did you answer the question?


He did.

If you researched the answers to his 4 questions, you would have
learned how to localize foreach's loop control variable, along
with 3 other things that are important to understand if you
hope to do much programming in Perl.
 
U

Uri Guttman

T> It's only an example. BTW, you seems like showing yourself as `guru'
T> rather than answer the question. Or did you answer the question?

do you like showing yourself off to be an idiot? i asked you a couple of
questions to see if you have any real basis for asking your
question. you obviously don't. perl doesn't support local that
way. it actually makes little sense do it as it is trivial to work
around. but you don't seem to know why local is bad in most cases
anyhow. if you did you wouldn't ask such a question and you would
quickly know how to get the same results. (hint: localize a var BEFORE
the loop).

there, i answered the question. will you answer mine? i doubt it.

uri
 
T

Todd

OK, let me reply you one by one:

Uri said:
why would you want local?
I don't want to use local. I like to use `my' here. But I just want to
know why `local' is not allowed here, so i put the example.
do you know the difference between local and my?
Why i dont know it? just try perldoc below if you want to:

perldoc -q local my

You'll the FAQ about the difference at faq7.
do you know you can localize an our variable?
Yes. `our' variable is just a global variable.
do you also know not to use $a (even in examples) as it is special cased with strict?
The result is same if use `$aaa' instead of $a, even i know $a is
magical, but why bother to be boring of this?


So you answer my question ?!!!? Seems you don't know the reason behind
it or the anser would be very simple rather than long useless paragrah
saying nothing useful.

-Todd
 
U

Uri Guttman

T> OK, let me reply you one by one:

T> I don't want to use local. I like to use `my' here. But I just want to
T> know why `local' is not allowed here, so i put the example.

then why do you care about local? you rarely need it and it is not
needed for loop variables. so perl doesn't allow it as it has no good
use there. simple. don't try to use local on a loop variable.
T> Why i dont know it? just try perldoc below if you want to:

T> perldoc -q local my

huh??

T> You'll the FAQ about the difference at faq7.

huh?? speak english.
T> Yes. `our' variable is just a global variable.
T> The result is same if use `$aaa' instead of $a, even i know $a is
T> magical, but why bother to be boring of this?

boring?

T> So you answer my question ?!!!? Seems you don't know the reason behind
T> it or the anser would be very simple rather than long useless paragrah
T> saying nothing useful.

and you ask nothing useful. i told you in another post the hint on how
to work around this.

uri
 
T

Todd

Uri said:
T> OK, let me reply you one by one:


T> I don't want to use local. I like to use `my' here. But I just want to
T> know why `local' is not allowed here, so i put the example.

then why do you care about local? you rarely need it and it is not
needed for loop variables. so perl doesn't allow it as it has no good
use there. simple. don't try to use local on a loop variable.

T> Why i dont know it? just try perldoc below if you want to:

T> perldoc -q local my

huh??

T> You'll the FAQ about the difference at faq7.

huh?? speak english.

T> Yes. `our' variable is just a global variable.

T> The result is same if use `$aaa' instead of $a, even i know $a is
T> magical, but why bother to be boring of this?

boring?

T> So you answer my question ?!!!? Seems you don't know the reason behind
T> it or the anser would be very simple rather than long useless paragrah
T> saying nothing useful.

and you ask nothing useful. i told you in another post the hint on how
to work around this.

I like the answer of this run. Why can't you be kind at first?

-Todd
 
J

Joost Diepenmaat

Todd said:
I like the answer of this run. Why can't you be kind at first?

The answer is: it doesn't work that way because nobody's bothered to
imlement it because it's extremely uncommon to need a local() loop
variable ($_ excepted).
 
A

Achim Peters

Tad said:
Don't use $a for anything other than sorting, even in examples.

Agreed, but shouldn't the FAQ be a shining example of that rule? ;-)

* http://faq.perl.org/perlfaq6.html :
| How do I substitute case insensitively on the LHS while
| preserving case on the RHS?
....
| $a = "this is a TEsT case";
| $a =~ s/(test)/preserve_case($1, "success")/egi;
| print "$a\n";

* http://faq.perl.org/perlfaq7.html :
| How do I temporarily block warnings?
....
| $a = $b + $c; # I know these might be undef

and

| Why do Perl operators have different precedence than C operators?
....
| ($maybe ? $a : $b) = $x;

* http://faq.perl.org/perlfaq8.html :
| How do I tell the difference between errors from the shell and perl?
....
| $a = 1 + undef;

Bye
Achim
 
M

Michele Dondi

He did.

If you researched the answers to his 4 questions, you would have
learned how to localize foreach's loop control variable, along
with 3 other things that are important to understand if you
hope to do much programming in Perl.

I beg to differ. He wasn't asking about *how* to localize for's loop
control variable, but why local() is not valid *syntax* there: that it
would be pretty much useless being a possible answer, albeit not a
compelling one. I for one think it *may* be made valid, with the
obvious semantics associated to it. But that's just because it's in my
nature to strive for consistency, which indeed may in turn be
completely out of place in Perl (5)'s realms...


Michele
 
M

Michele Dondi

T> I don't want to use local. I like to use `my' here. But I just want to
T> know why `local' is not allowed here, so i put the example.

then why do you care about local? you rarely need it and it is not

Some people, like me, are often concerned about consistency in
programming language concepts. It's just not always a matter of
"getting things done".
T> The result is same if use `$aaa' instead of $a, even i know $a is
T> magical, but why bother to be boring of this?

boring?

I presume he may mean annoying. For some reason the Italian verb
"annoiare," which has the same source of "to annoy," does not
translate directly into the latter, but into "to bore." Todd may not
be a native English speaker, and have incurred in a similar problem.


Michele
 
T

Todd

Great thanks, Michele Dondi.
You make many things clear enough here.
Hopes i'm not distract now due to ... :(

-Todd
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top