Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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
Thnx
LuCa

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.

HTH.

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


Comment on Re^3: Generate the perl sequence 1, 11, 111, ....
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-18 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls