in reply to Re^2: $_ functions vs argument-using functions
in thread $_ functions vs argument-using functions

I understand that the prototype sub f ($) {} makes sure f is called with exactly one argument.

Sorry, but that's not true:

#!/usr/bin/perl use strict; use warnings; sub f($) { print 'f called with ',0+@_,' arguments: ',join(', ',@_),"\n"; } sub g { print 'via g: '; &f; } f(42); &f(42); &f(); &f(42,99,333); g(42); g(); g(42,99,333);

Output:

f called with 1 arguments: 42 f called with 1 arguments: 42 f called with 0 arguments: f called with 3 arguments: 42, 99, 333 via g: f called with 1 arguments: 42 via g: f called with 0 arguments: via g: f called with 3 arguments: 42, 99, 333

See perlsub for what happens here, and why perl does not complain.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^4: $_ functions vs argument-using functions
by LanX (Saint) on Sep 10, 2013 at 10:30 UTC
    Was he really conpletely wrong, because an "except when called with &-sigil" was missing?

    Is it possible to talk about Perl and always mention all special cases???

    Cheers Rolf

    ( addicted to the Perl Programming Language)