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

Re: Why this simple code doesn't work?

by CountZero (Bishop)
on Mar 02, 2012 at 16:40 UTC ( [id://957511]=note: print w/replies, xml ) Need Help??


in reply to Why this simple code doesn't work?

Two more comments:
  1. Don't add prototypes to your subroutine declaration. Adding the empty round brackets after the subroutine name, means that this subroutine will not take any parameters! Obviously this is incorrect. Just drop the round brackets and forget you ever heard anything about Perl subroutine prototypes.
  2. Don't call your subroutines with a prepended "&". It is "old style" dating back to Perl 4! The effect of it now is that any prototypes are ignored. In the few cases they are actually usefully used, you surely do not want to do that and in all other cases it saves you one character less to type.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Why this simple code doesn't work?
by tobyink (Canon) on Mar 02, 2012 at 17:13 UTC

    Don't call your subroutines with a prepended "&".

    Actually in this case the "&" is meaningful. There is a Perl built-in called read, so the "&" serves as disambiguation, proving that you want to call the sub, and not the built-in.

    Of course, the wisdom of defining a function with the same name as a Perl built-in is debatable. Sometimes it makes sense (e.g. each in Set::Scalar is designed to be used as a method, so never looks ambiguous in code. Naming it same as the built-in serves as a hint as to its purpose.) but I'm not sure this is one of those times.

      In this case it is not meaningful to recycle a built-in keyword and even far less meaningful to add a totally wrong prototype (and then call the sub without regard to prototypes).

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re^2: Why this simple code doesn't work?
by uksza (Canon) on Mar 02, 2012 at 22:25 UTC
    1. Don't add prototypes to your subroutine declaration
    2. Don't call your subroutines with a prepended "&"
    I thing you are right. My Perl is little old fashioned (Learn Perl, 2-nd edition :) )
Re^2: Why this simple code doesn't work?
by JavaFan (Canon) on Mar 02, 2012 at 17:29 UTC
    Don't call your subroutines with a prepended "&". It is "old style" dating back to Perl 4! The effect of it now is that any prototypes are ignored. In the few cases they are actually usefully used, you surely do not want to do that and in all other cases it saves you one character less to type.
    Yeah, except in this case, things will break.

    Please, think before giving Pavlov reactions. Don't trigger a standard response to seeing a &sub. The use of &read is completely justified in this case, and your suggestion will break the construct. And not because of the prototype.

    Besides, there's no need to look down on constructs that predate Perl5. Regular expressions are "perl4" (actually, older than that). Many keywords are from the pre-perl5 era. And it's kind of silly to first say "don't use prototypes - in fact, try to forget they exist" and then "don't use &sub, because that bypasses prototypes".

      I totally agree that when following my advice the script will break. And for good reasons, since there is no good reason why you want to "re-cycle" a built-in keyword and then you can only make your bad choice of a sub-routine name work by using a construct that has an unwanted (and I am quite sure, unknown to the OP) side-effect.

      I think my "standard response" to using prototypes and the calling of subs with "&" is correct. Someone adds a wrong prototype to the sub-definition (I repeat, probably blissfully unaware of what he did) and then you need to set aside the prototype to make it work. Please, explain to me the merit of this kind of programming.

      In Perl 4 you had to prepend "&" to the sub-name to call it. In Perl 5 the meaning of "&" has totally changed.

      And I challenge you to proof that adding "&" to all your sub-calls is not silly. Some subs that work like built-ins need the prototyping to work.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        It's funny that you first totally ignored the, what you call, "re-use" of a keyword, and only when pointed out that the "saving of a keystroke" actually breaks you come with "yeah, but you shouldn't reuse a keyword".

        It's ever funnier that you first make a big deal about prototypes, and that you shouldn't use them, and then keep saying that the use of & circumvents them.

        And that while none of them contributed a single iota to solving the problem the OP had.

Log In?
Username:
Password:

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

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

    No recent polls found