develooper Front page | perl.perl5.porters | Postings from March 2003

Re: $_ not optional with lexical prints

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
March 20, 2003 09:24
Subject:
Re: $_ not optional with lexical prints
Message ID:
20030320182343.DFD5.H.M.BRAND@hccnet.nl
On Thu 20 Mar 2003 17:45, Ronald J Kimball <rjk@linguist.Thayer.dartmouth.edu> wrote:
> On Thu, Mar 20, 2003 at 05:39:50PM +0100, H.Merijn Brand wrote:
> 
> > Why is $_ *not* optional if the file handle is lexical?
> > 
> > a5:/data/0637/lep 157 > perl5.6.1 -le 'my $v; open $v, ">/dev/null" or die $!; $_ = "Test"; print $v'
> > GLOB(0x40013964)
> > a5:/data/0637/lep 158 > perl5.8.0 -le 'my $v; open $v, ">/dev/null" or die $!; $_ = "Test"; print $v'
> > GLOB(0x4001a364)
> > a5:/data/0637/lep 159 > perl5.9.0 -le 'my $v; open $v, ">/dev/null" or die $!; $_ = "Test"; print $v'
> > GLOB(0x400197dc)
> > a5:/data/0637/lep 160 >
> > 
> > [ Of course 'print $v $_' goes OK in all examples ]
> 
> Well, how is Perl supposed to tell the difference between:
> 
> my $v;
> open $v, ">/dev/null" or die $!;
> $_ = "Test";
> print $v;
> 
> and
> 
> my $v = "Test";
> print $v;

More elaborate:

--8<---
#!/pro/bin/perl

use strict;
use warnings;
use Devel::Peek;

our ($v, $w);

sub G ($$)
{
    my ($name, $var) = @_;
    print "============== \$$name = $var\n";
    Dump ($var);
    } # G

$v = "Test";
G ("v", $v);

open $w, ">/dev/null" or die $!;
G ("w", $w);
-->8---

a5:/pro/3gl/CPAN/perl-current 203 > perl xx.pl
============== $v = Test
SV = PV(0x4001ac64) at 0x40065224
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x40021818 "Test"\0
  CUR = 4
  LEN = 5
============== $w = GLOB(0x4001a8c4)
SV = PV(0x4001ac64) at 0x40065224
  REFCNT = 1
  FLAGS = (PADMY,ROK)
  RV = 0x4001a8c4
  SV = PVGV(0x40021860) at 0x4001a8c4
    REFCNT = 2
    FLAGS = (GMG,SMG)
    IV = 0
    NV = 0
    MAGIC = 0x40021ee0
      MG_VIRTUAL = &PL_vtbl_glob
      MG_TYPE = PERL_MAGIC_glob(*)
      MG_OBJ = 0x4001a8c4
    NAME = "$w"
    NAMELEN = 2
    GvSTASH = 0x4001a7ec        "main"
    GP = 0x400218a8
      SV = 0x4001a954
      REFCNT = 1
      IO = 0x4001a9b4                   <==========================
      FORM = 0x0
      AV = 0x0
      HV = 0x0
      CV = 0x0
      CVGEN = 0x0
      GPFLAGS = 0x0
      LINE = 19
      FILE = "xx.pl"
      FLAGS = 0x0
      EGV = 0x4001a8c4  "$w"
  PV = 0x4001a8c4 ""
  CUR = 0
  LEN = 0
a5:/pro/3gl/CPAN/perl-current 204 >

-- 
H.Merijn Brand        Amsterdam Perl Mongers (http://amsterdam.pm.org/)
using perl-5.6.1, 5.8.0 & 633 on HP-UX 10.20 & 11.00, AIX 4.2, AIX 4.3,
  WinNT 4, Win2K pro & WinCE 2.11.  Smoking perl CORE: smokers@perl.org
http://archives.develooper.com/daily-build@perl.org/   perl-qa@perl.org
send smoke reports to: smokers-reports@perl.org, QA: http://qa.perl.org



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About