Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

from 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, but below code block output are not expected, why?

sub swap_1 { (@_) = reverse(@_); } sub swap_2 { my @item = reverse(@_); (@_) = ($item[0], $item[1]); print "##swap2_1: $item[0]\n"; print "##swap2_1: $item[1]\n"; print "-------------\n"; $_[0] = $item[0]; $_[1] = $item[1]; print "##swap2_2: $_[0]\n"; print "##swap2_2: $_[1]\n"; print "-------------\n"; } sub swap_3 { $_[0] = "AAA"; $_[1] = "BBB"; } my $one = "I am one"; my $two = "I am two"; swap_1($one,$two); print "one is '$one'\n"; print "two is '$two'\n"; print "-------------\n"; swap_2($one,$two); print "one is '$one'\n"; print "two is '$two'\n"; print "-------------\n"; swap_3($one,$two); print "one is '$one'\n"; print "two is '$two'\n"; print "-------------\n";

I expect swap_1, swap_2 can do the swap work, but obviously, I am wrong. but swap_3 is expected

below are the output

one is 'I am one' two is 'I am two' ------------- ##swap2_1: I am two ##swap2_1: I am one ------------- ##swap2_2: I am two ##swap2_2: I am one ------------- one is 'I am one' two is 'I am two' ------------- one is 'AAA' two is 'BBB' -------------

In reply to Accessing Arguments inside Subroutines via @_ by citi2015

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 exploiting the Monastery: (4)
As of 2024-04-25 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found