#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2

    Append code creating file without dot in filename


    Hello,
    I tried out appending to a file using 2 methods. However, this method writes to the file data1txt though I specify data1.txt in my code.

    Method1 - creates file called data1txt
    Code:
    #!/usr/local/bin/perl
    
    open (FILE, '>>', data1.txt) or die ("could not open data1.txt");
    print FILE "Bob\n";
    close FILE;
    Method2 - works file, creates file called data2.txt
    Code:
    #!/usr/local/bin/perl
     open (MYFILE, '>>data2.txt');
     print MYFILE "Bob\n";
     close MYFILE;
    Please advise,
    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    I'm really sorry, would like to take back the question, but it's because in Method1, the filename is enclosed within quotes. Still weird output though, a compilation error would've been better.
  4. #3
  5. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    If you want compilation errors, you need to start your scripts with these lines:

    Code:
    #!/usr/local/bin/perl
    use strict;
    use warnings;
    And you really should want them. Compilation errors are a great help.

    The fact that errors aren't enabled by default is one of those weird legacy things about perl that people grumble about.
    There has been some movement (for example here) to have these things enabled by default in perl.

    Perhaps we'll see it someday, but for now just include both pragmas in your scripts.

    Comments on this post

    • Laurent_R agrees
    Last edited by keath; June 11th, 2013 at 08:20 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by keath
    And you really should want them. Compilation errors are a great help.
    Sure keath, shall keep that in mind and use it from now onwards.
    But I just become lazy at times because as I notice, when I use
    use strict; and use warnings; ,I have to declare each variable as my $value;

    But shall try and get into the habit of using use strict; and use warnings; .

    Thanks.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Originally Posted by IAMTubby
    Sure keath, shall keep that in mind and use it from now onwards.
    But I just become lazy at times because as I notice, when I use
    use strict; and use warnings; ,I have to declare each variable as my $value;
    But that is precisely what is great about the strict and warnings pragmas: they force you to declare variables, and that is a great help for preventing stupid bugs (such as typos in variable names). It will also force you to think about lexical scope and help you to make more efficient and more reliable programs.

    Originally Posted by IAMTubby
    But shall try and get into the habit of using use strict; and use warnings;
    Don't simply try, do it. Really. Do it for every single script whose line count is strictly greater than 1. It might take you a little bit of time at the beginning, but it will tell you about errors (variable name typos, just to name a stupid but common error) that would otherwise go unnoticed at compile time and may take several hours to debug.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by Laurent_R
    Don't simply try, do it. Really.
    Surely will do it

IMN logo majestic logo threadwatch logo seochat tools logo