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

Re: How's your Perl?

by tilly (Archbishop)
on Oct 27, 2003 at 16:26 UTC ( [id://302421]=note: print w/replies, xml ) Need Help??


in reply to How's your Perl?

My answers without looking at existing ones...
  1. 1: I've seen this in chatter mentioned by tye, but the trick slips my memory at the moment. :-(
  2. 2: {foo;redo} # See RE: My favorite looping mechanism in Perl is:
  3. 3: This is actually two questions
    • My first thought was pseudohashes but pseudohashes are deprecated and IIRC removed in 5.8.0. Then it was $foo = \*bar; $$foo->[1] = $$foo->{1} = "hello" Hrm, Pseudohashes are allowed? OK then, $x = [{1=>1}, "hi"]; Your test will pass, albeit with an ugly scalar for each, but they are not accidentally the same, they are both references to the same string.
    • Trivial with overload, but I am not sure if that is breaking your rules. OK, sub bar {$bar[$_[0]]} $foo = \*bar; $bar[1]="hi";, that isn't a different idea than what I originally did for 3a, but it works. UPDATE 3 I don't know how to do this after all.
  4. 4: dump # So sorry, did you want solutions that aren't supposed to core dump? :-P
  5. 5: my $static if 0; # See Tie::Static for my opinion on this...
  6. 6: Is it cheating for me to use a module but not write one?
    # Reformat to one line if you want -e use overload qq("")=>sub{"same"}; $x = bless \$foo; $y = bless \$bar; $$x= "x"; $$y= "x";
    UPDATE You do this like so: $x = do {local *foo}; $y = *foo; $foo = "hello";
  7. 7: UPDATE 2 $foo = *STDOUT{IO};
  8. 8: *x = \$|
  9. 9: This is actually two questions UPDATE Um, in the questions shouldn't q be qq everywhere? If you don't let $$foo interpolate, it shouldn't matter what is in it...
    • UPDATE 3 $foo = \$!; $!=1; UPDATE 5 Oops, this is wrong. I should test these answers a little, ya think?
    • 9b
  10. 10
  11. 11: UPDATE 4 Put sub CORE'GLOBAL'glob{""} at the END of your code. Location matters, this code has to be seen by the Perl interpreter after Perl has seen some evidence that it needs to load File::Glob (which will overwrite us).
Bonus
I, um, would have gotten more if I didn't have to go to work. Yeah, that's the ticket...

UPDATE Minor fixes. BTW I should add that while I am not terribly bad at this kind of trivia, I think that focussing on it is a horrible mistake.

UPDATE 2 Answered 7.

UPDATE 3 Took time out for 9a. I had been thinking about 10, but because nobody else bothers to hide their answers, I saw mtye's version already. 3b has me puzzled again (see Roy Johnson's note).

UPDATE 4 Added a solution to problem 11.

UPDATE 5 Admit my mistake in problem 9a. I think that I'll admit to not knowing stupid Perl trivia that well these days. Particularly since nobody else seems to be trying these too hard. Also note that my solution to problem 11 can't be placed in front with glob-assign without adding a semi-colon, which puts you over the character limit.

Replies are listed 'Best First'.
Re: Re: How's your Perl?
by xmath (Hermit) on Oct 27, 2003 at 17:37 UTC
    3. who cares about deprecated, they still work ;-)

    3/6. overload is a module which isn't allowed (though there are ways to circumvent that), but more importantly bless is explicitly forbidden by the rules

    the others look good :-)

    UPDATE Re 9: it really is q, not qq. (notice the eval? :-)

    UPDATE 2 As Roy Johnson pointed out, your 3b currently doesn't work (it's only a small one-char mistake though)

    UPDATE 3 Your 9a doesn't work, they print differently but compare numerically equal. make sure to test your answers with perl -e '......; eval(q[$$foo]) != eval(q[0+$$foo]) or die' rather than using print, to avoid traps like these :-)

    UPDATE 4 ex 11 works.. note that you can place it in front by using glob-assign

    UPDATE 5 strictly speaking the semi isn't needed to make the condition true (which is the exercise), but even if you do include it it's still possible by dropping the "" from the sub

      If by a fix you mean sub bar {$bar[$_[0]]} $foo = *bar; $bar[1]="hi";, that does not work to the given spec.

      Test it with print "yes" if \$foo->(1) eq \$foo->[1] and you see that it fails for me on 5.8.1. Get rid of the backslashes and it works, but the problem is that Perl's functions are pass by reference, return by value. Therefore the return of bar(1) is a new string with the same value as $bar[1], but you get something different by taking a reference to it.

        ah yes, you're right.. it needs more than a one-char fix :-)

        the overall idea is still right though (as in, the correct solution is very similar to this), but I overlooked some details of your implementation

Re: Re: How's your Perl?
by Roy Johnson (Monsignor) on Oct 27, 2003 at 22:17 UTC
    On your 3b, I get
    Not an ARRAY reference at testit line 7.
    upon trying to
    printf "Value: %s\n", \$foo->[1];
Re: Re: How's your Perl?
by zakzebrowski (Curate) on Oct 27, 2003 at 21:52 UTC
    For #4, perl -edump also works... :)


    ----
    Zak
    undef$/;$mmm="J\nutsu\nutss\nuts\nutst\nuts A\nutsn\nutso\nutst\nutsh\ +nutse\nutsr\nuts P\nutse\nutsr\nutsl\nuts H\nutsa\nutsc\nutsk\nutse\n +utsr\nuts";open($DOH,"<",\$mmm);$_=$forbbiden=<$DOH>;s/\nuts//g;print +;
Re: Re: How's your Perl?
by xmath (Hermit) on Oct 28, 2003 at 00:48 UTC
    I had been thinking about 10, but because nobody else bothers to hide their answers, I saw mtye's version already

    Note that his version doesn't work under strict, while a strict solution is possible. (the 'official' solution is very different from mtye's), so the exercise isn't spoiled just yet ;-)

      How about an answer for #10 at 24 characters that passes strict?
      use strict; #23456789_123456789_1234 BEGIN{$'y=*x::=*y::=*::} print "Yes\n" if $x::y::x::y::y::x::y::x::x::y::y::y::y::x::y::y;
        bingo :-)

        (it's (a slight variation of) the official solution)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found