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

I am passing a variable in my link called, course_no=243. This variable will change depending on the link selected. In the script, I want to open a couple of files corresponding to the course number. For example, if course_no=243 then I want to open 243desc.txt and 243links.txt. If the link for 244 were selected, then I would open the corresponding files.

In my script, I'm opening the files using the following code:

open (DESCDOC, "/usr/local/apache2/htdocs/crs_desc/$course_no . desc\. +txt") || &problem; my @descdoc = <DESCDOC>; close DESCDOC;

As you can see, I want to append to the course number the rest of the file name so it will access the appropriate file. I can't seem to get the proper syntax to open the file. I've tried numerous combinations of concantenations but nothing works. The problem sub just displays a message to let me know the file didn't open. How should I write the open command?
Thanks.
Chris

Replies are listed 'Best First'.
Re: Concantenation
by ajt (Prior) on May 05, 2003 at 17:53 UTC

    chriso

    You don't show the whole code, but on the face of it what you show isn't going to work, and worse still it may be very dangerous.

    I hope you have properly de-tainted and verified that the value of $course_no is nice and safe, otherwise all sorts of undesirable things may happen!!!

    Once you are happy that things are safe, then the following may work better with your concatination:

    my $file = "/usr/local/apache2/htdocs/crs_desc/" . $course_no . "desc. +txt"; open DESCDOC, "<", $file || die "Unable to open $file";

    Your concatination is a bit messy, which I clearly spelt out, so it should be okay now. Other solutions would work too. I've change your fatal to report which file it couldn't open, in case the name isn't what you expected - your error handling won't take care of that unless you pass the filename to it.

    Others will have more advice, but also see (from an earlier node of mine):


    --
    ajt
      The other alternative to spelling out the concatenation is to use Perl's provisions for disambiguating the variable name (which is why the OP split up the concatenation):
      my $file = "/usr/local/apache2/htdocs/crs_desc/${course_no}desc.txt";

      Makeshifts last the longest.

Re: Concantenation
by jgallagher (Pilgrim) on May 05, 2003 at 17:41 UTC
    Since you're using double quotes (which are interpolated), you don't need concatenation at all. What you have produces a string that looks like this (if $course_no = 243):
    /usr/local/apache2/htdocs/crs_desc/243 . desc.txt

    To do this with concatenation, try:

    open (DESCDOC, "/usr/local/apache2/htdocs/crs_desc/$course_no" . "desc.txt")

    Or, to do this without concatentation, try:

    open (DESCDOC, "/usr/local/apache2/htdocs/crs_desc/${course_no}desc.txt")
Re: Concantenation
by artist (Parson) on May 05, 2003 at 17:40 UTC
    my $file= "/usr/local/apache2/htdocs/crs_desc/${course_no}desc.txt"; open(DESCDOC,$file)|| &problem; my @descdoc = <DESCDOC>; close DESCDOC;

    artist