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

I am working on this tutorial and the exercise asks us to use $" and add a # to the start of every line in a file. I used this code:
use strict; my $file = 'names.txt'; # Name the file open(INFO, "$file"); # Open for appending $" = "\#"; my @lines = <INFO>; # Read it into an array print INFO "@lines"; close(INFO); # Close the file open(INFO1, "$file"); # Open the file @lines = <INFO1>; # Read it into an array close(INFO1); # Close the file print @lines; # Print the array
And This is the output. the file isn't modifying.
D:\Perl_Samples>perl file_Modify.pl Alpha Beta Chi Delta Epsilon
Please Advise.

Replies are listed 'Best First'.
Re: Using $"
by toolic (Bishop) on Dec 22, 2014 at 19:35 UTC

    Tip #1 from the Basic debugging checklist: warnings

    Filehandle INFO opened only for input at ...

    diagnostics:

    Filehandle INFO opened only for input at ... (W io) You tried to write on a read-only filehandle. If you inten +ded it to be a read-write filehandle, you needed to open it with "+<" +or "+>" or "+>>" instead of with "<" or nothing. If you intended onl +y to write the file, use ">" or ">>". See perlfunc/open.
      I have tried to use "+>>" and ">>" to no avail.

        The second time you open the file, it needs to be opened for output.

        open(INFO1, ">", $file) or die "Couldn't open: $!\n";

        It looks like you are using $" correctly so your question should probably be something about reading and writing to files. There are many tutorials on this topic for Perl.

      I tried "+<" and noticed that all but the first line had the # appended to it. Also instead of replacing the line in the file, the values were added to the end
      D:\Perl_Samples>perl -w file_Modify.pl Alpha Beta Chi Delta EpsilonAlpha #Beta #Chi #Delta #Epsilon
Re: Using $"
by LanX (Saint) on Dec 22, 2014 at 20:09 UTC
    > I am working on this tutorial and the exercise asks us to use $"

    Which tutorial?

    See perlvar

    $"

    When an array or an array slice is interpolated into a double-quoted string or a similar context such as/.../ , its elements are separated by this value. Default is a space.

    There is no double quote interpolation in your code.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      There is double quote interpolation in the first print of the original post. I think this is what the author of the exercise expected. The only problem is that it would not edit the first line. That could be fixed with:
      $" = '#'; print "#@lines";
      The OP's I/O problems hide the fact that he had basically solved the exercise.
      Bill
      http://www.comp.leeds.ac.uk/Perl/filehandling.html
      #!/usr/local/bin/perl # # Program to open the password file, read it in, # print it, and close it again. $file = '/etc/passwd'; # Name the file open(INFO, $file); # Open the file @lines = <INFO>; # Read it into an array close(INFO); # Close the file print @lines; # Print the array

      Exercise
      Modify the above program so that the entire file is printed with a # symbol at the beginning of each line. You should only have to add one line and modify another. Use the $" variable.

        http://www.comp.leeds.ac.uk/Perl/
        Is Perl4 Archives! Perl5 is in 5.21 and Perl6 is almost here!
        I will advice, you use the alternative link, the webpage link to after some seconds.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
        Did you read the intros? :)

        Legacy Tutorials

        These are old Perl tutorials that are well written and good references for old versions, but should not be used by newcomers to learn Perl.

        ... and. ..

        Please note: This tutorial was written in the early 1990's for version 4 of Perl. Although it is now significantly out of date, it was a popular source of information for many people over many years. It has therefore been left on-line as part of the historical archive of the Internet.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        besides from being outdated (the others already said it), the task would have been to just print the modified data, not to rewrite the original file...
Re: Using $"
by GrandFather (Saint) on Dec 22, 2014 at 21:45 UTC

    I'd find a different tutorial because that one is just dumb. $" is used as a separator, but "the start of every" is not a job you do with a separator. A sane way to do "start of every line" is:

    print "#$_" while defined $_ = <>;

    with appropriate file handling as required. Speaking of which, always use the three parameter open and lexical file handles:

    open my $info, '<', $file or die "Can't open '$file': $!\n";

    and remember that in general you can't "in place edit" files - they just don't work that way. Instead you need to make an updated copy of the file then rename or remove the old version and rename the new file to the original file's name. Perl can help a lot with that using the $^I ($INPLACE_EDIT) special variable that allows you to set the file extension to be appended to the backup (original) version of the file so you can write code like:

    local @ARGV = $file; local $^I = '.bak'; print "#$_" while defined $_ = <>;

    Which will "inplace edit" $file and create a backup copy of the original file by appending '.bak' to the original file name. Note that you could process a batch of files using the same code simply by adding the file names to the list assigned to @ARGV.

    Perl is the programming world's equivalent of English
Re: Using $"
by Anonymous Monk on Dec 22, 2014 at 20:15 UTC
    Well that seems like a really ridiculous excersize.