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

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

by Rohit Jain (Sexton)
on Sep 21, 2012 at 00:02 UTC ( #994792=note: print w/ replies, xml ) Need Help??


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

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..


Comment on Re^6: Use of "my" after Perl v5.14
Download Code
Re^7: Use of "my" after Perl v5.14
by Anonymous Monk on Sep 21, 2012 at 00:19 UTC

    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.
      Thanks :)

      Got it worked now.. Have to move chomp() outside that method call.. And then pass the modified numList as a Parameter..

Re^7: Use of "my" after Perl v5.14
by Anonymous Monk on Sep 21, 2012 at 02:41 UTC
    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
Re^7: Use of "my" after Perl v5.14
by AnomalousMonk (Monsignor) on Sep 21, 2012 at 05:00 UTC
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994792]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-08-30 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls