in reply to Re: Re: string context and list operators (was Re: Array in scalar context.)
in thread Array in scalar context.

7. When you said in your first reply that, "join function is a list operator, like print" are you refering to the syntactial behavior of print (because it can take an array as one of it's arguments) instead of the symantics of it ? Similarly when you say "print function flattens the list and processes each of the elements in string context, are you refering to it's symantics ?
No. join is a function, not an operator.
join, like all perl functions, takes lists as arguments (and conveniently provides them in the magical @_ array).
join is context independent, observe
use strict; use warnings; my @stuff = 1 .. 10; print( join(' a ',@stuff), $/ ); print( scalar join(' a ',@stuff), $/ ); __END__ 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10
  • Comment on Re: Re: Re: string context and list operators (was Re: Array in scalar context.)
  • Download Code

Replies are listed 'Best First'.
Re: string context and list operators (was Re: Array in scalar context.)
by jonadab (Parson) on Oct 13, 2003 at 14:05 UTC
    print( join(' a ',@stuff), $/ ); print( scalar join(' a ',@stuff), $/ );

    Yes, but...

    print scalar join ' a ', @stuff, $/;

    In this last example, join will take the $/ as just one more argument, with the result that you'll get one last " a " after the 10, before the newline. The reason it does this is because of join's behavior as a list operator. Just about any function in Perl will behave this way, if it is called this way. print is usually called this way, but just about anything can be. Like I said, Perl blurs the distinction between functions and operators. Context, as in most of Perl, is everything. One is almost tempted to say that Perl is a context-oriented language and that this could be viewed as an entirely separate paradigm, an alternative to imperative or object-oriented or functional programming. Almost.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Re: Re: Re: string context and list operators (was Re: Array in scalar context.)
by Anonymous Monk on Oct 13, 2003 at 11:37 UTC
    join, like all perl functions, takes lists as arguments (and conveniently provides them in the magical @_ array).
    Another good example is substr. It can be used in lvalue context ( special context for subroutines, means you can assign to their result)
    use strict; use warnings; my $a = 'abcdefg'; print $a,$/; print substr($a,0,3),$/; # rvalue (list) print $a,$/; print substr($a,0,3)='bob',$/; # lvalue print $a,$/; print scalar( substr($a,0,3,'ABC')),$/; # rvalue (scalar) print $a,$/; __END__ abcdefg abc abcdefg bob bobdefg bob ABCdefg
    As you can see substr has special behaviour in lvalue context. RValue means the result is treated as an ordinary value.