scoping u perl-u (main vs package)

1 view
Skip to first unread message

Dobrica Pavlinusic

unread,
Oct 30, 2009, 9:57:02 AM10/30/09
to zagr...@googlegroups.com
Prije nego što postam na perlmonks i ispadnem papak, da pitam vas:

zašto $out radi kada je u main scope-u, tj. zašto zapravo nije
$foobar::out?


#!/usr/bin/perl

use warnings;
use strict;
use Data::Dump qw(dump);

my $out; # this works

foobar::run( qw/ foo bar baz foo/ );

warn dump $out;

package foobar;

use warnings;
use strict;

#my $out; # XXX this doesn't work!

sub run {

my $code = eval q|
sub {
my $name = shift;
$out->{$name}++;
}
|;

$code->( $_ ) foreach @_;
}

--
Dobrica Pavlinusic 2share!2flame dpa...@rot13.org
Unix addict. Internet consultant. http://www.rot13.org/~dpavlin

Dobrica Pavlinusic

unread,
Oct 30, 2009, 10:14:53 AM10/30/09
to zagr...@googlegroups.com
On Fri, Oct 30, 2009 at 02:57:02PM +0100, Dobrica Pavlinusic wrote:
>
> Prije nego što postam na perlmonks i ispadnem papak, da pitam vas:
>
> zašto $out radi kada je u main scope-u, tj. zašto zapravo nije
> $foobar::out?

Da odgovorim na svoje pitanje: zato jer perl ima file scope...

Kada to podjelim na dva file-a radi jedino:

--- main.pl ---

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dump qw(dump);

use foobar;

foobar::run( qw/ foo bar baz foo/ );

warn dump $foobar::out;


--- foobar.pm ---

package foobar;

use warnings;
use strict;

my $out; # XXX this doesn't work!

sub run {

my $code = eval q|
sub {
my $name = shift;

$foobar::out->{$name}++;
}
|;

$code->( $_ ) foreach @_;
}

1;

Igor Rumiha

unread,
Oct 30, 2009, 10:42:55 AM10/30/09
to zagr...@googlegroups.com
2009/10/30 Dobrica Pavlinusic <dpa...@gmail.com>:

>
> On Fri, Oct 30, 2009 at 02:57:02PM +0100, Dobrica Pavlinusic wrote:
>>
>> Prije nego što postam na perlmonks i ispadnem papak, da pitam vas:
>>
>> zašto $out radi kada je u main scope-u, tj. zašto zapravo nije
>> $foobar::out?
>
> Da odgovorim na svoje pitanje: zato jer perl ima file scope...
>

Bezobrazni copy-paste sa http://perldoc.perl.org/functions/my.html:

A my declares the listed variables to be local (lexically) to the
enclosing block, file, or eval.

Viđao sam da ljudi rade ovakve stvari:

package main;

{

my $var; # Vidljivo samo unutar ovog bloka

... kod koji koristi $var...

}

--
IgorR

Boris Shomodjvarac

unread,
Oct 30, 2009, 10:51:11 AM10/30/09
to zagr...@googlegroups.com

Damian Conway je to stalno koristio za njegov Class::Std, inace stara
knjiga Object Oriented Perl :)

http://search.cpan.org/~dconway/Class-Std-v0.0.8/lib/Class/Std.pm#Automating_Inside-Out_Classes

Boris.

signature.asc

Dobrica Pavlinusic

unread,
Oct 30, 2009, 10:58:41 AM10/30/09
to zagr...@googlegroups.com
> .. kod koji koristi $var...
>
> }

Ma, da... taj implicitni { scope } je cool i čak ga i sam koristim
(povremeno) ali nekako uvijek zaboravim na taj file scope...

Igor Rumiha

unread,
Oct 30, 2009, 11:32:09 AM10/30/09
to zagr...@googlegroups.com
2009/10/30 Dobrica Pavlinusic <dpa...@gmail.com>:

>
> Ma, da... taj implicitni { scope } je cool i čak ga i sam koristim
> (povremeno) ali nekako uvijek zaboravim na taj file scope...
>


Mala škola Enterprise Perla, lekcija br. 114:

Jedan package, jedan file. I obratno.

;)

--
IgorR

Reply all
Reply to author
Forward
0 new messages