Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: Generate the perl sequence 1, 11, 111, ....

by blazar (Canon)
on Oct 11, 2008 at 09:49 UTC ( #716578=note: print w/replies, xml ) Need Help??

in reply to Re^2: Generate the perl sequence 1, 11, 111, ....
in thread Generate the perl sequence 1, 11, 111, ....

What I don't understood is how it is possible that map can accept different syntax. For example, is it possible to write a perl subroutine that does the same thing, myPerlSub BLOCK LIST

UPDATE: thnx for the explanation!!

I personally believe that you shouldn't be too surprised. Quite a lot of builtin functions accept different kinds of arguments. Just see the first one that springs to mind, e.g. perldoc -f split:

split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split Splits the string EXPR into a list of strings and returns +that list. By default, empty leading fields are preserved, and +empty trailing ones are deleted. (If all fields are empty, they +are considered to be trailing.)

Ok, the case of map is somewhat more surprising: actually, not only is it possible, as others explained to you, to implement the syntax above with Prototypes (which indeed are there much to the objective of letting you write subs that behave like builtins) but it is currently impossible to implement the alternate syntax of map() i.e. namely that with an expression since that would be more akin to to a macro, but there's not provision for such a beast under Perl 5. Of course you can write a sub that accepts a generic argument as the first one and when you use it, then the argument can be an expression, but that would be evaluated and passed to the sub as a scalar, not as an expression to be evaluated by your sub itself.


If you can't understand the incipit, then please check the IPB Campaign.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716578]
Discipulus de gustibus non disputandum
[LanX]: tasty bus ? ; -p

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-04-28 11:28 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (521 votes). Check out past polls.