in reply to Contents of a file into a scalar

The concatenation operator in Perl is dot ("."), not plus ("+"). You're evaluating the slurped-in string in numeric context, and presumably it has a value that numerifies to zero. That should handle your first example.

Your second example works for me. And it seems that you are stating that your third and fourth examples do function as you expect. So it's really just the first that's a problem. Use the dot operator for concatenation.

Also in your first example you're not checking for open failure with an "or die $!" clause. That would be a good practice.


Dave

Replies are listed 'Best First'.
Re^2: Contents of a file into a scalar
by MarkofPerl (Initiate) on Aug 10, 2013 at 02:31 UTC

    So what you're suggesting is, that in the first example, everything is more or less correct except the line that says $string = read_line($QBF);, and should be replaced with $string .= read_line($QBF);?

    I'm aware of the "or die!" stuff, I'm just debugging right now, and I know the file is there.

    And the second poster (toolic) says I shouldn't bother with the '+' in-between data types when printing? (sorry, I come from the land of C++.)

      Incorrect. I'm not talking about $string = read_line($QBF);. That line is probably working fine, assuming the file opened correctly (which you can't be sure of, because you didn't use "or die $!;". Debugging time is the most important time to have error reporting in place.

      What I am talking about is this line: "print $string + "\n"; # doesn't print anything. (or prints "0")"

      That line should not be adding $string to \n. It should be either concatenating with the 'dot' operator, or passing a list, with the comma operator. In other words, either of these would work:

      print $string . "\n"; print $string, "\n";

      You are using the addition operator. The addition operator applies numeric context to your "$string". Perl will convert that to the numeric value of "0", unless the string happens to begin with a number, or consists entirely of a number. Then it does the same thing to "\n". You said that "print $string + "\n";" prints "0". I'm saying the reason it prints zero is because both $string and \n, in the numeric context applied by "+" morph themselves into values of zero. Zero plus zero is zero, and that's what gets printed.

      If you used either of the print statements I suggested, no such numification would occur, and assuming open worked ok, you would see the results you're after.

      In C++ you would often overload '+' to deal gracefully with various data types. Unfortunately, + is also overloaded in C++ to concatenate std::string objects. Perl reserves "+" for math, and '.' for concatenation.

      So in C++ you might say:

      cout << my_string + "\n"; // Concatenation with newline. cout << my_string << std::endl; // Stream insertion with standard lin +e ending. cout << my_string << "\n"; // Stream insertion with newline.

      In Perl, similar semantics are invoked with:

      print $string . "\n"; # Concatenation with newline. print $string, $/; # List of items to print, with standard +line ending. print $string, "\n"; # List of items to print, with newline. # Or more conveniently... print "$string\n"; # Interpolation of a variable and newlin +e.

      Dave

      And the second poster (toolic) says I shouldn't bother with the '+' in-between data types when printing?
      Correct. In Perl, + is used to add numbers (Additive Operators).