Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Humm, don't forget that Perl uses a special variable @_ for the arguments. What you can't forget is that this array @_ is global, but it's elements will change for each call. So, the performance of @_ will be similar to a global variable, but will work as a stack, so, will work for multiple calls of the same sub and recursivelly.

This code will show that:

sub test1 { ++$_[0] ; print "T1 [@_]\n" ; test2($_[0]) ; } sub test2 { ++$_[0] ; print "T2 [@_]\n" ; test3($_[0]) ; } sub test3 { ++$_[0] ; print "T3 [@_]\n" ; print "end\n" ; } my $n = 10 ; test1($n) ; print "N: $n\n" ;
output:
T1 [11] T2 [12] T3 [13] end N: 13
And forget the use of a global variable for any type of code and start learning OO. Take a look at perlobj, perltoot, perlboot and perltooc.

For Object Orientation (OO) I use Class::HPLOO.

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Re: Global variable vs passing variable from sub to sub by gmpassos
in thread Global variable vs passing variable from sub to sub by kiat

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 chilling in the Monastery: (9)
As of 2024-03-28 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found