Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Let me answer your last question first.

The =~ binding operator binds the scalar on its left with the regexp (or transliteration) operator on the right, so that the regexp operator may examine and act upon the scalar on the left.

The = assignment operator assigns the value of the expression on the right hand side to the variable on the left.

The == equality operator evaluates the numeric equality of the items to its left and right. It has nothing to do with assignment.

Now for your first question:

$_[0] is a sort of alias to the first argument passed to the enclosing subroutine. In fact, perlsyn states, "The array @_ is a local array, but its elements are aliases for the actual scalar parameters." As the documentation also states, modifying $_[0] will attempt to modify the value held in the variable passed in the sub's arg list. For example:

my $value = 100; changeit( $value ); print "$value\n"; sub changeit { $_[0]++; } __OUTPUT__ 101

As you can see, incrementing $_[0] increments $value too (because they're basically the same thing).

Now as the docs say, if you attempt to modify the value of an unmodifiable scalar, you get an error. For example:

changeit( 100 ); sub changeit { $_[0]++; }

If you run this, you'll get an error. This is because $_[0] is aliased to a value, not a variable, in this case, and you can't change a hard-coded value. 100 cannot be 101 :)

As a matter of maintainable style, it's often discouraged to allow your subroutines to modify the values of their parameters. But as with most things Perlish, this rule of thumb only applies where convenient. After all, chomp modifies its parameter list. So does chop.

I hope this helps.


Dave


In reply to Re: What are multiple $_[0] =~ operations doing? by davido
in thread What are multiple $_[0] =~ operations doing? by Plotinus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2021-01-27 10:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?