On May 21, Paul Cotter said:

>> The problem is:  PROTOTYPES MUST BE SEEN BEFORE THE FUNCTION IS CALLED.
>
>I've seen this statement before and do not really understand it, having come
>from a 'true-compiler' background. It is the 'seen' that puzzles me.
>
>If I have a 'require' then I believe the prototype is inspected.
>If the subroutine occurs at the front of the 'main' code (ie before being
>used) then I believe the prototype is inspected
>
>However if I have code that goes:
>
>&dummysub( )
>&realsub('paulus')
>.
>.
>sub realsub($)
>{....}
>sub dummysub( )
>{...}

What prototypes do is modify the call to your function:

  sub foobar ($$) { ... }

  foobar(@this, @that);
  # becomes
  foobar(scalar(@this), scalar(@that));

  foobar(1,2,3,4);
  # becomes
  # a compile-time error -- 4 args sent, 2 expected

  foobar(%this, @that);
  # becomes
  foobar(scalar(%this), scalar(@that));


  sub arrays (\@\@) { ... }

  arrays(@a, @b);
  # becomes
  arrays(\@a, \@b);

  arrays(%c, $d);
  # becomes
  # a compile-time error -- expected '@' leading sigil

That's what they do.  Really.  And Perl can't modify your source code if
it hasn't seen the instruction to modify it.  These modifications happen
at compile time, when Perl is reading in your code for the first time.

Here, compare the opcodes for these two lines:

  japhy% perl -MO=Terse
  sub foo (\@);  foo(@a);
  bar(@a); sub bar (\@b);
  __END__

    UNOP (0x142d00) pp_entersub [2]
        UNOP (0x13bbb0) pp_null [141]
            OP (0x1287d0) pp_pushmark
        >>> UNOP (0x12e280) pp_refgen
                UNOP (0x13bb80) pp_null [141]
                    OP (0x1287e8) pp_pushmark
                    UNOP (0x12e240) pp_rv2av [1]
                        GVOP (0x12e220) pp_gv  GV (0xce8ac) *a
            UNOP (0xbede0) pp_null [17]
                GVOP (0xbed80) pp_gv  GV (0x12b7d4) *foo

    UNOP (0x142dc0) pp_entersub [4]
        UNOP (0x13bac0) pp_null [141]
            OP (0x128788) pp_pushmark
            UNOP (0x142de0) pp_rv2av [3]
                GVOP (0x142ca0) pp_gv  GV (0xce8ac) *a
            UNOP (0x142cc0) pp_null [17]
                GVOP (0x142ce0) pp_gv  GV (0x14c3d0) *bar

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **

Reply via email to