Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

AFAIK there is now no type of subroutine that MUST be called with a leading ampersand or without comma's seperating the arguments
hmmm ... I could have emphasized more the 'definition' I was considering for MUST and MUST NOT. In other words, think of it like a kind of puzzle, can I write code in such a way that violating the rule will cause an error or a warning?. Then, if you find an answer to the puzzle, consider whether the code is likely to happen in the 'real world' ... if 'yes', then you have a candidate for MUST and MUST NOT. I have found this approach to be useful because it essentially asks "how many different ways can I (legally) say the same thing?"

For example, here is code that illustrates the ampersand 'rules'.

### init use strict; use warnings; ### ampersand variants fooFunc (1,2); ### ok, ampersand optional &fooFunc(1,2); ### ok, apersand optional #foneFunc(1,2); ### NOT ok, called too early &foneFunc(1,2); ### ok, ampersand required #fwoFunc(1,2); ### NOT ok, too early &fwoFunc(1,2); ### ok, ampersand required print "\n--------------------------\n"; sub fooFunc { if($_[0]){print "fooFunc arg0: ". $_[0] ."\n"} if($_[1]){print "fooFunc arg1: ". $_[1] ."\n"} print "-------------\n"; } sub foneFunc() { if($_[0]){print "foneFunc arg0: ". $_[0] ."\n"} if($_[1]){print "foneFunc arg1: ". $_[1] ."\n"} print "-------------\n"; } sub fwoFunc($$){ if($_[0]){print "fwoFunc arg0: ". $_[0] ."\n"} if($_[1]){print "fwoFunc arg1: ". $_[1] ."\n"} print "-------------\n"; }

As far as a subroutine call where separating commas MUST NOT be included, consider a user-defined subroutine that works like map CODEBLOCK ARRAY putting a comma between CODEBLOCK and ARRAY causes error.

Note that we don't run into problems until we start introducing prototypes, which generally people say to "stay away from", which is fine, but with caveats...

  • For someone used to programming in JavaScript (for example) it's not entirely obvious that fooFunction() is anything more than just a bare declaration of a subroutine name (as opposed to a 'prototype' embodied in the empty parenth).
  • You can only 'stay away' from something for so long, and then finally you want to become one of those mythical people who 'really really really know what they are doing'
  • It's interesting to know whether the 'stay away' items are either 1) legacy stuff that is going to be deprecated, or 2) advanced stuff that will remain for those who wish to delve deeper.
  • It seems like some 'stay away' items that are advanced, overlap with 'stay away' items that are to be deprecated.


In reply to Re: Re: Subroutine Bewilderment by dimar
in thread Subroutine Bewilderment by dimar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found