Solved

Perl: Backslash found where operator expected

Posted on 2014-04-03
4
2,265 Views
Last Modified: 2014-04-03
Hello expert,

This Perl code works when the path used for the target
file is in the same directory as the Perl executable.
But when I change to a shared drive on the network
   open (MYFILE, '>>I:\Internal_Processes\Service_Now_Discovery\CMDB_data\data2.txt');
I get
Backslash found where operator expected at open_ie4.pl line 18, near "Internal_Processes"
Obviously it doesn't like the the backslashes in the file path but how do I tell it
where the target file is without using backslashes in the file path?

Thanks

Allen

+++++++++++++++complete code+++++++++++++++++++++
  #!/strawberry perl

  use v5.10.0;
use warnings;
#use strict;

# go to url that is the table of servers in ServiceNow configuration    
# read the table into variable $req
 use LWP::UserAgent;
 $ua = LWP::UserAgent->new;
 $req = HTTP::Request->new(GET => 'https://lhphgtst.service-now.com/cmdb_ci_datacenter.do?XML');
 $req->authorization_basic('allen.pitts', 'password');
# write the table into a file on a local disk
   open (MYFILE, '>>I:\Internal_Processes\Service_Now_Discovery\CMDB_data\data2.txt');
 print MYFILE $ua->request($req)->as_string;
  close (MYFILE);
++++++++++++++++++++++end complete code++++++++++++++++++++++++
0
Comment
Question by:9apit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39975663
Use forward slashes instead of back slashes.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 39975694
Additionally,

1) Commenting out the 'use strict; statement is a bad idea.  Ignoring the errors that it would point out is never a good idea.

2a) You should use a lexical var for the filehandle instead of a bareword.

2b) You should use the 3 arg form of open

2c) You should always check the return code of an open call to verify that is was successful and take proper action if it failed.  That normally means adding a 'die' statement which includes the filename and reason it failed.

#!/strawberry perl

use v5.10.0;
use warnings;
use strict;
use LWP::UserAgent;

# go to url that is the table of servers in ServiceNow configuration    
# read the table into variable $req
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => 'https://lhphgtst.service-now.com/cmdb_ci_datacenter.do?XML');
$req->authorization_basic('allen.pitts', 'password');

# write the table into a file on a local disk
my $file = 'I:/Internal_Processes/Service_Now_Discovery/CMDB_data/data2.txt';
open my $MYFILE, '>>', $file or die "failed to open '$file' <$!>";
print $MYFILE $ua->request($req)->as_string;
close ($MYFILE);

Open in new window

0
 

Author Comment

by:9apit
ID: 39975877
Dr. FishMonger,

Awesome answer.

1) Commenting out the 'use strict; statement is a bad idea.  Ignoring the errors that it would point out is never a good idea.
I get this. Sort of reminds of 'option explicit' in VB. It requires that variable be declared.

2a) You should use a lexical var for the filehandle instead of a bareword.
This one took some research
Lexical Variables
Lexical variables are those declared with my.
Lexical variables must be explicitly declared using the my qualifier.
Lexical variables belong to no package so cannot be fully qualified with a package name.
I think 'use strict' also disables the inappropriate use of barewords.
I think a bareword is an undeclared variable or filehandle.
Researched 'package' but could never get a clear explanation of what a package is.

2b) You should use the 3 arg form of open...
Again had to wrap my head around this but found
Three arguments: the filehandle to open or vivify, the mode of the filehandle, and the name of the file.
So in your excellent code: $MYFILE is the filehandle; >> is the mode, append; [could also be >, overwrite, other modes?] and $file is the name and path of the file
or die is a sort elegant way of handling an error

2c) You should always check the return code of an open call
die is a sort elegant way of handling an error

Thanks

Allen in Dallas (Perl noob)
0
 

Author Closing Comment

by:9apit
ID: 39975894
excellent reply
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 194
work on ods spreadsheet with perl in ubuntu 4 81
How to strip .csv from file name 9 85
Perl string filter 5 88
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question