S
SomeDeveloper
Hello,
The following is an excerpt from the 'perlsub' man page (line
numbering mine):
1. | So
2. | my ($foo) = <STDIN>; # WRONG?
3. | my @FOO = <STDIN>;
4. |
5. | both supply a list context to the right-hand side,
while
6. |
7. | my $foo = <STDIN>;
8. |
9. | supplies a scalar context.
If on Line 2 a list context is being supplied to the rhs, the I/O
operator would read multiple lines. (I verified that it indeed is
reading multiple lines.) So far so good.
Now, once the the input (consisting of multiple lines) stands read,
why wouldn't (or rather: shouldn't) this text get assigned in its
entirety (complete with '\n' characters) to $foo? Just like the result
of join('', <STDIN>) ?
It appears to me that the list context or scalar context by itself is
not sufficient in determining the behavior. But rather "who or what is
providing this context" also matters.
As a user, I was expecting that since parentheses with the 'my'
modifier are used only to declare multiple lexicals in one-shot, Line
2 SHOULD essentially HAVE behaved like Line 7.
Are such concepts covered in a single online resource somewhere...
complete with rationale and examples?
Many thanks,
/SD
The following is an excerpt from the 'perlsub' man page (line
numbering mine):
1. | So
2. | my ($foo) = <STDIN>; # WRONG?
3. | my @FOO = <STDIN>;
4. |
5. | both supply a list context to the right-hand side,
while
6. |
7. | my $foo = <STDIN>;
8. |
9. | supplies a scalar context.
If on Line 2 a list context is being supplied to the rhs, the I/O
operator would read multiple lines. (I verified that it indeed is
reading multiple lines.) So far so good.
Now, once the the input (consisting of multiple lines) stands read,
why wouldn't (or rather: shouldn't) this text get assigned in its
entirety (complete with '\n' characters) to $foo? Just like the result
of join('', <STDIN>) ?
It appears to me that the list context or scalar context by itself is
not sufficient in determining the behavior. But rather "who or what is
providing this context" also matters.
As a user, I was expecting that since parentheses with the 'my'
modifier are used only to declare multiple lexicals in one-shot, Line
2 SHOULD essentially HAVE behaved like Line 7.
Are such concepts covered in a single online resource somewhere...
complete with rationale and examples?
Many thanks,
/SD