Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Ternary Quizical behaviour? (updated)

by haukex (Bishop)
on Jul 10, 2020 at 21:18 UTC ( #11119164=note: print w/replies, xml ) Need Help??


in reply to Re^3: Ternary Quizical behaviour?
in thread Ternary Quizical behaviour?

I have literally never used the term alias to refer to a variable passed by reference. I don't think that's as common as you potentially think.

Then I think a clarification of terminology is in order. In Perl, there is a clear difference between reference and alias.

perlsub:

The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). ... Assigning to the whole array @_ removes that aliasing, and does not update any arguments.

perlsyn:

... the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

perlmod:

Assignment to a typeglob performs an aliasing operation, i.e., *dick = *richard; causes variables, subroutines, formats, and file and directory handles accessible via the identifier richard also to be accessible via the identifier dick.

map and grep:

Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables. [Similarly, grep returns aliases into the original list, much as a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a foreach, map or another grep) actually modifies the element in the original list. This is usually something to be avoided when writing clear code.]

None of these cases have to do with Perl's references (perlref).

Because in Perl we usually start a sub with my $x = shift; or my ($x,$y) = @_;, which discards the aliasing, people often mistakenly think that Perl is call-by-value, and that to do call-by-reference one has to pass an explicit reference into a sub, e.g. sub foo { my $x = shift; $$x = 5; } my $y; foo(\$y);. But that's not the case, Perl is in fact call-by-reference because the elements of @_ are aliases (not Perl's references!) to the original arguments, and we can modify the original arguments through these aliases. However, it's actually not a recommended practice in Perl, or at least fairly uncommon, and it's normally suggested to use one or more return values instead of modifying the arguments.

Update: Added "map and grep" item to the list and expanded last paragraph.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-09-21 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (127 votes). Check out past polls.

    Notices?