barcodecontrol.com

sub bye{ print "Bye $name\n"; } (Output) 2 Hello Ellie. 5 Bye Ellie in .NET Use barcode pdf417 in .NET sub bye{ print "Bye $name\n"; } (Output) 2 Hello Ellie. 5 Bye Ellie

sub bye{ print "Bye $name\n"; } (Output) 2 Hello Ellie. 5 Bye Ellie generate, create none none on none projects Web app Scope of Variables Scope describes w here a variable is visible in your program. Perl variables are global in scope. They are visible throughout the entire program, even in subroutines.

If you declare a variable in a subroutine, it is visible to the entire program. If you change the value of an already existing variable from within a subroutine, it will be changed when you exit the subroutine. A local variable is private to the block, subroutine, or file where it is declared.

You must use either the local or my built-in functions to create local variables, since, by default, Perl variables are global in scope. (See "Call-by-Value with local and my" on page 332.) Example 11.

4.. (The Script) # Sc none for none ript: perlsub_sub2 # Variables used in subroutines are global by default 1 sub bye { print "Bye $name\n"; $name="Tom";} # Subroutine definition 2 $name="Ellie"; 3 print "Hello to you and yours!\n"; 4 &bye; 5 print "Out of the subroutine. Hello $name.\n"; # $name is now Tom 6 &bye; (Output) 3 Hello to you and yours! 1 Bye Ellie 5 Out of the subroutine.

Hello Tom. 1 Bye Tom. Explanation 1. The subroutine none none bye is defined. Within the subroutine block, the variable $name is assigned the value Tom.

$name is a global variable; in other words, it is visible throughout the program. [a]. We are assuming t hat the program was compiled into one package, main. For more on packages and scope, see 12, "Modularize It, Package It, and Send It to the Library!". 2. Program execution starts here. Global variable $name is assigned the value Ellie. 3. This line is here just to show you the flow of execution. 4. The subroutine none none &bye is called. The program jumps into the subroutine on line 1.

The value of $name is still Ellie. After the line Bye, Ellie is printed, and the variable $name is assigned a new value, Tom. The subroutine exits and the program resumes execution at line 5.

. 5. The value of the global variable $name was changed in the subroutine. 6. The subroutine is called again. The value of $name is Tom. We are assuming t hat the program was compiled into one package, main. For more on packages and scope, see 12, "Modularize It, Package It, and Send It to the Library!". We are assuming t none none hat the program was compiled into one package, main. For more on packages and scope, see 12, "Modularize It, Package It, and Send It to the Library!". 11.2. Passing Arguments If a user wants t none none o send values to a subroutine, he calls the function with a comma-separated list of arguments enclosed in parentheses. The feed_me function below takes 3 arguments when called: @fruit=qw(apples pears peaches plums); $veggie="corn"; &feed_me( @fruit, $veggie, "milk" ); # Declare variables. # Call subroutine with arguments The arguments can none for none be a combination of numbers, strings, references, variables, etc. They are received by the function in a special Perl array, called the @_ array, as a list of corresponding values called parameters. sub feed_me{ print join(",", @_),"\n"; } # Subroutine gets arguments in @_ array Output: apples, pears, peache, plums, corn, milk.

Call-by-Reference and the @_ Array Arguments, whethe none for none r scalar values or lists, are passed into the subroutine and stored in the @_ array. The @_ array is a local array whose values are implicit references to the actual parameters. If you modify the @_ array, you will modify the actual parameters.

However, if you shift or pop off elements of the @_ array, you merely lose the reference to the actual parameters. (See "Call-by-Value with local and my" on page 332.) When arrays or scalars are passed to a subroutine, the default in Perl is call-by-reference.

The @_ is a special local array used for referencing the names of the formal arguments. Its values can be changed, thus changing the value of the actual parameters. The elements of the @_ array are $_[0], $_[1], $_[2], and so on.

If a scalar variable is passed, its value is the first element of the @_ array, $_[0]. Perl doesn"t care if you don"t use all the parameters passed or if you have an insufficient number of parameters. If you shift or pop the @_ array, you merely lose your reference to the actual arguments.

If you want to modify the global copy rather than the local @_ array, then you can use either typeglobs (symbolic references) or pointers (hard references). Hard references are discussed briefly in section 11.3.

2 on page 349 and in more detail in 13, "Does This Job Require a Reference ".
Copyright © barcodecontrol.com . All rights reserved.