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

Hi Experts,

I have written small Perl program to read and update network file. Its working fine in we run by command prompt.
But if we invoke by External program program dies without any error.

I have not used any Module to read file. its running on windows environment file path will be like \\\\xxxx\\f$\\....
i have tried forward slash and escaping $ but nothing works. Can you please help me?

@wslfiles[0]='\\\\XXXXXXX\\f\$\\apps\\webapps\\auth.wsl'; my @wsl = @{$_[0]}; foreach $filename(@wsl) { open(FIN, $filename) or die "Could not open file '$filename' $!"; while (my $row = <FIN>) .... }
Thanks
Srini

Replies are listed 'Best First'.
Re: Network File read not working if invoked externally
by haukex (Archbishop) on Jan 05, 2017 at 11:09 UTC

    Hi SriniK,

    First, there's the questions that Corion already raised. I just wanted to add: inside single-quoted strings, you don't need to escape $ symbols, just escape single quotes (') and other backslashes (\) with backslashes. Note how there's an extra backslash in $wf1 in the following example:

    use warnings; use strict; my $wf1 = '\\\\XXXXXXX\\f\$\\apps\\webapps\\auth.wsl'; my $wf2 = '\\\\XXXXXXX\\f$\\apps\\webapps\\auth.wsl'; print $wf1, "\n"; print $wf2, "\n"; use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($wf1, $wf2); __END__ \\XXXXXXX\f\$\apps\webapps\auth.wsl \\XXXXXXX\f$\apps\webapps\auth.wsl $VAR1 = "\\\\XXXXXXX\\f\\\$\\apps\\webapps\\auth.wsl"; $VAR2 = "\\\\XXXXXXX\\f\$\\apps\\webapps\\auth.wsl";

    Hope this helps,
    -- Hauke D

Re: Network File read not working if invoked externally
by Corion (Patriarch) on Jan 05, 2017 at 08:15 UTC

    Most likely this is either an issue because the current directory is not what you think it is, or a permissions issue because the external program runs as a Windows service which usually does not have the permissions to access network devices.

    As you have not shown an SSCCE, it is quite hard to give you more concrete advice than to output all data as your program progresses to a log file and then compare the log files between a manual run and a run from the External program.

      Thanks for replying. I have attached the sample code here.

        If that is real, tested code, can you please explain what it is supposed to do:

        @wslfiles[0]='\\\\XXXXXXX\\f\$\\apps\\webapps\\auth.wsl'; my @wsl = @{$_[0]}; foreach $filename(@wsl)

        If that was not real code that raises the error, then please post short, real code that raises the error (after removing the server name etc.).

        From the error message, does the file mentioned in the error message exist?

        dir \\XXXXXXX\f$\apps\webapps\auth.wsl
Re: Network File read not working if invoked externally
by soonix (Chancellor) on Jan 06, 2017 at 13:51 UTC
    If it is really dying without error, you should follow the Basic debugging checklist, especially points 2 and 4:
    use Data::Dumper; ... print Dumper \@wsl; foreach $filename(@wsl) { warn "trying to open file '$filename'\n"; open(FIN, $filename) or die "Could not open file '$filename' $!"; warn "successfully opened file '$filename'\n"; while (my $row = <FIN>) ...
    I prefer to put filenames in configuration files¹, e.g. using Config::Tiny.
    • The advantage of this approach is that you can put your filenames there totally unchanged, including backslashes, spaces and other special characters.
    • Disadvantage: for characters outside the ASCII range you have to use the same encoding (binmode) as your editor (or whatever tool you use to put the filename into the config file).
    ¹ - obviously this is not feasible for the name of the configuration file itself…