Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Escaping single quote in $string

by calebcall (Sexton)
on Feb 17, 2013 at 20:44 UTC ( #1019209=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that searches for media files to convert. These files could contain apostrophes in their name (i.e. the TV Show, How It's Made). That apostrophe (single quote) makes my script bomb out. What I need to do is escape it or remove it from the file name. I've tried the following, but none of them work. I'm sure I'm just doing something wrong.

$string =~ s/'/\\'/g;

$string =~ s/\'/\\'/g;

$string =~ s/\'//g;

Any help?


Replies are listed 'Best First'.
Re: Escaping single quote in $string
by moritz (Cardinal) on Feb 17, 2013 at 20:48 UTC
Re: Escaping single quote in $string
by tangent (Vicar) on Feb 17, 2013 at 21:15 UTC
    To remove the quote the third one you have there should work, as long as you don't do either of the first two beforehand:
    my $string = "How It's Made"; $string =~ s/\'//g; print "$string\n"; # prints "How Its Made"
Re: Escaping single quote in $string
by manorhce (Beadle) on Feb 17, 2013 at 21:57 UTC

    Hey calebcall I think you have not tried with the last match you have mentioned.

    my $string = q/hi it's mine test ' correct/; print $string if $string =~ s#\'#is#g;

    Thanks, Mano

Re: Escaping single quote in $string
by calebcall (Sexton) on Feb 17, 2013 at 22:10 UTC

    Ok, I re-looked at what I was doing and I'm starting to see what my problem is. So what I'm doing is assigning $infile to $outfile and changing the extension on outfile, then plugging those in to handbrakeCLI to do the conversion. I was trying to do the search and replace on the $outfile, when I should be doing it on the $infile. So, it looks like as of now, it seems to be working.


      I take it you're using the system($string) syntax (or similar) for calling out to handbrake? Have you tried the system(@list) syntax? It doesn't involve the shell, so you don't need to escape the file names.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019209]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-06-21 17:00 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (99 votes). Check out past polls.