Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Assign in loop with and without declaration

by andreas1234567 (Vicar)
on May 22, 2008 at 07:57 UTC ( [id://687888]=perlquestion: print w/replies, xml ) Need Help??

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

What is it that prohibits assignment in loop to work when combined with declaration (my)? Why can't perl just DWIM (which is to set $i = 3 in both examples below)?
$ perl -l use strict; use warnings; my $i = undef; $i = $_ for (1 .. 3); ($i) ? print $i : warn "pre-defined - missing"; my $j = $_ for (1 ..3); ($j) ? print $j : warn "direct - missing"; __END__ 3 direct - missing at - line 9.
I just wonder since, after all, it does what I mean without warnings and my:
$ perl -l $j = $_ for (1 ..3); ($j) ? print $j : warn "no warnings - missing"; __END__ 3
For the record:
This is perl, v5.8.5 built for i386-linux-thread-multi
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Replies are listed 'Best First'.
Re: Assign in loop with and without declaration
by ikegami (Patriarch) on May 22, 2008 at 08:56 UTC

    Don't use a statement modifier on an my. It's a bug to do so. This is even explicitly mentioned in perlsyn.

    There is no answer to your "why?" since the docs specifically say that's Perl's behaviour in that situation can be anything. (That would include crashing.)

      Thank you for directing me to the documentation that says:
      NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
      My follow-up question is: Should there not at least be a warning issued when one attempts something as unpredictable as this?
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Assign in loop with and without declaration
by Anonymous Monk on May 22, 2008 at 08:19 UTC
    because of scope, same as
    my $i = undef; for(1..3){ $i = $_; } for(1..3){ my $j = $_; }
Re: Assign in loop with and without declaration
by mwah (Hermit) on May 22, 2008 at 08:26 UTC

    imho, it creates it's own scope level that includes the LHS of the assignment:

    ... { my $j; $j = $_ for (1 ..3) } ...

    Regards

    mwa

Re: Assign in loop with and without declaration
by runrig (Abbot) on May 22, 2008 at 16:19 UTC
    I just wonder since, after all, it does what I mean without warnings and my

    Because without strict and warnings, the variable is a package variable, not a lexical variable. The strict version of your last example would be:

    use strict; use warnings; use vars qw($j); # or "our $j;" $j = $_ for (1 ..3); ($j) ? print $j : warn "no warnings - missing";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found