in reply to Re: Fake JAPH
in thread Fake JAPH

Fundamentally correct: Mr. Muskrat already pinpointed some details. What I'd like to stress is that in Perl (5, that is!) there's hardly anything like a *string* context: C<q...> is an (hopefully somewhat cryptic) alternative to scalar().

Had I not put it in, <DATA> would have been in list context, thus returning a list of all the lines of that FH. In which case I may have used

# This prints: Just another Perl hacker, seek DATA,15,0 and print +(<DATA>)[0];
instead, which is not just as nice IMHO...

Replies are listed 'Best First'.
Re^3: Fake JAPH
by Anonymous Monk on Dec 14, 2004 at 12:43 UTC
    What I'd like to stress is that in Perl (5, that is!) there's hardly anything like a *string* context

    Actually, there is, and lots and lots of it. Perl could not do automatical transformation between numerical values and string values if it wasn't for string context:

    #!/usr/bin/perl use strict; use warnings; use Devel::Peek; my $var = 13; Dump($var); print "\n\n"; my $dummy = q...$var; Dump($var); __END__ SV = IV(0x8194b2c) at 0x8193d70 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 13 SV = PVIV(0x8184448) at 0x8193d70 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,POK,pIOK,pPOK) IV = 13 PV = 0x818bca0 "13"\0 CUR = 2 LEN = 3
    As you can see, the use of $var in string context caused the upgrade from an IV to a PVIV.

    String context is also what triggers the stringify overload in objects (if defined).

      What I'd like to stress is that in Perl (5, that is!) there's hardly anything like a *string* context
      Actually, there is, and lots and lots of it. Perl could not do automatical transformation between numerical values and string values if it wasn't for string context:
      Indeed Perl does magic dwimmeries with automatic conversions between numerical and string values depending on how these are used, but what I meant is that only to a very limited extent there does exist a well-definite concept of "string (or numerical) context" comparable to, say, "scalar (or list) context". As an example, quoting from 'perldoc perlvar':
      $!      If used numerically, yields the current value of the C "errno"
              variable, or in other words, if a system or library call fails,
              it sets this variable.  This means that the value of $! is
              meaningful only immediately after a failure:
      As you can see, it consistently avoids speaking of "numerical context". Incidentally my stress on Perl5 was due to the fact that Perl6 is supposed to have such contexts instead (and to support them unambiguously!)

      Also compare e.g.

      http://www.google.it/search?q=%22string+context%22+site:perldoc.com
      with
      http://www.google.it/search?q=%22scalar+context%22+site:perldoc.com