Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by Biker (Priest)
on May 26, 2003 at 08:56 UTC ( [id://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.

    HTH

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
    #include www.liquidfusion.org.uk

      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.
    HTH

    _________
    broquaint

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found