Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Ternary Quizical behaviour?

by perlfan (Priest)
on Jul 10, 2020 at 20:41 UTC ( #11119161=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.

Update- thank you for the replies. TIL! This is why I come here.

Replies are listed 'Best First'.
Re^4: Ternary Quizical behaviour? (updated)
by haukex (Bishop) on Jul 10, 2020 at 21:18 UTC
    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.

Re^4: Ternary Quizical behaviour? (Ref vs Alias)
by LanX (Cardinal) on Jul 10, 2020 at 21:51 UTC
    > I don't think that's as common as you potentially think.

    sometimes an example is clearer than many words:

    the first arg to tst() is a scalar-ref, the second the scalar itself

    DB<107> sub tst { print "ref of alias match: ".$_[0] if $_[0] == \ $ +_[1] ; $_[1] = 42 } DB<108> my $x =666; tst( \$x,$x); print "\n now x = $x" ref of alias match: SCALAR(0x339fb20) now x = 42

    as you can see these are quite different concepts.

    References are a datatype in Perl pointing to another thing.

    Aliases are just the same thing by another symbolic name (sic).

    And in fact, to prove it, that alias has even the identical reference!

    compare also:

    > never used the term alias to refer to a variable passed by reference.

    well never too late! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2020-09-21 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (126 votes). Check out past polls.

    Notices?