Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://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":



  • 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 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?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found