Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

No this is not incorrect at all, it is exactly what you expect. The glob *X consists of the following elements $X @X %X &X and X (the filehandle). When you write open *X, $foo Perl opens $foo onto the filehandle X from the *X glob. Thus when you say close X you close the filehandle X - this is what your close $_ example does. When you say close *X Perl also does a close X as X is the filehandle in the *X glob - this is what your close $rh{$_} example does.

Update (changed code to better example)

#/usr/bin/perl -w use strict; my %rh = (E=> *E, F=> *F, G=> *G); { no strict 'refs'; print "\n\n\$_ has a value, used as a hard reference\n"; for (values %rh) { open $_, ">test.txt" or die $!; print $_ "$_ This works\n"; close $_ or die $!; open $_, "<test.txt" or die $!; print <$_>; close $_ or die $!; unlink "test.txt" or die $!; } print "\n\n\$_ has a value, used as a symbolic reference\n"; for (keys %rh) { open $_, ">test.txt" or die $!; print $_ "$_ This works\n"; close $_ or die $!; open $_, "<test.txt" or die $!; print <$_>; close $_ or die $!; unlink "test.txt" or die $!; } } # and under strict.... print "\n\n\$_ has a value, used as a hard reference\n"; for (values %rh) { open $_, ">test.txt" or die $!; print $_ "$_ This works\n"; close $_ or die $!; open $_, "<test.txt" or die $!; print <$_>; close $_ or die $!; unlink "test.txt" or die $!; } print "\n\n\$_ has a value, used as a symbolic reference\n"; for (keys %rh) { open $_, ">test.txt" or die $!; print $_ "$_ This works\n"; close $_ or die $!; open $_, "<test.txt" or die $!; print <$_>; close $_ or die $!; unlink "test.txt" or die $!; }

Part of the problem you are having is not distinguishing a hard reference (OK under strict) an a symbolic reference (not OK under strict)

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: unexpected close() success on (non-) filehandle by tachyon
in thread unexpected close() success on (non-) filehandle by mkmcconn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-16 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found