http://www.perlmonks.org?node_id=951037

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

I'm having an issue simply opening a file and having perl output it on the screen when I run it in the command prompt.

Basically...if I put the file in the same folder as my .pl script, and run it...it works fine.

However, when I change the location to say something like this:
$loc = '\\Server\Shared Folder\Text.txt'; I can't get it to read it?
Here's my code...what am I doing wrong?:

print "ENTER ACCOUNT NUMBER: "; $acct = <>; print "\n>SEARCHING FOR DISPATCH INSTRUCTIONS ON ACCOUNT #: $acct Ple +ase Wait..."; $loc = "\\\\Server\\MappedNetworkDrive\\Folder\\$acct\\DispatchInstruc +tions.txt"; print $loc; #Get File Info open(MYFILE, "$loc") or print "Didn't Open $loc"; while (<MYFILE>) { sleep(5); chomp; print "$_\n"; } close (MYFILE);
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Perl Program works on local machine, not over network?
by toolic (Bishop) on Jan 31, 2012 at 19:08 UTC
    $acct will have a newline in it. chomp it.
    print "ENTER ACCOUNT NUMBER: "; $acct = <>; chomp $acct;
      Worked Great...THANKS BIG TIME! YOU GET MY VOTE :)
      I love it when a program comes together - jdhannibal
Re: Perl Program works on local machine, not over network?
by dasgar (Priest) on Jan 31, 2012 at 19:12 UTC

    A couple of suggestions to help you get more help from perl itself:

      1.)  Add use strict; and use warnings; at the start of your scripts. Of course, this will cause you to need to use my the first time that you use a variable.

      2.)  Modify you line of code where you open the file to print out a more useful error message by changing it to:

    open(MYFILE, "$loc") or print "Didn't Open $loc: $!\n";

    With more useful warning and error messages, it will be easier for you (and others) to debug the issue(s).

      Even better with the open is to use the three parameter variant and lexical file handles:

      open my $inFile, '<', $sloc or die "Can't open $loc: $!\n";

      which makes the open mode explicit (and avoids a potential injection attack), and gives all the strictly goodness advantages from using the lexical file handle.

      Oh, and don't interpolate a single variable ("$loc") - there is no need and clutters the code.

      True laziness is hard work