0
func1(func2);

As we know Perl needs to know the context to evaluate,but in the above case how does func2 know it's in scalar or list context?

asker
  • 2,159
  • 3
  • 22
  • 27

2 Answers2

5

If func1 does not have a prototype (or a @ prototype), it will be list context. If func1 has a prototype of $, then it will be scalar context.

Caveat: please do not use prototypes, they're evil.

Community
  • 1
  • 1
Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
2

Apparently. it's list.

$ perl
sub f1 { print "called f1\n" } 
sub f2 { print "called f2\n"; print wantarray ? "list": "scalar"; print "\n"; }
f1(f2);
^d
called f2
list
called f1

Why? That's another question entirely - I assume because function params are implicitly lists come what may.

Penfold
  • 2,548
  • 16
  • 16
  • You may want to complete the picture here by showing what happens when `f1` defines the prototype. – Zaid Jul 29 '11 at 08:05
  • 1
    Re "Why?", in what context do you think a list of arguments should be constructed? – ikegami Jul 29 '11 at 08:30