[Chicago-talk] OPEN() conditional

tiger peng tigerpeng2001 at yahoo.com
Wed Jul 6 07:12:51 PDT 2011


The empty string does work.

...
               If FILEHANDLE is an undefined lexical ("my")
               variable the variable is assigned a reference to a
               new anonymous filehandle, otherwise if FILEHANDLE
               is an expression, its value is used as the name of
               the real filehandle wanted.  (This is considered a
               symbolic reference, so "use strict 'refs'" should
               not be in effect.)
...
               Open returns nonzero upon success, the undefined
               value otherwise.

...

/tmp> perl -MData::Dumper -lwe '
my $fh="";
print Dumper $fh;
open $fh, "<", "test.txt";
print Dumper $fh;
print while <$fh>;
print Dumper $fh;
'
$VAR1 = '';

$VAR1 = '';

test file

$VAR1 = '';


/tmp> perl -MData::Dumper -lwe '
my $fh;
print Dumper $fh;
open $fh, "<", "test.txt";
print Dumper $fh;
print while <$fh>;
print Dumper $fh;
'
$VAR1 = undef;

$VAR1 = \*{'::$fh'};

test file

$VAR1 = \*{'::$fh'};

/tmp> perl -MData::Dumper -lwe '
my $fh="F";
print Dumper $fh;
open $fh, "<", "test.txt";
print Dumper $fh;
print while <F>;
print Dumper $fh;
'
Name "main::F" used only once: possible typo at -e line 6.
$VAR1 = 'F';

$VAR1 = 'F';

test file

$VAR1 = 'F';


perl -MData::Dumper -lwe '
my $fh="";
print Dumper $fh;
open $fh, "<", "test.txt"
 or warn Dumper $fh;
print while <$fh>;
print Dumper $fh;
'
$VAR1 = '';

test file

$VAR1 = '';



________________________________
From: Randal L. Schwartz <merlyn at stonehenge.com>
To: Steven Lembark <lembark at wrkhors.com>
Cc: chicago-talk at pm.org
Sent: Tuesday, July 5, 2011 5:42 PM
Subject: Re: [Chicago-talk] OPEN() conditional

>>>>> "Steven" == Steven Lembark <lembark at wrkhors.com> writes:

Steven> my $fh  = '';

Did you want autovivification there?  If so, leave $fh as undef, not
empty string.

Steven> open $fh, '<', $path1
Steven> or 
Steven> open $fh, '<', $path2
Steven> or 
Steven> die "$path1, $path2";

If it accidentally works with empty string, I'd be surprised.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
_______________________________________________
Chicago-talk mailing list
Chicago-talk at pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20110706/42a14865/attachment.html>


More information about the Chicago-talk mailing list