Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Fun with Prototypes

by haukex (Archbishop)
on Jul 19, 2017 at 11:54 UTC ( [id://1195441]=perlmeditation: print w/replies, xml ) Need Help??

Can you tell me:

  1. How does this get parsed? (e.g. try adding parentheses to show precedence)
  2. Given that the body of each of the four subs is { print Dumper(\@_) }, what is the output?
$_ = "#x"; my @a = qw/b c/; foo /#(\w+)/, @a; bar /#(\w+)/, @a; quz /#(\w+)/, @a; baz /#(\w+)/, @a;

Hint:

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
    I think you shouldn't hide the "hint", or at least rename it to "missing info".

    Hint implies that it's already solvable.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Thanks for the comments, and I did think about it - but I think I want to keep the "trick question" aspect of the node for now ;-)

      Also thank you for your other replies, that's certainly getting pretty tricky with the parsing!

Re: Fun with Prototypes
by LanX (Saint) on Jul 19, 2017 at 21:45 UTC
    More fun for you: ;)

    Some people want debug functions to disappear without footprint in production.

    Like with Smart::Comments but without source filter.

    Try to find a way where changing the prototype of a function &dmp will turn the call to a no op.

    Your bar /#(\w+)/, @a; is already quite close, but it's still involving a division. :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Isn't this sufficiently clear?

      use constant DEBUG => 0; use Data::Dumper; # ... warn Dumper($myvar) if DEBUG;

      Or PerX::Assert.

        > Isn't this sufficiently clear?

        > ...

        > warn Dumper($myvar) if DEBUG;

        clear but unnecessarily verbose.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

      an example avoiding a sub-call, unfortunately the division in void context is not optimized away.

      and the warning will create runtime overhead, too :/

      update
      But I think a little overhead is acceptable when debugging.

      another edge case are empty arrays causing a division by zero...

      update:

      fixed minor bug

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Fun with Prototypes
by Laurent_R (Canon) on Jul 19, 2017 at 14:16 UTC
    I don't quite understand what you're after, but you can check this module: B::Deparse

    Update: Sorry haukex, the spoilers did not work when I first tried to click on them. Now that I can see them, I understand your point.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1195441]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found