CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
can't # out code or the script fails to run

 



lukdk
Novice

Jun 19, 2012, 4:05 AM

Post #1 of 9 (1146 views)
can't # out code or the script fails to run Can't Post

Hello,

I was wondering how it is possible adding a #-sign can break this script?

working code:
foreach my $computer (MOlib::read_file("input.txt")){
$computer=MOlib::trim($computer);
}

Not working code:
foreach my $computer (MOlib::read_file("input.txt")){
$computer=MOlib::trim($computer);
#
}

This is the error i get:
Missing right curly or square bracket at test.pl line 1, at end of line
syntax error at test.pl line 1, at EOF
Execution of test.pl aborted due to compilation errors.

This is the code for the MOlib:
sub read_file{
my $read_file=shift;
open (FILE, "<$read_file") or die "Can't open $read_file: $!\n";
my @file = <FILE>;
close FILE;
return @file;
}
sub trim{
my $str=shift;
return "" if !defined $str;
$str=~s/^\s+//;
$str=~s/\s+$//;
return ($str);
}


(This post was edited by lukdk on Jun 19, 2012, 7:16 AM)


FishMonger
Veteran / Moderator

Jun 19, 2012, 6:31 AM

Post #2 of 9 (1126 views)
Re: [lukdk] can't quote out code or the script fails to run [In reply to] Can't Post

You didn't add a quote character, you added a pound/hash character which in Perl is the start of a comment.

I suspect that you rewrote your code rather than doing a copy/paste when posing your question. In your actual code is the curly brace on the same line after the # character?


FishMonger
Veteran / Moderator

Jun 19, 2012, 6:34 AM

Post #3 of 9 (1125 views)
Re: [lukdk] can't quote out code or the script fails to run [In reply to] Can't Post

Don't reinvent the wheel. Use the File::Slurp module instead of your inefficient read_file() sub.


lukdk
Novice

Jun 19, 2012, 7:14 AM

Post #4 of 9 (1120 views)
Re: [FishMonger] can't quote out code or the script fails to run [In reply to] Can't Post

Hello,

thank you for your reply. It is indeed a #sign I was talking about and not a quote. I've edited my post.

The code is not rewritten but copy pasted.

The curly brace is indeed in both examples, and that's exactly why i don't understand it.


There might indeed be better already existing ways to write this, but without trial and error it's not educative.


(This post was edited by lukdk on Jun 19, 2012, 7:17 AM)


rovf
Veteran

Jun 19, 2012, 7:20 AM

Post #5 of 9 (1116 views)
Re: [lukdk] can't quote out code or the script fails to run [In reply to] Can't Post

Have a look at your source file using a hex editor. Are the line endings correct and consistent? Any unwanted, unprintable characters inbetween?


lukdk
Novice

Jun 19, 2012, 7:41 AM

Post #6 of 9 (1114 views)
Re: [rovf] can't quote out code or the script fails to run [In reply to] Can't Post

I've made the code as small as possible, but still have the same issue. when i remove the # sign, i get printed:
foo1
foo2


Code
package MOlib; 

use strict;
use warnings;

sub read_file{
return ("foo1","foo2");
}
1;



Code
use strict; 
use warnings;
use MOlib;

foreach my $computer (MOlib::read_file("input.txt")){
print "$computer\n";
#
}



FishMonger
Veteran / Moderator

Jun 19, 2012, 8:01 AM

Post #7 of 9 (1111 views)
Re: [lukdk] can't quote out code or the script fails to run [In reply to] Can't Post

Works correctly for me.


Code
c:\testing>type MOlib.pm 
package MOlib;

use strict;
use warnings;

sub read_file{
return ("foo1","foo2");
}
1;

c:\testing>type lukdk.pl
use strict;
use warnings;
use MOlib;

foreach my $computer (MOlib::read_file("input.txt")){
print "$computer\n";
#
}

c:\testing>lukdk.pl
foo1
foo2



lukdk
Novice

Jun 19, 2012, 8:13 AM

Post #8 of 9 (1109 views)
Re: [FishMonger] can't quote out code or the script fails to run [In reply to] Can't Post

Hello,

Both previous answers helped me. It's a problem with the file itself. Not the code.

When i tried "type test.pl" i didn't get the same output as you fishmonger. All was written on one line as if there was no "enter" used.

I always use notepad++ to write these things, I've checked my previous scripts, but they didn't have this issue.
In notepad++ the difference is that normally at the end of the line i use a "CR LF", in my not working script only a "CR" was used. (in human language: a different type of go to the next line)


rovf
Veteran

Jun 20, 2012, 1:18 AM

Post #9 of 9 (1099 views)
Re: [lukdk] can't quote out code or the script fails to run [In reply to] Can't Post

CR would be (old) Mac-style line ending. Not something which is used frequently nowadays (where even on the Mac, LF is recognized).

BTW, unless you have good reason to do otherwise, I think it is best to use LF only as line terminator. The vast majority of text editors - even on Windows - are happy with it, and Perl accepts it too.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives