Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Escaping single quote in $string

by calebcall (Sexton)
on Feb 17, 2013 at 20:44 UTC ( [id://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?

Thanks

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 (Parson) 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.

    Thanks

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found