Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Barstool Trivia

by dchetlin (Friar)
on Dec 15, 2000 at 06:02 UTC ( #46775=perlmeditation: print w/ replies, xml ) Need Help??

While sitting at a bar after a ballgame, conversation inevitably turns to sports trivia. Sports trivia is really based around stumping and confounding your buddies with obscure, cleverly put, ambiguous, and potentially misleading questions.

The classic "How many ways to get on base can you name?" is a great example.

"Single, double, triple..."

"No, no, no! Those are all just base hits. That counts as one."

"Oh. OK. Base hit, walk, hit by pitch, wild pitch, ..."

"Wild pitch?! You can't get on base with a wild pitch."

"You could if you swung at it and it was your third strike."

"Well, that's the dropped third strike rule. Nothing to do with wild pitches."

&c. You get the idea. Lately I've been trying to extend this game to Perl questions when hanging out with Perl buddies.

So here is my favorite question so far. I thought a few monks might be interested, and might also have some of their own.

How many distinct ways can you name to start a new scope?

Hint: a loop is a loop is a loop, and it just counts for one.

-dlc

Comment on Barstool Trivia
Re: Barstool Trivia
by Dominus (Parson) on Dec 15, 2000 at 08:08 UTC
    I would just have said "start a block". Otherwise you have to worry about whether a bare block counts as a loop.

    Anyway, I'll toss one in: eval on a string starts a new scope. I can think of at least one other one, but I don't want to be a spoil sport so I'll let someone else jump in.

    As for the baseball thing, I know that I knew this at one time, and I'm sure there are either seven or eight ways to reach base, but I'm not sure which, and I can only think of seven. So if anyone out there knows that it's eight, please let me know so that I can rack my brains for a while.

      Were you thinking of creating a file boundary with do or require? Or does that not count? What about fork? :)

        Says saucepan:
        > Were you thinking of creating a file boundary with do or require?
        Yes, that's the only other one I could think of.

        Dan, what are we missing?

Re: Barstool Trivia
by Caillte (Friar) on Dec 15, 2000 at 22:29 UTC
    system("perl -w anotherscript.pl");

    Though that seems a little pointless ;)

Re: Barstool Trivia
by redcloud (Parson) on Dec 15, 2000 at 22:50 UTC
    Hmmm. What about package?

      It's not clear that changing packages counts as a scope since it doesn't affect lexical variables:

      package SomePackage; my $foo = "bar\n"; package main; print $foo; # prints 'bar'

      I could be wrong about the formal definition of a "scope", though. We'll have to leave that up to the judge. :)

Re: Barstool Trivia
by dws (Chancellor) on Dec 16, 2000 at 02:46 UTC
    By surrounding the -e argument in code, perl -n and perl -p effectively create new scopes.
Re: Barstool Trivia
by extremely (Priest) on Dec 25, 2000 at 05:27 UTC
    OK, i'm going nuts, what is the final list here? This one just dropped off radar but it is bugging me still!

    --
    $you = new YOU;
    honk() if $you->love(perl)

      Oops, sorry about that. I'd given the answer in a /msg and totally forgotten that I hadn't given it in a node.

      Recall that this is a fairly ambiguous question, and that my answer really just reveals what the source considers to be different scopes.

      That answer is found in cop.h, line 360 as of recent perls.

      #define CXt_NULL 0 #define CXt_SUB 1 #define CXt_EVAL 2 #define CXt_LOOP 3 #define CXt_SUBST 4 #define CXt_BLOCK 5 #define CXt_FORMAT 6

      OK, the easy ones first

      • CXt_LOOP is for, e.g., while (<>) { ... }.
      • CXt_BLOCK is for regular old blocks, like do { ... } while <>.
      • CXt_EVAL is for eval STRING, not eval BLOCK, which is covered by CXt_BLOCK.
      • CXt_SUB is for a subroutine.

      That leaves three: CXt_NULL, CXt_SUBST, and CXt_FORMAT. You can probably guess what CXt_SUBST and CXt_FORMAT do, even though the fact that they start new scopes may be surprising.

      [~] $ perl -le'$_="foo";s/foo/my $x="bar"/e;print;print ">>>$x<<<"' bar >>><<<

      There's s/// creating a new scope. Here's format:

      [~] $ cat tmp/format format = @<<<<<<< my $x = "foo" . write; print ">>>$x<<<"; [~] $ perl -l !$ perl -l tmp/format foo >>><<<

      OK, so what does that leave us with? CXt_NULL. CXt_NULL is the block passed to `sort', strangely enough. It's also referred to as a pseudo-block throughout the source.

      -dlc

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-12-21 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls