in reply to Re^2: A curious case of of my()
in thread A curious case of of my()

I did of course test my assertions before presenting them. Separating the "my" from the if makes the problem go away. If the array were lexically declared it would not persist between calls to the subroutine. What do you think is happening - C static declaration leaked in there? Hardly! All of your points are in fact incorrect. In addition your style of argument is unacceptable to more educated people. Perhaps if you were more careful in your analysis to improve your accuracy, you wouldn't need to use expletives and straw men (like creating a line of argument, implying dishojnestly that another said it and then shooting it down) to sell your fallacies. I don't like dishonest people - go away.

One world, one people

Replies are listed 'Best First'.
Re^4: A curious case of of my() (What is GLOBAL?)
by LanX (Saint) on Apr 05, 2011 at 12:18 UTC
    As already said my has a compile-time effect of declaring a lexical variable and the run time effect of initialization.

    So this variable is a lexical which just isn't resetted in further runs, because of the if condition, (such that the old value of previous runs remains in the assigned memory-slot)

    One might argue what a global variable is, in Perl context it's normally used as synonym for package variables.

    Actually I'm trying to avoid the term "global" and prefer saying "package" or "lexical"! (There are hacks to globally manipulate lexicals)

    IMHO the only acceptable least misunderstandable use of the term "global" is with special vars like $_ which always belong to main:: without full qualification, no matter which package you're in.

    $_=666; package Foo; print $_; # -> 666

    HTH! :)

    Cheers Rolf

      ... special vars like $_ which always belong to main:: without full qualification

      ...unless $_ is lexicalized in the current scope :)

      $_=666; { my $_=777; print $_; # -> 777 print $main::_; # -> 666 }

      (just meaning to say that $_ isn't the best example — otherwise I agree of course)

        I'm not sure but IIRC lexical $_ is a rather new feature...

        But thanks!

        Cheers Rolf

      hmmm - but separating the my from the if turns on the lexical scope whereas leaving it as is keeps that turned off when tested.

      One world, one people

        Scope is another often misunderstood concept.

        For instance my, our and package follow the same scope rules (block or file scope).

        The scope decides which declaration is used at compile time too chose a "memory slot" for an (unqualified) variable. And the effect is hardcoded into the compiled opcodes!

        (i.e. either a hash-lookup in a "symbol table" of a package or "variable pad" of the surrounding lexical blocks)

        You're identifying scope with initialization, but the latter is a run time issue, which is disabled in an if-clause.

        Other languages like JS or Python follow more static mechanisms, leading to other quirks.

        Cheers Rolf

        Can you either shut up, or actually post some code that shows the variable isn't bounded by a lexical scope?

        You keep babbling on that the variable isn't lexical, but remain utterly silent when queried for some actual proof.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^4: A curious case of of my()
by JavaFan (Canon) on Apr 05, 2011 at 11:43 UTC
    I did of course test my assertions before presenting them.
    Care to post the test that showed @array is a global variable?
    If the array were lexical it would not persist between calls to the subroutine.
    That's not the definition of a lexical variable.
    All of your points are in fact incorrect
    Please show some code that proves my @array if EXPR; generates a global variable. Or how enabling strict and warnings would have pointed out the problem to the OP.
      It's the line "push @array ..." that auto-vivified the global array in the absence of strict declarations - the my did not take effect.

      One world, one people

        Bullshit.

        Please provide use with a code sample, that

        1. Shows that such an array is visible outside the lexical scope it's declared in.
        2. Shows that slapping use strict; on it actually makes a difference.
Re^4: A curious case of of my()
by ikegami (Patriarch) on Apr 05, 2011 at 15:33 UTC

    If the array were lexically declared it would not persist between calls to the subroutine.

    Really?

    use Devel::Peek qw( Dump ); sub f { my $x; Dump($x); $x = "abc"; } f(); f();
    SV = NULL(0x0) at 0x2cb90dc REFCNT = 1 FLAGS = (PADMY) SV = PV(0x1542f64) at 0x2cb90dc REFCNT = 1 FLAGS = (PADMY) PV = 0x154b194 "abc"\0 CUR = 3 LEN = 12

    You can tell it's the same variable in both calls to the function not only by the address being the same, but by the state left over between calls. Would you care to change you claim that this $x isn't a lexical?

    See Re: A curious case of of my()