Can you tell me:
$_ = "#x"; my @a = qw/b c/; foo /#(\w+)/, @a; bar /#(\w+)/, @a; quz /#(\w+)/, @a; baz /#(\w+)/, @a;
Hint:
Okay, it's a trick question, I'm trying to make a point about Prototypes - I didn't give enough information for you to answer it, and if you were already thinking of an answer, sorry but you're probably wrong. So here's the missing information:
sub foo { print Dumper(\@_) } sub bar () { print Dumper(\@_) } sub quz ($) { print Dumper(\@_) } sub baz ($\@) { print Dumper(\@_) }
Solution:
1. Parsing according to perl -MO=Deparse,-p (with some edits for clarity):
foo( /#(\w+)/, @a ); ( bar() / # division; "#(\w+)/, @a;" is a comment quz( /#(\w+)/ ) ), @a; # void context &baz( scalar(/#(\w+)/), \@a );
2. Output (again slightly edited, and without warnings):
$VAR1 = ['x','b','c']; # sub foo $VAR1 = []; # sub bar () $VAR1 = [1]; # sub quz ($) $VAR1 = [1, ['b','c']]; # sub baz ($\@)
See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen. The code was inspired by merlyn's great node On Parsing Perl.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Fun with Prototypes
by LanX (Saint) on Jul 19, 2017 at 21:37 UTC | |
by haukex (Archbishop) on Jul 23, 2017 at 20:35 UTC | |
Re: Fun with Prototypes
by LanX (Saint) on Jul 19, 2017 at 21:45 UTC | |
by tobyink (Canon) on Jul 22, 2017 at 11:22 UTC | |
by LanX (Saint) on Jul 22, 2017 at 22:28 UTC | |
by LanX (Saint) on Jul 20, 2017 at 12:49 UTC | |
Re: Fun with Prototypes
by Laurent_R (Canon) on Jul 19, 2017 at 14:16 UTC |