I just want to be sure I understand correctly :
In your article at perl.com you describes various ways and situations
when perl creates a topic and this is described as perl making the
following binding on my behalf:
$_ := $some_var ; *1*
and probably marking $_ with some additional properties (e.g. is
read-only ...) .
questions:
???
if I will write that explicitly myself will that have *the same*
effect ? in other words, is *1* _all_ that topic is about ? Or there
is some additional magic. particularly , will "when" work as before ,
What will happen if I will _override_ $_ explicitly inside
e.g. "given" construct or other topicalizers:
my $x,$z;
given $x->$y {
$_ := $z ;
when 2 { ... } #checks against $z ???
}
???
methods topicalize their invocant. Is $self aliased to $_ inside the
method in this ex. ?
method sub_ether ($self: $message) {
.transmit( .encode($message) );
}
will it be an error to write
method sub_ether ($self: $message, $x is topic) {...}
what happens if I write
method sub_ether ($self: $message) {
$_ := $message ;
}
or
method sub_ether ($self: $message) {
$_ = $message ;
}
is $_ always lexical variable. Or I can have $MyPackage::_ ?
and just on the related topic :
* can I alias $something to $_ ?
$something := $_
(it seems that I can , because $_ is just another variable )
also , is this valid ?
$b := $a ;
$c := $b ;
( now changing value of one variable will change other two ??? )
or e.g.
$a = 1 ;
$Z = 10 ;
$b := $a ;
$c := $b ;
print $c # prints 1
$a := $Z ;
print $c # prints 10
$a = 5;
print $Z # prints 5
am I wrong ?
also
@a := ( $a, $b)
$b := $c
@a[1] = 10 ;
print $c # prints 10
???
thanks
Arcadi
> In your article at perl.com you describes
> various ways and situations when perl
> creates a topic and this is described as
> perl making the following binding on my behalf:
>
> $_ := $some_var ; *1*
Well, $_ might not be bound to a named variable
but instead be just set to a value, or it might
be bound to an array cell or some other unnamed
container.
> is *1* _all_ that topic is about ?
Sorta. To quote an excellent summary:
"Topic is $_".
> my $x,$z;
> given $x->$y {
> $_ := $z ;
> when 2 { ... } #checks against $z ???
> }
Yes.
> methods topicalize their invocant. Is $self
> aliased to $_ inside the method in this ex. ?
>
> method sub_ether ($self: $message) {
> .transmit( .encode($message) );
> }
Yes.
> will it be an error to write
> method sub_ether ($self: $message, $x is topic) {...}
No.
> what happens if I write
> method sub_ether ($self: $message) {
> $_ := $message ;
> }
> or
>
> method sub_ether ($self: $message) {
> $_ = $message ;
> }
Both Ok. $_ is "it" and has the value $message;
in the former case $_ and $message are bound.
> is $_ always lexical variable.
Yes.
> Or I can have $MyPackage::_ ?
You can copy or alias any value.
> * can I alias $something to $_ ?
> $something := $_
Sure. Because...
> (it seems that I can , because $_ is just
> another variable )
> $b := $a ;
> $c := $b ;
>
> ( now changing value of one variable will
> change other two ??? )
Yes.
> or e.g.
>
> $a = 1 ;
> $Z = 10 ;
>
> $b := $a ;
> $c := $b ;
>
> print $c # prints 1
> $a := $Z ;
> print $c # prints 10
> $a = 5;
> print $Z # prints 5
Yes.
> also
>
> @a := ( $a, $b)
Er, I don't think (it makes sense that) you
can bind to a literal.
> $b := $c
> @a[1] = 10 ;
> print $c # prints 10
Lost you there, even ignoring the literal issue.
--
ralph
Thanks .
> > In your article at perl.com you describes
> > various ways and situations when perl
> > creates a topic and this is described as
> > perl making the following binding on my behalf:
> >
> > $_ := $some_var ; *1*
>
> Well, $_ might not be bound to a named variable
> but instead be just set to a value,
sure , I forgot , e.g.
given $x+1 {
when 2 { ... }
}
or it might
> be bound to an array cell or some other unnamed
> container.
>
>
>
> > is $_ always lexical variable.
>
> Yes.
>
>
> > Or I can have $MyPackage::_ ?
>
> You can copy or alias any value.
no, I mean is '$_' a valid name to live in package namespace ?
$main::_ = 1 ;
$::_ = 1;
our $_ ;
???
or variable with name '$_' is always implicitly "my" ??
> > also
> >
> > @a := ( $a, $b )
>
> Er, I don't think (it makes sense that) you
> can bind to a literal.
I think I meant this :
*@a := ( $a, $b )
although , to be true , I dont understand why the first version is
wrong. Do you mean that @a := expect 1 array variable and I give it
2 scalars ?
but if :
$ref = ( $a, $b ) ;
@a := $ref ;
this , probably is OK, but now changing $a or $b will not affect @a
and vice-versa.
so , anyway,
*@a := ( $a, $b )
> > $c := $b
> > @a[1] = 10 ;
> > print $c # prints 10
???
and also , one more question.
is this correct :
$x is constant = 1;
$y = 5;
$y := $x ;
$y = 1 # ERROR cannot change constant value
or in words, are (all) compile - time properties passed automatically
upon binding ?
thanks ,
arcadi .
A "real" topicalizer also sets a topicalizer scope that can be broken out of.
: > also
: >
: > @a := ( $a, $b)
:
: Er, I don't think (it makes sense that) you
: can bind to a literal.
Well, it makes as much sense as binding it in a function parameter. In this
case it'd be equivalent to
@a := [ $a, $b ]
That is, it's bound to a temporary.
: > $b := $c
: > @a[1] = 10 ;
: > print $c # prints 10
No, the [...] copies values upon composing the anonymous array.
Larry
so if I understand correctly ,
Every topicalizer defines a topicalizer scope in which there is
implicit declaration
my $_ ;
and then lexical $_ ( implicitely ) is bound to ( or assigned to )
whatever it should in this particular topicalizer. And from that on $_
is just another lexical variable .
Question(s) :
with no "use strict vars" any "just another variable" is taken by perl as
being global -- it is implicitly "our $just_another_var;" (???)
about any lexical veriable ( just_another_variable ) Perl have to be
explicitly informed as being such . is $_ just_another_variable in
that respect too ???
in other words , what happens if I just use $_ ( that is , without
previous declaration ) *outside any topicalizer* ?
* will it be implicitly "our $_" ( probably not , because it is
always lexical )
or * will it be implicitely "my $_" -- class/package lexical
or * will it be error to just use it without declaration
* with "use strict vars"
* with "no strict vars "
will it be an error to declare it as "our $_" ;
and to repeat the question from previous post ,
what will perl do when it see
$My_Package::_ = 1 ;
???
thanks , arcadi .
Yes.
: Question(s) :
:
: with no "use strict vars" any "just another variable" is taken by perl as
: being global -- it is implicitly "our $just_another_var;" (???)
: about any lexical veriable ( just_another_variable ) Perl have to be
: explicitly informed as being such . is $_ just_another_variable in
: that respect too ???
$_ is always a lexical. Every file scope has an implicit "my $_" at the top.
Every nested scope either sets up its own lexical $_ or aliases to an outer $_.
: in other words , what happens if I just use $_ ( that is , without
: previous declaration ) *outside any topicalizer* ?
There is no "outside any topicalizer" from that standpoint. However,
$_ does start off undefined in an implicit topicalizer, and we may
well disallow "break" to such an implicit topicalizer at file scope,
and maybe at subroutine scope.
: * will it be implicitly "our $_" ( probably not , because it is
: always lexical )
Correct, $_ is always lexical. But...
: or * will it be implicitely "my $_" -- class/package lexical
There's no such thing as a "class/package lexical". I think you
mean file-scoped lexical here.
: or * will it be error to just use it without declaration
: * with "use strict vars"
: * with "no strict vars "
:
: will it be an error to declare it as "our $_" ;
No, in this case, $_ is still considered a lexical, but it just happens
to be aliased to a variable in the current package.
: and to repeat the question from previous post ,
: what will perl do when it see
:
: $My_Package::_ = 1 ;
It'll set the $My_Package::_ variable to 1, I presume. Whether that
has any influence on the value of $_ depends on how $_ is currently
aliased. But the name $_ will always be interpreted according to the
lexical definitions set up by "my" and "our" (including the implicit
outer "my").
Larry
ooo, now I understand : *scope* is orthogonal concept to class/module
symbol-tables . scope is related only to ( current ) lexical
symbol-table. and the outmost scope is file scope . all other ( inner
) lexical scopes are enclosed by closure braces wheither it is a
definition of class , subroutine or loop .
>
> : will it be an error to declare it as "our $_" ;
>
> No, in this case, $_ is still considered a lexical, but it just happens
> to be aliased to a variable in the current package.
>
which variable ? it seems that "our $_" is something like that (???)
my $_ # implicit -- at the beginning of file ( or actually any other
# lexical scope
..
..
..
our $_ ; # translated to : our $Main::_ := $_ ;
.. # or $_ := $Main::_
..
..
???
( i have in mind that "our $thing " is something like this : "dont
worry , $thing is variable from current package )
but that would be strange , because I thaought that my/our manipulate
names in symbol-table , while aliasing is compleatly orthogonal to
that. or "our $_" is just special case with perl making additional
magic .
> aliased. But the name $_ will always be interpreted according to the
> lexical definitions set up by "my" and "our" (including the implicit
> outer "my").
>
arcadi
No, that's backwards.
: . # or $_ := $Main::_
More like that, except a new lexical name is introduced as with "my".
: ( i have in mind that "our $thing " is something like this : "dont
: worry , $thing is variable from current package )
Well, it has that effect, but it does so by pretending it's a lexical.
: but that would be strange , because I thaought that my/our manipulate
: names in symbol-table , while aliasing is compleatly orthogonal to
: that. or "our $_" is just special case with perl making additional
: magic .
No special magic. For any variable, saying
package P;
our $foo;
is very much like
my $foo ::= $P::foo
Larry