Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: $_ functions vs argument-using functions

by afoken (Prior)
on Sep 10, 2013 at 05:07 UTC ( #1053160=note: print w/ replies, xml ) Need Help??


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". ;-)


Comment on Re^3: $_ functions vs argument-using functions
Select or Download Code
Re^4: $_ functions vs argument-using functions
by LanX (Canon) 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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-07-05 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls