Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

my $variable with &&do{} doesn't compile

by Biker (Priest)
on May 26, 2003 at 08:56 UTC ( #260800=perlquestion: print w/replies, xml ) Need Help??

Biker has asked for the wisdom of the Perl Monks concerning the following question:

Guess I'm missing something basic here. (Again ;-)

How come that this compiles (and executes) fine:

my $epoch; $epoch=stat($self->{ABS_FILENAME})->mtime&&do{ $self->{CONFIG_EPOCH}=$epoch; };
while this fails to compile with the error message "Global symbol "$epoch" requires explicit package name at <path and module> line 95."
my $epoch=stat($self->{ABS_FILENAME})->mtime&&do{ $self->{CONFIG_EPOCH}=$epoch; };
I see it that $epoch gets unconditionally created with a conditional value (depending on the outcome of stat) and as such must exist. Looks like Perl is not as sure as I am. Why?

This is with Perl 5.6.1 from Active State.

Everything went worng, just as foreseen.

Replies are listed 'Best First'.
Re: my $variable with &&do{} doesn't compile
by Skeeve (Parson) on May 26, 2003 at 09:14 UTC
    perldoc perlsub says:
    The declared variable is not introduced (is not visible) until after the current statement. Thus, my $x = $x; can be used to initialize a new $x with the value of the old$x, [...]
    So perl tries to find another $epoch.


Re: my $variable with &&do{} doesn't compile
by Tanalis (Curate) on May 26, 2003 at 09:06 UTC
    The way I understand this is that in the first instance, you're creating the $epoch variable before it's used within the expression.

    In the second example, however, you create the variable at the same time as you attempt to use it. As the variable doesn't exist until after the statement is executed (which doesn't happen until the compiler hits the semi-colon), it's not possible to pass it into a function within that statement.

    I will point out that I'm not 100% sure about this, though - this is based on someone else's attempt at explaining a similar problem to me.

    Hope that helps ..

    -- Foxcub

      See below. You can be sure ;-)
Re: my $variable with &&do{} doesn't compile
by broquaint (Abbot) on May 26, 2003 at 12:39 UTC
    This is due to the fact that lexical variables are introduced at a statement level, not an expression level (which should be the case for perl6). Recently discussed at Problems with scoping.


Re: my $variable with &&do{} doesn't compile
by Aristotle (Chancellor) on May 29, 2003 at 22:08 UTC
    Both variants are rather unreadable. Why not like so?
    my $epoch = stat($self->{ABS_FILENAME})->mtime; $self->{CONFIG_EPOCH} = $epoch if $epoch;
    Repetitive, but it can't really be helped in this case.

    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://260800]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-01-27 21:29 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (71 votes). Check out past polls.