Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Lexicals in if() scope gotcha!

by hossman (Prior)
on Mar 31, 2004 at 00:44 UTC ( #341161=note: print w/ replies, xml ) Need Help??


in reply to Lexicals in if() scope gotcha!

  1. This is not just an issue with "if" it also happens with "for" (but not foreach, or while)...
    #!/usr/bin/perl print "foreach...\n"; foreach (Foo->new) {} print "after foreach\n"; print "while...\n"; while (my $object = Foo->new && 0) {} print "after while\n"; print "for...\n"; for (my $object = Foo->new; 1 < 0; ) {} print "after for\n"; package Foo; sub new { my $self = bless {},shift; print "CREATED $self\n"; $self } sub DESTROY { print "DESTROYED $_[0]\n" } __END__ bester:~/tmp> monk.pl foreach... CREATED Foo=HASH(0x22494) DESTROYED Foo=HASH(0x22494) after foreach while... CREATED Foo=HASH(0x305f0) DESTROYED Foo=HASH(0x305f0) after while for... CREATED Foo=HASH(0x22494) after for DESTROYED Foo=HASH(0x22494)
  2. This is why you should allways head the immortal words of perltoot...
    Perl's notion of the right time to call a destructor is not well-defined currently, which is why your destructors should not rely on when they are called.


Comment on Re: Lexicals in if() scope gotcha!
Download Code
Re: Re: Lexicals in if() scope gotcha!
by QM (Vicar) on Mar 31, 2004 at 02:31 UTC
    So for and foreach are not just aliases for each other, as I've seen elsewhere.

    Update: So they are aliases, but there are still 2 entities, and the entity chosen depends on context.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      They are aliases. If you look, you see that for and foreach have been used differently. This should clarify that.

      #!/usr/local/bin/perl5.9.1 print "foreach1...\n"; foreach (Foo->new) {} print "after foreach\n"; + print "for...\n"; for (my $object = Foo->new; 1 < 0; ) {} print "after for\n"; + print "foreach2...\n"; foreach (my $object = Foo->new; 1 < 0; ) {} print "after foreach\n"; + package Foo; sub new { my $self = bless {},shift; print "CREATED $self\n"; $self } sub DESTROY { print "DESTROYED $_[0]\n" } __END__ foreach1... CREATED Foo=HASH(0x81249dc) DESTROYED Foo=HASH(0x81249dc) after foreach for... CREATED Foo=HASH(0x8124ac0) after for foreach2... CREATED Foo=HASH(0x81249dc) after foreach DESTROYED Foo=HASH(0x81249dc) DESTROYED Foo=HASH(0x8124ac0)

      Cheers,
      Ovid

      New address of my CGI Course.

      There's a for-loop, with three expressions (sometimes called "C-style for").

      There's a foreach-loop, with single scalar (sometimes implied with $_) and a list of values to be walked (sometimes called a "csh-style foreach").

      They're definitely two different kinds of loops. However the text for and foreach may be used to introduce either one. The actual loop-style is determined by what follows.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re: Re: Lexicals in if() scope gotcha!
by Mr. Muskrat (Abbot) on Mar 31, 2004 at 02:51 UTC

    I got slightly different results but I also changed the foreach a bit...

    #!/usr/bin/perl print "foreach...\n"; foreach (my $object = Foo->new) {} print "after foreach\n"; print "while...\n"; while (my $object = Foo->new && 0) {} print "after while\n"; print "for...\n"; for (my $object = Foo->new; 1 < 0; ) {} print "after for\n"; package Foo; sub new { my $self = bless {},shift; print "CREATED $self\n"; $self } sub DESTROY { print "DESTROYED $_[0]\n" } __END__ foreach... CREATED Foo=HASH(0x813361c) after foreach while... CREATED Foo=HASH(0x8133700) DESTROYED Foo=HASH(0x8133700) after while for... CREATED Foo=HASH(0x813cfcc) after for DESTROYED Foo=HASH(0x813cfcc) DESTROYED Foo=HASH(0x813361c)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://341161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-05 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (66 votes), past polls