kevind0718 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks:

I have a very simple print question: The following code does what I need:
@array = 5..9; print " @array \n"; push @array, 10; print "@array \n" ; push @array, qw( fred barney wilma) ; print "@array \n" ;
produces the following, which is what I expected:
C:\dev\learningPerl>perl ex3a.pl 5 6 7 8 9 5 6 7 8 9 10 5 6 7 8 9 10 fred barney wilma C:\dev\learningPerl>
My question is: what is going on in this code?
@array = 5..9; print @array . "\n"; push @array, 10; print @array . "\n" ; push @array, qw( fred barney wilma) ; print @array . "\n" ;
produces the following, which seems strange to me. Certainly not what I expected.
C:\dev\learningPerl>perl ex3a.pl 5 6 9 C:\dev\learningPerl>

Just try to learn Perl and the above seeems strange to me.

Many Thanks for your kind assistance.

Best

KD

Replies are listed 'Best First'.
Re: simple printing question
by jwkrahn (Abbot) on Jan 31, 2012 at 23:33 UTC

    The concatenation operator (.) forces scalar context on its operands and an array in scalar context returns the number of elements in the array.

      crystal clear now

      many thanks

      KD
Re: simple printing question
by InfiniteSilence (Curate) on Jan 31, 2012 at 23:32 UTC

    Your usage is forcing @array into scalar context which returns the number of elements in the array.

    ~linux> perl -e '@array = 5..9; push @array,10; print scalar @array;'
    Same thing. There are some excellent books you should read in order to learn Perl. Trial and error will only get you so far and will contribute to both confusion and bad habits. Read Effective Perl Programming to eradicate these problems sooner than later.

    Celebrate Intellectual Diversity