a declaring
am 21.04.2008 16:40:19 von peng.kyo
I'm not sure, but why this can work?
use strict;
use warnings;
use Data::Dumper;
my $y=0;
my @x =(1,2,3) if $y;
print Dumper \@x;
Since $y is false, it seems @x shouldn't be declared.
But why the last print can work?
--
J. Peng - QQMail Operation Team
eMail: peng.kyo@gmail.com AIM: JeffHua
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 21.04.2008 17:37:20 von Gunnar Hjalmarsson
J. Peng wrote:
> I'm not sure, but why this can work?
>
> use strict;
> use warnings;
> use Data::Dumper;
>
> my $y=0;
> my @x =(1,2,3) if $y;
> print Dumper \@x;
>
>
> Since $y is false, it seems @x shouldn't be declared.
Please read the last para in the "Statement Modifiers" section in
"perldoc perlrun".
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 21.04.2008 17:54:23 von Gunnar Hjalmarsson
Gunnar Hjalmarsson wrote:
> J. Peng wrote:
>> I'm not sure, but why this can work?
>>
>> use strict;
>> use warnings;
>> use Data::Dumper;
>>
>> my $y=0;
>> my @x =(1,2,3) if $y;
>> print Dumper \@x;
>>
>> Since $y is false, it seems @x shouldn't be declared.
>
> Please read the last para in the "Statement Modifiers" section in
> "perldoc perlrun".
I should add that variable declaration happens at compile time, while
assigning happens at run time.
C:\home>type test.pl
my $var = 123;
BEGIN { print "\$var is $var at compile time.\n" }
print "\$var is $var at run time.\n";
C:\home>test.pl
$var is at compile time.
$var is 123 at run time.
C:\home>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 21.04.2008 19:21:46 von chas.owens
On Mon, Apr 21, 2008 at 11:37 AM, Gunnar Hjalmarsson wrote:
snip
> Please read the last para in the "Statement Modifiers" section in "perldoc
> perlrun".
snip
I think you mean perlsyn, not perlrun:
http://perldoc.perl.org/perlsyn.html#Statement-Modifiers
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 21.04.2008 19:58:28 von chas.owens
On Mon, Apr 21, 2008 at 10:40 AM, J. Peng wrote:
> I'm not sure, but why this can work?
>
> use strict;
> use warnings;
> use Data::Dumper;
>
> my $y=0;
> my @x =(1,2,3) if $y;
> print Dumper \@x;
>
>
> Since $y is false, it seems @x shouldn't be declared.
> But why the last print can work?
snip
Because of a quirk in how the current and past versions of perl parsed
and handled the statement. It is a mis-feature according to Larry.
Some people used to use it to create state variables. The correct way
to create a state variable prior to 5.10 is with a closure:
{
my @x = (1, 2, 3);
sub function_that_uses_x {
...
}
}
In Perl 5.10 we got the state* function that works like the my
function but does not reinitialize each time through:
sub function_that_uses_x {
state @x = (1, 2, 3);
...
}
Of course, the old way is still handy if you need to share a state
value between functions:
{
my $shared = 0;
sub f1 {
...
}
sub f2 {
...
}
}
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 21.04.2008 22:12:37 von Gunnar Hjalmarsson
Chas. Owens wrote:
> On Mon, Apr 21, 2008 at 11:37 AM, Gunnar Hjalmarsson wrote:
> snip
>> Please read the last para in the "Statement Modifiers" section in "perldoc
>> perlrun".
> snip
>
> I think you mean perlsyn, not perlrun:
> http://perldoc.perl.org/perlsyn.html#Statement-Modifiers
Yes I did; thanks for the correction.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 22.04.2008 03:25:38 von peng.kyo
On Tue, Apr 22, 2008 at 1:58 AM, Chas. Owens wrote:
> Because of a quirk in how the current and past versions of perl parsed
> and handled the statement. It is a mis-feature according to Larry.
Chas, do you mean this is a bad style to declare and assign a variable
like below?
my $y=0;
my @x =(1,2,3) if $y;
print Dumper \@x;
Thanks!
--
J. Peng - QQMail Operation Team
eMail: peng.kyo@gmail.com AIM: JeffHua
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: a declaring
am 22.04.2008 03:27:17 von chas.owens
On Mon, Apr 21, 2008 at 9:25 PM, J. Peng wrote:
> On Tue, Apr 22, 2008 at 1:58 AM, Chas. Owens wrote:
>
> > Because of a quirk in how the current and past versions of perl parsed
> > and handled the statement. It is a mis-feature according to Larry.
>
> Chas, do you mean this is a bad style to declare and assign a variable
> like below?
>
>
> my $y=0;
> my @x =(1,2,3) if $y;
> print Dumper \@x;
snip
I mean it is an abuse of a bug. The proper way to achieve that
behavior is to either use the state function in 5.10 or closures in
earlier versions.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/