foreach loops and lexically scoped loop variables

D

Derek Basch

Why does this:

foreach my($key) (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don't";
}

produce this:

Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i

I searched alot and I can't find a good answer except for my Perl
version being outdated and mine is not:

perl -v

This is perl, v5.8.3 built for x86_64-linux-thread-multii

Thanks,
Derek Basch
 
D

Dr.Ruud

Derek Basch schreef:
foreach my($key) (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don't";
}

produce this:

Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i

$ perl -MO=Deparse -e '
foreach my($key) (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don-t";
}
'
Missing $ on loop variable at -e line 2.


Remove the () around $key:


$ perl -MO=Deparse -e '
foreach my $key (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don-t";
}
'
foreach my $key (sort keys %reactivate_account_ref) {
print 'I should work...but I don-t';
}
-e syntax OK
 
T

Tad McClellan

Derek Basch said:
Why does this:

foreach my($key) (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don't";
}

produce this:

Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i


Because you are attempting to use invalid syntax.

foreach my $key (sort(keys(%reactivate_account_ref))) {
 
M

Matt Garrish

Derek Basch said:
Why does this:

foreach my($key) (sort(keys(%reactivate_account_ref))) {
print "I should work...but I don't";
}

produce this:

Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i

Because you can only assign to a single scalar not a list (or array or
hash), which is what you are doing by wrapping $key in parentheses.

foreach my $key (sort keys %reactive_account_ref) {

Matt
 
D

Derek Basch

Thanks everyone,

I guess I just find it confusing what to put in parentheses and what
not to in Perl. For instance I see examples all the time the have none
around 'keys' and other that do. There doesn't seem to be a consistency
to it.

Derek Basch
 
U

Uri Guttman

DB> I guess I just find it confusing what to put in parentheses and what
DB> not to in Perl. For instance I see examples all the time the have none
DB> around 'keys' and other that do. There doesn't seem to be a consistency
DB> to it.

perl is actually very consistant. you just don't understand the
consistancies. the conditional of an if/while is always in parens. keys
is a builtin function and can be called with/without parens just like
all the other builtins. perl is consistantly flexibile there. now you
still may need to use parens for precedence but that is another
consistancy you need to learn.

uri
 
M

Michele Dondi

perl is actually very consistant. you just don't understand the

While I agree with all the rest of this answer of yours, and I find
Perl to be much more consistent than is often regarded by people who
don't really know it, it is perhaps slightly exaggerated to call it
"very consistent". We all know two key ingredients in its design have
been magic and pragmatism, possibly at the expense of some
consistency...


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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top