Chris [perl AT thesnyderhome DOT com] wrote:
>Mike Arms wrote:
>>You are making the incorrect assumption that the function
>>call intRand(45) will get evaluated inside of double quotes.
>>This is not the case.
>>
>>You can do it in two steps:
>>
>>  my $index = intRand(45);
>>  print " $class[$index]";
>>
>>But you might prefer something like this:
>>
>>  my $index = intRand( scalar @class );
>>  print " $class[$index]";
>>
>>This assumes that intRand( N ) returns an integer
>>in the range 0 .. N-1 .  The value "scalar @class" is the
>>number of elements of the @class array.
>
>I actually just tested it and the function call intRand(45) did get
>evaluated.  Perl 5.8.3 build 809
>
>-- Chris

Wow! I am almost dumbfounded. Almost. :-) From my past experences
with interpolation of double quoted strings, I could have sworn
that no functions would be interpolated. I was wrong as Chris
points out. After his message, I tested under Cygwin Perl 5.8.2
and Win32 ActivePerl 5.6.1 (build 631). Indeed the function is
getting evaluated:

  perl -e 'sub intRand { return 3; } my @a=qw( a b c d e f ); print qq(
>$a[intRand(45)]<\n);'

Output:

  >d<

Thanks, Chris. Love it when I learn something new.

--
Mike Arms


_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to