Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to have / in FILEHANDLE?

by Mark_Galeck (Novice)
on May 28, 2012 at 02:38 UTC ( #972769=perlquestion: print w/ replies, xml ) Need Help??
Mark_Galeck has asked for the wisdom of the Perl Monks concerning the following question:

If
open 'FILE', file; print <FILE>
prints
contents
then why does
open '/FILE', file; print </FILE>
print
/FILE
Thank you, Mark

Comment on how to have / in FILEHANDLE?
Select or Download Code
Re: how to have / in FILEHANDLE?
by Anonymous Monk on May 28, 2012 at 02:54 UTC

    Because that is how <> diamond operator works, it wants a bareword if you want readline , otherwise you get glob

    But, you should never use arbitrary strings as filehandles, its just painful for the sake of being painful (a stupid idea)

Re: how to have / in FILEHANDLE?
by toolic (Chancellor) on May 28, 2012 at 02:55 UTC
Re: how to have / in FILEHANDLE?
by tobyink (Abbot) on May 28, 2012 at 06:45 UTC

    You can't have a slash in a filename for the same reason you can't have one in a sub name:

    sub hello/world { say "Hello World"; }

    Subs and non-lexical filehandles are declared using a "bareword", and barewords can't contain slashes. A better question is why in the name of all that is good and holy would you want to do this?!

    You do know that the file handle and the file name have nothing to do with each other?

    OK, I'll admit I lied earlier. It is possible to name a sub with a slash. You just need to declare it and call it using unusual syntax...

    *{'hello/world'} = sub { print shift, "\n"; }; &{'hello/world'}("Hello World!");

    And similarly it can be done with filehandles...

    open *{'hello/world'}, '>', '/home/tai/tmp/helloworld.txt'; print {*{'hello/world'}} "Hello World!\n";

    The "why in the name of all that is good and holy" question still stands though.

    Nowadays there's rarely any excuse to not be using lexical file handles. (i.e. file handles declared using the my keyword.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      And similarly it can be done with filehandles...

      Like the OP shows, the globularness isn't required, just quote a string

      open '/ugly', 'foo.txt' or die $!; print readline '/ugly'; close '/ugly';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://972769]
Approved by toolic
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-07-29 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls