Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^8: map in void context

by pat_mc (Pilgrim)
on Dec 22, 2008 at 11:00 UTC ( [id://732037]=note: print w/replies, xml ) Need Help??


in reply to Re^7: map in void context
in thread map in void context

ikegami -

From a formal point of view there is, of course, a huge difference between calling a subroutine or using an operator - even if the difference is not discernable in Perl from their surface strings as you illustrate (other programming languages, of course, choose to handle this differently). I am not talking about how the input and output data get shifted around in memory here. The main difference from a formal perspective is that operators are part of the language inventory (its lexicon, to be precise) while subroutines are user-defined constructs based on language inventory and hence do not belong to the inventory themselves. As a result, operators may have additional properties - such as side-effects - associated with them by definition while subroutines do and cannot (as far as I am aware I cannot define side-effects for a subroutine in Perl).

To come up with a uniform argument on the production of side-effects by Perl commands (not statements!) one may wish to argue that the side-effect of a subroutine call is identical to its return value ... this behaviour would permit (the meaningful?) generalisation that every construct in Perl involving an operator (be it Perl-internal or user-defined) and its operands returns its return value plus one or more side-effect(s).

Replies are listed 'Best First'.
Re^9: map in void context
by chromatic (Archbishop) on Dec 23, 2008 at 05:36 UTC
    ... as far as I am aware I cannot define side-effects for a subroutine in Perl....

    It's easy:

    use 5.010; sub square_a_number { my $value = shift; $Some::Global::Variable += $value; say "Squaring the value '$value'"; return $value * $value; }

    There are two trivial side effects, as well as the non-obvious side-effect of setting SvNOK on $value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found