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.