Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Using s/// Inline

by bichonfrise74 (Vicar)
on Nov 16, 2009 at 19:39 UTC ( #807546=note: print w/replies, xml ) Need Help??


in reply to Re: Using s/// Inline
in thread Using s/// Inline

Hi Ikegami,

I was looking at the search result of prototypes in perlmonk and found that the general wisdom is not to use prototypes. But is there a good explanation as to why it should not be used?

I looked at Prototypes and it looks like prototypes are useful if you want to control the arguments coming into your subroutine (this might be wrong, but this is just how I understood).

Replies are listed 'Best First'.
Re^3: Using s/// Inline
by ikegami (Pope) on Nov 16, 2009 at 20:00 UTC

    Simply put, they change the parsing rules (e.g. the evaluation context of the arguments). Did you mean to change the parsing rules? No, you probably wanted some form of argument validation. Therefore, you shouldn't be using prototypes.

    Besides, you were placing them on methods where they are totally ignored (to the point that you were using the wrong one and you didn't notice).

      @ikegami, I believe you've mistaken bichonfrise74 and myself as being the same person, however it seems they've simply stumbled across this thread and wanted some clarification on how perl's prototypes work.

      I would like to note, though, that I made some hasty last-minute changes to that subroutine before posting in an attempt to trim it down to only the code that I felt was relevant to the issue at hand. Unfortunately I neglected to remove the $self initialization at the top of the subroutine, which seems to have caused some confusion.

      @bichonfrise74, I'm rather new to perl, myself, but I did some digging and it seems that the prototypes don't necessarily enforce constraints on subroutine arguments, rather they coerce the arguments that are passed into the variable types listed in the prototype. At least, that's the impression I got from http://www.perlfoundation.org/perl5/index.cgi?prototype

      Update: Seems I was a little hasty in my interpretation. "Coerced" isn't necessarily the correct word there, as it implies the variable's value would remain yet be offered in a new context. Instead, the arguments are interpreted by the subroutine as being in the context of the prototype, meaning if an @array is passed where a $scalar is expected, what would result is actually the length of that array, since $scalar = @array assigns the length of @array to @scalar.

      I hope that's clearer, my apologies for any misunderstandings.

        the arguments are interpreted by the subroutine as being in the context of the prototype,
        should read as
        the arguments are evaluated in the context indicated by the prototype,

        The subroutine has nothing to do with it. It's only called after the arguments have been evaluated.

        Hello,

        here is pretty exhaustive explanation on perl prototypes by Tom Christiansen:

        http://www.perl.com/language/misc/fmproto.html

        his conclusion is rather against them, although they might be useful at places as parameter coercion.

        -- Roman

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://807546]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-05-23 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?