Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

customize string issue

by emilford (Friar)
on Jul 16, 2003 at 01:20 UTC ( [id://274652]=perlquestion: print w/replies, xml ) Need Help??

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

I have a variable that I want to allow users of my script to be able to customize. It's simply a variable that they can use to store a couple of sentences to be sent out in an email. I'm wondering what the best approach for doing this would be. I initially had it in a set of single quotes, but people would constantly email me saying the script wasn't working correctly and 9 times out of 10 it was because they included a single quote in the variable value without escaping it. I can't blame them if they just didn't know, but I need a work around for this. Is there a construct I could use to avoid any "illegal" characters someone could add to the variable value?

Replies are listed 'Best First'.
Re: customize string issue
by sauoq (Abbot) on Jul 16, 2003 at 02:16 UTC

    It's probably better to require them put their message in another file and then read that in with your script... but, if you are set on allowing them to edit the script, I suggest using a here document with single quotes:

    my $message =<<'END_OF_MESSAGE'; They can put pretty much whatever they want in here except for the string 'END_OF_MESSAGE' on a line by itself. END_OF_MESSAGE

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: customize string issue
by edan (Curate) on Jul 16, 2003 at 08:03 UTC

    I agree 100% with sauoq in that you should always try to separate user-configurable stuff from the script itself into a config file of some sort. But again, if you feel that it must be inside your script, in the spirit of TIMTOWTDI, another way would be:

    local $/ = undef; my $user_defined_string = <DATA>; # script here # USER: put your text after the __DATA__ token, but leave that line a +lone! __DATA__ blah blah blah and more of the same
    --
    3dan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found