Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: How's your Perl?

by davido (Archbishop)
on Oct 27, 2003 at 04:04 UTC ( #302325=note: print w/ replies, xml ) Need Help??


in reply to Re: How's your Perl?
in thread How's your Perl?

Though I'm almost certain that's what the OP had in mind, it relies on a bug rather than a documented feaure. For that reason I happen to like this solution to question #5 of the OP's quiz:

sub{my$x=\$x};

Updated version that creates only one $x:

sub { my $x; $x = \$x; }

The anonymous sub is created and immediately falls out of existance because it's not passed to a scalar variable. And yet $x never disappears because its reference count is always going to be 1; it refers to itself.

It probably doesn't qualify as a static, and is pretty much useless, but it meets the definition of static that the OP gave.


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein


Comment on Re: Re: How's your Perl?
Select or Download Code
Re: Re: Re: How's your Perl?
by Anonymous Monk on Oct 27, 2003 at 04:27 UTC
    I do not think your code does what you think it does. You have two different $x variables shown in your anonymous subroutine, not a single one referencing itself.
Re: Re: Re: How's your Perl?
by Anonymous Monk on Oct 27, 2003 at 05:38 UTC
    A static variable in a subroutine context would be created only once and hold its value between invocations. You've created a circular reference in an uninvokable routine. Even if it weren't optimized away, of which I am uncertain, it bears no resemblance to a static variable.
      You are correct, given the conventional definition of a static variable. But the Original Poster provided us with a test of expert Perl prowess, and went so far as to give us his expert definition of static:

      "static variable" means a variable with infinite lifetime...

      My solution meets his spec. In a rediculous way, of course... but then again it's a rediculous quiz.


      Dave


      "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
        But the Original Poster provided us with a test of expert Perl prowess, and went so far as to give us his expert definition of static

        ok ok I messed up; because I've had questions about what a "static variable" is (not all perl programmers are C programmers) so I added a quick clarification and got it wrong.. nobody's perfect

        no need to get sarcastic here

        My solution meets his spec. In a rediculous way, of course... but then again it's a rediculous quiz.
        true on both points   :-)
Re[3]: How's your Perl?
by xmath (Hermit) on Oct 27, 2003 at 06:36 UTC
    oops, you're right, my definition of "static" in the clarification was flawed :-)

    I do mean function-scoped static like in C/C++ ofcourse

    btw tachyon, your #1 solution isn't:
    perl -le 'my @x = ( \$_, \$_ ); $x[0]=42; print $x[1]'

      Read your rules. You don't specify HOW the value of $x[0] is changed :-) vis Create an array @x such that changing $x[0] also sets $x[1] to the same value While assignment is one way to change a value....

      @x=(\$_,\$_); $_=10; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; $_=20; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; __DATA__ $x[0] = 10 $x[1] = 10 $x[0] = 20 $x[1] = 20

      cheers $[

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Create an array @x such that changing $x[0] ...

        @x=(\$_,\$_); my $initial_value = $x[0]; $_=10; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; die "But \$x[0] did not change.\n" if $x[0] eq $initial_value; $_=20; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; die "But \$x[0] did not change.\n" if $x[0] eq $initial_value;

        You're really printing the wrong thing.

        print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; ^^ ^ LHS is $foo, RHS is $$foo. The output is very misleading. print "\${\$x[$_]} = ${$x[$_]}\n" for 0..$#x; LHS is $$foo and RHS is $$foo. But we're interested in $foo. print "\$x[$_] = $x[$_]\n" for 0..$#x; LHS is $foo and RHS is $foo. Output is correct and shows that the need +ed change did not happen.

        You're changing the variable that is refered to by the references in $x[0] and $x[1], but you're not actually changing the value in the container that is named $x[0].

        Clearer would be: "so that assigning to $x[0] also changes ...", but as said, we're against sanity.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-08-21 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (139 votes), past polls