Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Perl Program works on local machine, not over network?

by jdlev (Scribe)
on Jan 31, 2012 at 18:43 UTC ( #951037=perlquestion: print w/replies, xml ) Need Help??
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 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 (Curate) 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951037]
Approved by toolic
[Discipulus]: oh and dont miss my last poetry!
[marioroy]: Hello all. After several attempts, am able to move forward and will soon release a Parallel:: ForkManager compatible MCE::Hobo:: Manager, also MCE::Hobo::Simple, and refactored MCE::Hobo engine supporting multiple instances. Wait works beautifully.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-05-26 08:20 GMT
Find Nodes?
    Voting Booth?