prompt> perl $foo = "cheese"; $bar = "stuff/$foo.txt"; print "Bar is [$bar]\n"; ^D Bar is [stuff/cheese.txt]
As someone else mentioned. The difference is in the quoting. Single quotes don't interpret variables in the string. Double quotes do.
prompt> perl $foo = "cheese"; $bar = 'stuff/$foo.txt'; print "Bar is [$bar]\n"; ^D Bar is [stuff/$foo.txt]
As the same person mentioned, you need to be careful with the characters which follow the variable name (the .txt in the example above). If perl can tell that these aren't part of the variable name then all is well. If perl can't be sure where your variable name finishes then you need to write the variable as ${foo}.
$bar = "stuff/$foo1234"; # Bad $bar = "stuff/${foo}1234"; # Good
And the real reason I added this bit:

BE VERY CAREFUL WRITING CGI SCRIPTS! DON'T USE DATA FROM USERS WITHOUT CHECKING/SANITISING IT!

Sorry for shouting, but bad things can happen. Please read the 'perlsec' page from the perl documentation before writing CGI scripts which might be accessed by anyone apart from yourself and close friends :-)

Read up on the taint option (-T) which you can put on the #! line of your script (even on Windows...:-)

What if the user entered a filename of "../../../../../etc/passwd" (not much in most setups, but you get the idea of one way in which a malicious user can attack an insecure script).

There are lots of perl modules and pieces of advice on how to write good CGI scripts. Don't be scared off practicing with CGI but please don't put publically visible scripts up without decent security checking unless you don't mind losing your data and possibly everyone else's on the same server as you.

Have a nice day ;-)


In reply to RE: string combinations by jbert
in thread string combinations by parsifal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.