Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^4: Use of "my" after Perl v5.14

by Rohit Jain (Sexton)
on Sep 20, 2012 at 23:27 UTC ( #994782=note: print w/replies, xml ) Need Help??

in reply to Re^3: Use of "my" after Perl v5.14
in thread Use of "my" after Perl v5.14

Then I suspect, local works just like control variable in foreach loop, where I read that, if the control variable is already declared before the loop, then its value will be restored after the foreach loop ends.

E.g: -

my $packageVar = 5; foreach $packageVar (@someArr) { # Some code } print $packageVar; # Will print 5;

*EDIT: - Didn't saw this difference.. The "local" state of variable is visible accross multiple subroutines (in fact at all places in that file I guess) untill we get out of scope of that subroutine.. That is not the case with foreach loop..

Replies are listed 'Best First'.
Re^5: Use of "my" after Perl v5.14
by tobyink (Abbot) on Sep 20, 2012 at 23:38 UTC

    local can generally be implemented manually. The following two are pretty much exactly equivalent.

    ## Example 1 our $x = "Hello"; sub f { local $x = "Goodbye"; ...; } f(); ## Example 2 our $x = "Hello"; sub f { my $original = $x; $x = "Goodbye"; ...; $x = $original; # restore } f();

    local just means that you don't have to remember to manually restore the original value, which can be especially tricky if your sub has several different exit routes (e.g. different return statements in if blocks). Given that local is so much easier than the manual way, there's little reason to go down the manual route. However situations where you need to do this kind of thing are usually pretty rare, and often confined to Perl's built-in variables like $_ and $/.

    state is also not especially difficult to handle manually. The following is a reimplementation of my previous "counter" example:

    use v5.14; { my $count = 1; sub counter { say $count++; } } counter(); counter(); counter(); counter();

    Unlike local there is a valid reason to avoid state. It's a relatively new feature, so if you use it your code will not run on Perl versions prior to 5.10.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      use v5.14; { my $count = 1; sub counter { say $count++; } } counter(); counter(); counter(); counter();

      Unfortunately, this approach hides a subtle pitfall — well, subtle enough, at any rate, to catch me more than once. The problem is here illustrated:

      >perl -wMstrict -le "use v5.14; ;; counter(); counter(); ;; { my $count = 42; ;; sub counter { say $count++; } } ;; counter(); counter(); " 0 1 42 43

      There is a critical dependence upon order of execution. The  $count lexical variable is created at compile time, so the  counter() function has no problem accessing it; the code runs with warnings and strictures fully enabled. The variable is not initialized within its closure until some moment during run time; this moment may precede or follow any invocation of the function that quite happily accesses the variable. KaBoom!

      It's pretty easy to avoid this problem: perhaps the easiest way is to just use a state variable! But the first step toward avoidance is awareness.

      I have this code: -

      #!/perl/bin use v5.14; sub total { local $::@numList = @_; # This line is giving Compiler error.. my $sum = 0; # ForEach is not working in this case.. Even when I use my @nu +mList in declaration.. foreach (@numList) { say "list ", @numList; # Printing 3 (if 1, 2, 3) is input.. say "sum is : $sum"; $sum += $_; } $sum; } say "Enter a list of number to calculate sum :"; my $total = total(chomp(my @numList = <STDIN>)); say "Total : $total";
      I can't understand what can be the problem with the code.. I am trying to find the total of some integer from user input..
            local $::@numList = @_;   # This line is giving Compiler error..

        Aside from any consideration of package versus lexical variables, that statement is trying to local-ize a package scalar-array variable named numList (both  $ (scalar) and  @ (array) sigils are present), but Perl does not know what a scalar-array variable is, nor do I.

        A scalar in the  main package might be written as  $main::numList or with the shorthand  $::numList version, or an array might be written  @main::numList or  @::numList similarly.

        I can't understand what can be the problem with the code..

        see chomp , not exactly easy to see at a glance what the return value is , you'll have to read that prose or employ tricks :)

        $ perldoc -f chomp |ack return -A1 $INPUT_RECORD_SEPARATOR in the "English" module). It retur +ns the total number of characters removed from all its arguments. + It's -- number of characters removed is returned.
        use v5.14; sub total { my $sum = 0; foreach (@_) { $sum += $_; } say "list @_\nsum is $sum"; $sum; } say "Enter a list of number to calculate sum :"; my @numList = split /[,\s]+/, <STDIN>; chomp @numList; @numList = grep length, @numList; my $total = total(@numList); say "Total : $total"; __END__ Enter a list of number to calculate sum : 10 12 20 list 10 12 20 sum is 42 Total : 42

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994782]
[shmem]: harishnv: no way if you don't provide code and samples, as stated

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-02-20 08:27 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (268 votes). Check out past polls.