Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Having problem to substitute "'"

by Sun751 (Beadle)
on Jul 24, 2009 at 01:03 UTC ( [id://782815]=perlquestion: print w/replies, xml ) Need Help??

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

I am having problem with "'";
I have a list of commands in array which I am extracting and running one by one but when I try to run chdir its throwing error due to "'"; for example,
path = 'abc/dead/apple'
I tried to substitute "'" by
$path =~ s/'//g;
for some reason its not working and I tried chomp which is also not working can any one suggest me solution for this? please.
Cheers

Replies are listed 'Best First'.
Re: Having problem to substitute "'"
by graff (Chancellor) on Jul 24, 2009 at 01:30 UTC
    I am having problem with "'";

    But you've managed to give no evidence or clear description of this.

    I have a list of commands in array which I am extracting and running one by one but when I try to run chdir its throwing error due to "'";

    What exactly was the error message you got, and what exactly was the operation that caused the error? The notion of "running commands one by one" is actually quite vague. There are various, very different kinds of operations that could be described this way (because the description is vague), and various different ways they could be done wrong. No point in us trying to guess what you did.

    I tried to substitute "'" by $path =~ s/'//g;

    for some reason its not working

    How do you know it didn't work? And what does "not working" mean, exactly?

    and I tried chomp which is also not working

    Note that chomp is only supposed to remove the value of $/ from the end of the arg(s) that you pass to it; if the value of $/ happens to not be present at the end of the arg(s), nothing happens. (The chop function is different -- it just removes the last character from its arg(s), whatever the character may be.)

    Cheers

    Pointing out why we can't help you is a cheery exercise, indeed. (not)

    (Update: Forgot to mention... if a variable like $path really does contain one or more apostrophe characters, then $path =~ s/'//g will definitely remove them, and you can test this out independently on the command line -- check the output from the following command:

    echo "this isn't a problem if you're using bash" | perl -pe "s/'//g"
    However, if a file or path name really does contain one or more apostrophes (I feel sorry for you in this case), maybe just stripping them out from command lines is the wrong way to go -- try escaping them instead, or doing other things to protect them from being "interpreted" by a shell as metacharacters.)
Re: Having problem to substitute "'"
by toolic (Bishop) on Jul 24, 2009 at 01:39 UTC
    $path =~ s/'//g;
    will remove all single quote characters from the string $path. Here is a contrived example:
    use warnings; use strict; my $path = q('/abc/dead/apple'); print "$path\n"; $path =~ s/'//g; print "$path\n";

    This prints out:

    '/abc/dead/apple' /abc/dead/apple
Re: Having problem to substitute "'"
by AnomalousMonk (Archbishop) on Jul 24, 2009 at 01:12 UTC
    The string  'abc/dead/apple' has no  ' (single quote) character in it for which anything else (including the empty string) could be substituted.

    Can you please give an example of just what you are trying to do and what the result is?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://782815]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-16 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found