Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

Update 2: Also, for completeness, there's Data::Alias and the (currently experimental) Assigning to References.


In reply to Re^4: Ternary Quizical behaviour? (updated) by haukex
in thread Ternary Quizical behaviour? by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-28 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?