Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm confused to what the purpose of passing a variable by reference is since you claim that you don't *have" to in order to modify it
A good reason for passing references is so you can pass distinct groups of aggregate data types e.g
sub three_args { my($array, $scalar, $hash) = @_; print "three_args() got - $array, $scalar, $hash\n"; } my $scalar = 'a string'; my @array = qw(foo bar baz); my %hash = (key => 'value'); # with references three_args(\@array, $scalar, \%hash); # without references three_args(@array, $scalar, %hash); __output__ three_args() got - ARRAY(0x81089ac), a string, HASH(0x81089f4) three_args() got - foo, bar, baz
As you can see, if you don't pass an array by reference it flattens out (the same goes for hashes) because it's in list context (the default context when passing arguments to user-defined functions). However I can't think of any particularly compelling reason off the top of my head for passing simple scalars by reference. It's generally a much better idea to return the desired values from the function.
If the subroutine can modify this variable no matter how you pass it, what's the point of distinguishing between the two?
For the clarity and simplicity of code. You're more than welcome to access the variables that were passed to you through @_, but you're doing so at your own risk (and anyone else that has to maintain your code). To draw a similarity between with C++ - @_ is sort of like having all your arguments as references (i.e foo(int& x) where as passing by reference is more like passing pointers.
HTH

_________
broquaint


In reply to Re: Re: Re: Re: Re: Some suggestions on coding style - a chance to critique by broquaint
in thread Some suggestions on coding style - a chance to critique by emilford

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 having an uproarious good time at the Monastery: (4)
As of 2024-04-20 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found