virtuemart2 has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,
This is my first question so please be gentle.
My question is about

open

function, howto open text file in Linux and Microsoft

open (fh, "C:\windows\file.txt"); open fh $PATH;


Please tell if the above $PATH is ok? I mean like
"C:\windows\file.txt" any wrong with the syntax grammar?

Replies are listed 'Best First'.
Re: open function -- text file PATH
by NetWallah (Canon) on Sep 09, 2013 at 05:49 UTC
    Welcome !
    Please read the "File Input and Output" section of this site's tutorial on Input and Output.

    To understand why you need to escape backslashes, please read the documentation on string "interpolation".

    Basically, if you use double-quotes ("), the backslash(\) is the escape character. If you want to use that in the string, it needs to be doubled up.

    "C:\\windows\\file.txt"
    but Windows (and perl) allow you to use the "/" (forward-slash) instead of backslash, and using "/" does NOT requirie doubling up, and is easier to read.

    As you become more familiar with perl, you will understand the reasons to write "open" in the following form:

    my $filename="C:\\windows\\file.txt" ; ## or "C:/windows/file.txt" open (my $fh, "<", $filename) or die "Could not open $filename: $!";
    This is usually followed by:
    while (<$fh>){ # Process the contents of the record # which is stored in $_ } close $fh or warn "Could not close $filename:$!";

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: open function -- text file PATH
by kcott (Archbishop) on Sep 09, 2013 at 06:29 UTC

    G'day virtuemart2,

    Welcome to the monastery.

    You've got a few issues with that. Firstly, you can test that string yourself:

    $ perl -Mstrict -Mwarnings -le 'print "C:\windows\file.txt"' Unrecognized escape \w passed through at -e line 1. C:windows ile.txt

    Obviously, that's not good. Note that -Mwarnings (on the command line) is the same as use warnings; (in a script). When it's used, Perl tells you what's wrong; when you don't use it, you just see strange output without any explanation:

    $ perl -Mstrict -le 'print "C:\windows\file.txt"' C:windows ile.txt

    See warnings: it's your friend. Also note -Mstrict (as I have here, on the command line) is the same as use strict; (in a script). See strict: it, too, is your friend.

    If you're stuck with a situation where you need double-quotes, you must escape (prefix with a backslash) each backslash, i.e. write "\\" wherever you had "\":

    $ perl -Mstrict -Mwarnings -le 'print "C:\\windows\\file.txt"' C:\windows\file.txt

    That looks rather messy, and would be even messier with a long pathname. The best thing to avoid this, is to use single-quotes, i.e. 'C:\windows\file.txt'. I can't actually show that on the command line because I'm already using single-quotes; however, '...' is the same as q{...}, so:

    $ perl -Mstrict -Mwarnings -le 'print q{C:\windows\file.txt}' C:\windows\file.txt

    And, just for completeness, "..." is the same as qq{...}:

    $ perl -Mstrict -Mwarnings -le 'print qq{C:\windows\file.txt}' Unrecognized escape \w passed through at -e line 1. C:windows ile.txt

    Although, I've used braces ({...}) to delimit the string, you can use other characters: matchingleft-right pairs such as q[...] and q(...), and non-matchingidentical pairs such as q/.../ and q|...|. This is explained in perlop: Quote and Quote-like Operators, with further details in perlop: Quote-Like Operators. [Yes, those two section headings are somewhat confusing — I didn't write them. :-) ]

    That's strings sorted out. The next problem is open fh $PATH. Assuming you'd given $PATH a value, you'd get this somewhat cryptic message:

    $ perl -Mstrict -Mwarnings -e 'my $PATH = q{not_a_file}; open fh $PATH +' Can't locate object method "fh" via package "not_a_file" (perhaps you +forgot to load "not_a_file"?) at -e line 1.

    The problem is that you've used incorrect syntax for the open function. You could just make a minor change to fix the syntax; however, it would be much better if you wrote that like this:

    open my $fh, '<', $path or die "Can't open $path: $!";

    See open for a detailed explanation. Also look at die; and you'll find $! explained in perlvar.

    Putting all of that together:

    # Uncomment ONLY ONE of these: #my $path = 'C:\windows\path\to\filename'; #my $path = '/linux/path/to/filename'; # Now open your chosen file for reading: open my $fh, '<', $path or die "Can't open $path: $!";

    Finally, have a look at perlrun, so you can run those sorts of tests for yourself; and, I suspect, you'd benefit greatly from reading "perlintro -- a brief introduction and overview of Perl".

    -- Ken

Re: open function -- text file PATH
by choroba (Cardinal) on Sep 09, 2013 at 08:20 UTC
    In double quotes, backslashed are special. Use single quotes:
    open fh, 'C:\windows\file.txt';

    Note that using lowercase barewords for filehandles begs for troubles. It is safer to use lexical filehandles, 3-argument form of open, and also check the return value of open in case the opening did not succeed:

    open my $FH, '<', 'C:\windows\file.txt' or die $!;

    See open for details.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: open function -- text file PATH
by Marshall (Canon) on Sep 09, 2013 at 06:28 UTC
    There is not a need to use the escaped "back-slash".
    "C:\\windows\\file.txt" is fine but also
    "C:/windows/file.txt" is fine also.
    That works even with Windows.

    In general, I would recommend using the forward
    slash instead of the "back slash".

Re: open function -- text file PATH
by Laurent_R (Canon) on Sep 09, 2013 at 06:15 UTC

    open fh $PATH;

    As a brief additional comment to the clear and complete answer made by NetWallah, the line quoted above does not make any sense since $PATH has not been initialized and/or declared anywhere. I am not sure what your intention is with it, but it can be removed altogether.