LINUX.ORG.RU

PERL и use strict


0

0


#!/usr/bin/perl -w

use strict;

my ($a,$b);
$a=&finding;


sub finding{
my $distfiles;
open(MAKE_CONF,"/etc/make.conf") || die "Can't open /etc/make.conf";
foreach (<MAKE_CONF>){
if(/PORTDIR=(.*)/){
my $port=$1;
print $port;
}
if(/DISTDIR=(.*)/){
my $dist=$1;
}
}
return $port;
}

./main.pl
Global symbol "$port" requires explicit package name at ./main.pl line 21.
Execution of ./main.pl aborted due to compilation errors.

чё за глюки у меня?

Ответ на: комментарий от golodranez

Вобщем, объявление my действует только в пределах скобок {}, будь то цикл, подпрограмма и т.п.

from perldoc perlsub:

Private Variables via my()

Synopsis:

my $foo; # declare $foo lexically local
my (@wid, %get); # declare list of variables local
my $foo = "flurp"; # declare $foo lexical, and init it
my @oof = @bar; # declare @oof lexical, and init it
my $x : Foo = $y; # similar, with an attribute applied

WARNING: The use of attribute lists on "my" declarations
is still evolving. The current semantics and interface
are subject to change. See attributes and Attribute::Han-
dlers.

The "my" operator declares the listed variables to be lex-
ically confined to the enclosing block, conditional
("if/unless/elsif/else"), loop ("for/fore-
ach/while/until/continue"), subroutine, "eval", or
"do/require/use"'d file. If more than one value is
listed, the list must be placed in parentheses. All
listed elements must be legal lvalues. Only alphanumeric
identifiers may be lexically scoped--magical built-ins
like $/ must currently be "local"ize with "local" instead.

Unlike dynamic variables created by the "local" operator,
lexical variables declared with "my" are totally hidden
from the outside world, including any called subroutines.
This is true if it's the same subroutine called from
itself or elsewhere--every call gets its own copy.

This doesn't mean that a "my" variable declared in a stat-
ically enclosing lexical scope would be invisible. Only
dynamic scopes are cut off. For example, the "bumpx()"
function below has access to the lexical $x variable
because both the "my" and the "sub" occurred at the same
scope, presumably file scope.

vilfred ☆☆
()
Ответ на: комментарий от golodranez

> так когда я use Strict отключаю, все работает

так оно для этого и служит. привносит в всеобщее языковое
безобразие Perl ограничения C. многие рекомендуют писать
только в strict режиме...

signal11
()
Ответ на: комментарий от signal11

да это понятно...яж говорю написал уже

golodranez ★★★★
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.