Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts

    No such file or directory at...

    Hi,
    Do you guys know why if I execute this simple script it reads all the linse from my file but when I run it though the DOS window it displays the error:
    No such file or directory at…

    Script
    Code:
    #!/usr/bin/perl
    my$file = 'SM_Error.log';
    
    open (IN, "$file") or die $!;
    
    while (<IN>) {
      print $_;
    }
    Thank you.
    Last edited by FishMonger; 04-17-2011 at 01:55 AM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,874
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Please you the code tags when you include code in your posts. I've edited your post to include those tags.

    First, you should ALWAYS include the following 2 pragmas in EVERY Perl script you write.
    Code:
    use strict;
    use warnings;
    Those 2 pragmas will point out lots of coding problems, some of which could be difficult to track down without their help.

    Do not use bareword filehandles and don't quote single vars. It's better to use lexical vars for filehandles and the 3 arg form of open.

    The die statement should include the filename and a brief description.

    It's better to use a named var instead of relying on the $_ default var in the while loop.

    Here's a better version of your test script which should help in finding the problem.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $file = 'SM_Error.log';
    
    open my $fh, '<', $file or die "failed to open <$file> for reading: $!";
    
    while ( my $line = <$fh> ) {
        print $line;
    }
    close $fh;

  • Users who have thanked FishMonger for this post:

    vl123456 (04-17-2011)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thanks for the reply!
    I hope I will learn a lot of useful tricks.
    I do not know much about Perl but I learned to avoid < use strict ; > or < use warnings;>
    It never works for me, it always produces some kind of errors even if a script work fine.
    I tried your version of the script and it still produces the same error (script still works fine)
    Error massage :
    Microsoft Windows [Version 6.1.7600]
    failed to open <SM_Error.log> for reading: No such file or directory at C:\PERL_
    TEST\1\OO.pl line 8.

    I just wanted to understand <why> I got these errors (the scripts still working ok).

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,874
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I do not know much about Perl but I learned to avoid < use strict ; > or < use warnings;>
    That is exactly opposite of what you should have learned. Ignoring the problems they point out will make your troubleshooting harder and the final code you end up using will (not might) be much worse.

    Is SM_Error.log in the same directory as the script and are you executing the script from that same directory, or are you in some other directory and specifying the path when executing the script?

  • Users who have thanked FishMonger for this post:

    vl123456 (04-22-2011)

  • #5
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by vl123456 View Post
    I do not know much about Perl but I learned to avoid < use strict ; > or < use warnings;>
    vl123456,

    I have to second FishMonger's comments here. Using strict and warnings is the #1 thing you can do to help your code improve. It's not the source of your issues here. However, I also will often stop helping someone in forums if they don't take this most basic step to have tighter and more robust code.

    Regards,
    - Miller

  • Users who have thanked miller for this post:

    vl123456 (04-22-2011)

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    You are right again. Then more I learn then more I understand how it works. Thanks your guys for helping me.
    vl123456


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •