Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Why this simple code doesn't work?

by JavaFan (Canon)
on Mar 02, 2012 at 17:29 UTC ( [id://957520]=note: print w/replies, xml ) Need Help??


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

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".

Replies are listed 'Best First'.
Re^3: Why this simple code doesn't work?
by CountZero (Bishop) on Mar 02, 2012 at 18:06 UTC
    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.

        It pleases me a lot that my funny remarks brought a smile upon your face! :)

        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

Log In?
Username:
Password:

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

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

    No recent polls found