Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to handling string contains single quote and double quote (http://www.velocityreviews.com/forums/t902872-how-to-handling-string-contains-single-quote-and-double-quote.html)

vikrant 04-28-2007 08:41 AM

How to handling string contains single quote and double quote
 
Hi,
I facing a problem in saving a string contains single and double quote
into a file .The string look like the following:-

Example:-
Developer's community "com.lang.perl"
--------------------------------------------------------
The part of Code is:-

#!/bin/perl -w
use strict;

my $Date="10-09-2007";

system("echo '$Date;$String_Value' > data.txt");
----------------------------------------------------------------------------------------
Here,the $String_Value Variable store the string which i have
mentioned above.

I cannot put the slash( \ ) before the double quote's because in my
case value of input string is dynamic.
Also,I know putting a slash before double quote do not solve my
problem because single quote is left which will create the problem.I
get to know about this when i put slash before the double quote's.

The error coming on screen is:-
------------------------------------------------------------------------------------------------
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
-----------------------------------------------------------------------------------------------------

So,please tell me how do i handle this type of input.
I apologize if someone already asked this question earlier.

Thanks and Regards,
Vikrant


vikrant 04-28-2007 08:45 AM

Re: How to handling string contains single quote and double quote
 
On Apr 28, 1:41 pm, vikrant <vikrant.kan...@gmail.com> wrote:
> Hi,
> I facing a problem in saving a string contains single and double quote
> into a file .The string look like the following:-
>
> Example:-
> Developer's community "com.lang.perl"
> --------------------------------------------------------
> The part of Code is:-
>
> #!/bin/perl -w
> use strict;
>
> my $Date="10-09-2007";
>
> system("echo '$Date;$String_Value' > data.txt");
> ----------------------------------------------------------------------------------------
> Here,the $String_Value Variable store the string which i have
> mentioned above.
>
> I cannot put the slash( \ ) before the double quote's because in my
> case value of input string is dynamic.
> Also,I know putting a slash before double quote do not solve my
> problem because single quote is left which will create the problem.I
> get to know about this when i put slash before the double quote's.
>
> The error coming on screen is:-
> ------------------------------------------------------------------------------------------------
> sh: -c: line 0: unexpected EOF while looking for matching `''
> sh: -c: line 1: syntax error: unexpected end of file
> -----------------------------------------------------------------------------------------------------
>
> So,please tell me how do i handle this type of input.
> I apologize if someone already asked this question earlier.
>
> Thanks and Regards,
> Vikrant



Sorry for the Subject line mistake it should be something like
following:-
How to handle the string contains single quote and double quote


Paul Lalli 04-28-2007 10:26 AM

Re: How to handling string contains single quote and double quote
 
On Apr 28, 4:41 am, vikrant <vikrant.kan...@gmail.com> wrote:
> Hi,
> I facing a problem in saving a string contains single and double quote
> into a file .The string look like the following:-
>
> Example:-
> Developer's community "com.lang.perl"
> --------------------------------------------------------
> The part of Code is:-
>
> #!/bin/perl -w
> use strict;
>
> my $Date="10-09-2007";
>
> system("echo '$Date;$String_Value' > data.txt");
> ---------------------------------------------------------------------------*-------------
> Here,the $String_Value Variable store the string which i have
> mentioned above.
>
> I cannot put the slash( \ ) before the double quote's because in my
> case value of input string is dynamic.
> Also,I know putting a slash before double quote do not solve my
> problem because single quote is left which will create the problem.I
> get to know about this when i put slash before the double quote's.
>
> The error coming on screen is:-
> ---------------------------------------------------------------------------*---------------------
> sh: -c: line 0: unexpected EOF while looking for matching `''
> sh: -c: line 1: syntax error: unexpected end of file
> ---------------------------------------------------------------------------*--------------------------
>
> So,please tell me how do i handle this type of input.
> I apologize if someone already asked this question earlier.


Why are you calling a system command to do the echoing? Just open the
file for writing in Perl, and print the line to the file in Perl.
Then you don't have to worry about any escaping of quotes.

open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
print $fh "$Date;$String_Value\n";
close $fh;

Paul Lalli


vikrant 04-28-2007 11:35 AM

Re: How to handling string contains single quote and double quote
 
On Apr 28, 3:26 pm, Paul Lalli <mri...@gmail.com> wrote:
> On Apr 28, 4:41 am, vikrant <vikrant.kan...@gmail.com> wrote:
>
>
>
> > Hi,
> > I facing a problem in saving a string contains single and double quote
> > into a file .The string look like the following:-

>
> > Example:-
> > Developer's community "com.lang.perl"
> > --------------------------------------------------------
> > The part of Code is:-

>
> > #!/bin/perl -w
> > use strict;

>
> > my $Date="10-09-2007";

>
> > system("echo '$Date;$String_Value' > data.txt");
> > ---------------------------------------------------------------------------*-------------
> > Here,the $String_Value Variable store the string which i have
> > mentioned above.

>
> > I cannot put the slash( \ ) before the double quote's because in my
> > case value of input string is dynamic.
> > Also,I know putting a slash before double quote do not solve my
> > problem because single quote is left which will create the problem.I
> > get to know about this when i put slash before the double quote's.

>
> > The error coming on screen is:-
> > ---------------------------------------------------------------------------*---------------------
> > sh: -c: line 0: unexpected EOF while looking for matching `''
> > sh: -c: line 1: syntax error: unexpected end of file
> > ---------------------------------------------------------------------------*--------------------------

>
> > So,please tell me how do i handle this type of input.
> > I apologize if someone already asked this question earlier.

>
> Why are you calling a system command to do the echoing? Just open the
> file for writing in Perl, and print the line to the file in Perl.
> Then you don't have to worry about any escaping of quotes.
>
> open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
> print $fh "$Date;$String_Value\n";
> close $fh;
>
> Paul Lalli


Thanks for the information.Actually,the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.
Comments are most welcome.

Thanks and Regards,
Vikrant


Jens Thoms Toerring 04-28-2007 11:45 AM

Re: How to handling string contains single quote and double quote
 
vikrant <vikrant.kansal@gmail.com> wrote:
> On Apr 28, 3:26 pm, Paul Lalli <mri...@gmail.com> wrote:
> > On Apr 28, 4:41 am, vikrant <vikrant.kan...@gmail.com> wrote:
> >
> > > system("echo '$Date;$String_Value' > data.txt");

> >
> > Why are you calling a system command to do the echoing? Just open the
> > file for writing in Perl, and print the line to the file in Perl.
> > Then you don't have to worry about any escaping of quotes.
> >
> > open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
> > print $fh "$Date;$String_Value\n";
> > close $fh;
> >
> > Paul Lalli


> Thanks for the information.Actually,the code is a part of a
> function,which called again and again.So,i thought that opening and
> closing a file on each call may effect the performance.That was the
> only reason of using the system command.


Actually, your approach uses a lot more resources and time.
The system() function must create a new process, which in turn
executes a shell to deal with the command you sent to system().
And the file must be opened and closed again anyway, but now
the shell will do that. So all the system() call does is add
overhead, saving you nothing at all (beside making it harder
to check for errors).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

vikrant 04-28-2007 03:38 PM

Re: How to handling string contains single quote and double quote
 
On Apr 28, 4:45 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> vikrant <vikrant.kan...@gmail.com> wrote:
> > On Apr 28, 3:26 pm, Paul Lalli <mri...@gmail.com> wrote:
> > > On Apr 28, 4:41 am, vikrant <vikrant.kan...@gmail.com> wrote:

>
> > > > system("echo '$Date;$String_Value' > data.txt");

>
> > > Why are you calling a system command to do the echoing? Just open the
> > > file for writing inPerl, and print the line to the file inPerl.
> > > Then you don't have to worry about any escaping of quotes.

>
> > > open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
> > > print $fh "$Date;$String_Value\n";
> > > close $fh;

>
> > > Paul Lalli

> > Thanks for the information.Actually,the code is a part of a
> > function,which called again and again.So,i thought that opening and
> > closing a file on each call may effect the performance.That was the
> > only reason of using the system command.

>
> Actually, your approach uses a lot more resources and time.
> The system() function must create a new process, which in turn
> executes a shell to deal with the command you sent to system().
> And the file must be opened and closed again anyway, but now
> the shell will do that. So all the system() call does is add
> overhead, saving you nothing at all (beside making it harder
> to check for errors).
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de


Hi JensThoms,

Thanks for sharing the information.It is very helpful to me.

Regards,
Vikrant


Joe Smith 05-02-2007 06:23 AM

Re: How to handling string contains single quote and double quote
 
vikrant wrote:
> the code is a part of a
> function,which called again and again.So,i thought that opening and
> closing a file on each call may effect the performance.That was the
> only reason of using the system command.
> Comments are most welcome.


FYI: Invoking system() like that means 32 separate file operations each
time it is called.

linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
3 open("/etc/ld.so.cache", O_RDONLY) = 3
4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
7 open("/lib/libdl.so.2", O_RDONLY) = 3
8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
9 open("/lib/libc.so.6", O_RDONLY) = 3
10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
14 open("/etc/mtab", O_RDONLY) = 3
15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
16 open("/proc/meminfo", O_RDONLY) = 3
17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
28 access("/bin/sh", X_OK) = 0
29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
30 access("/bin/sh", X_OK) = 0
31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666

Wade Ward 05-02-2007 06:36 AM

Re: How to handling string contains single quote and double quote
 

"Joe Smith" <joe@inwap.com> wrote in message
news:hsKdnbJqrI3ls6XbnZ2dnUVZ_vyunZ2d@comcast.com. ..
> vikrant wrote:
>> the code is a part of a
>> function,which called again and again.So,i thought that opening and
>> closing a file on each call may effect the performance.That was the
>> only reason of using the system command.
>> Comments are most welcome.

>
> FYI: Invoking system() like that means 32 separate file operations each
> time it is called.
>
> linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
> 1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars
> */]) = 0
> 2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file
> or directory)
> 3 open("/etc/ld.so.cache", O_RDONLY) = 3
> 4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
> 5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
> 6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
> 7 open("/lib/libdl.so.2", O_RDONLY) = 3
> 8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
> 9 open("/lib/libc.so.6", O_RDONLY) = 3
> 10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
> 11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
> 12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
> 13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
> 14 open("/etc/mtab", O_RDONLY) = 3
> 15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
> 16 open("/proc/meminfo", O_RDONLY) = 3
> 17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
> 18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) =
> 0
> 19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
> 20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
> 21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
> 22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
> 23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
> 24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
> 25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file
> or directory)
> 26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file
> or directory)
> 27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
> 28 access("/bin/sh", X_OK) = 0
> 29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
> 30 access("/bin/sh", X_OK) = 0
> 31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666

What's the 32nd?
--
WW



vikrant 05-17-2007 04:37 PM

Re: How to handling string contains single quote and double quote
 
On May 2, 11:23 am, Joe Smith <j...@inwap.com> wrote:
> vikrant wrote:
> > the code is a part of a
> > function,which called again and again.So,i thought that opening and
> > closing a file on each call may effect the performance.That was the
> > only reason of using the system command.
> > Comments are most welcome.

>
> FYI: Invoking system() like that means 32 separate file operations each
> time it is called.
>
> linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
> 1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
> 2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
> 3 open("/etc/ld.so.cache", O_RDONLY) = 3
> 4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
> 5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
> 6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
> 7 open("/lib/libdl.so.2", O_RDONLY) = 3
> 8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
> 9 open("/lib/libc.so.6", O_RDONLY) = 3
> 10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
> 11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
> 12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
> 13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
> 14 open("/etc/mtab", O_RDONLY) = 3
> 15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
> 16 open("/proc/meminfo", O_RDONLY) = 3
> 17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
> 18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
> 19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
> 20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
> 21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
> 22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
> 23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
> 24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
> 25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
> 26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
> 27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
> 28 access("/bin/sh", X_OK) = 0
> 29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
> 30 access("/bin/sh", X_OK) = 0
> 31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666


Once again thanks for providing this valuable information.i shall try
to follow the above procedure to get the behavior of the code.

Regards,
vikrant



All times are GMT. The time now is 04:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.