http://www.perlmonks.org?node_id=954977


in reply to new to perl, syntax question

Damien have a look at Arrays: A Tutorial/Reference. The relevant section is "Get count of elements" but it is well worth reading through.
Often you can find your answer by adding a print statement, taking ikegami's example:
print "@name"; # prints x y z print @name; # prints 3

Replies are listed 'Best First'.
Re^2: new to perl, syntax question
by ikegami (Patriarch) on Feb 20, 2012 at 05:05 UTC
    print evaluates its operands in list context, so print @name; does not print 3. (print scalar(@name); and print 0+@name; do, though)
      Aagh - I did try it before I replied but like this:
      print @name . "\n";
      then I took the "\n" out to make it clearer (but didn't run it!). I take it the concatenation makes print evaluate in scalar context. So should be:
      print "@name"; # prints x y z print @name; # prints xyz print @name . "\n"; # prints 3
      (lidden: I was still fixing this when you replied)

      Update - then I found this:
      If you believe in Lists in Scalar Context, Clap your Hands
      I'll be back in a few weeks.
        Concatenation puts its arguments in scalar context, that is the array is in scalar context.
Re^2: new to perl, syntax question
by Marshall (Canon) on Feb 20, 2012 at 10:53 UTC
    Update: Again just genuinely trying to be helpful with a suggestion on how to print arbitrary results - for beginners, this is WAY COOL. Didn't mean to offend anybody.

    I think better is to use the core module:

    use Data::Dumper; .... print Dumper \@name;
    Another module that you have to install before being able to use it:
    use Data::Dump qw(pp); ..... print pp \@name;
    Basically, both of these modules can print/dump an arbitrary Perl data structure.
    The format of the output is a bit different, but they both can do it.
    This is actually pretty much magic.
    It is of huge help when debugging.
    And it is of very huge help once you get into multidimensional data structures.