#!/usr/bin/perl -w ####################################################### # class2_13_context # # showing how different list and scalar expressions # behave in list and scalar contexts... # # modified by Sharon Tuttle from "Learning Perl", # by Schartz and Phoenix # # last modified: 4-11-03 ####################################################### @values = (3..8); # the values 3 through 8 @myArr = @values; # also has the values 3 through 8 print "\@myArr contains: "; foreach ( @myArr ) { print "$_ "; } $myScalar = @values; # gets the NUMBER OF ELEMENTS in $values! print "\n\$myScalar contains: $myScalar\n"; @words = qw/ hello goodbye toodles /; @backwards = reverse @words; # the values toodles, goodbye, hello print "\n\@backwards contains: "; foreach ( @backwards ) { print "$_ "; } $backwards = reverse @words; # the reversed STRING "seldooteybdoogolleh" print "\n\$backwards contains: $backwards\n"; @myArr2 = 6 * 7; # gets the 1-element list (42) print "\n\@myArr2 contains: "; foreach ( @myArr2 ) { print "$_ "; } @myArr3 = undef; # OOPS! the 1-element array (undef) print "\n\@myArr3 contains: "; foreach ( @myArr3 ) { print "<$_ >"; } @myArr4 = (); # the PROPER way to initialize an EMPTY array print "\n\@myArr4 contains: "; foreach ( @myArr4 ) { print "<$_ >"; } # to "force" scalar context: print "\nThere are ", scalar @myArr, " elements in \@myArr\n"; print "type anything on as many lines as you like ---\n"; print " (type control-d to quit!)\n"; @lines = ; # reads MULTIPLE lines, until end-of-file # encountered, control-D typed, etc.; print "\n\@lines contains:\n "; foreach ( @lines ) { print $_; } # end of class2_13_context