Re: Comments trouble
by fx (Pilgrim) on Dec 28, 2003 at 20:32 UTC
|
It would appear to be an end of line thing. When your perl is in its own file, a comment extends to the end of the line. You can quite happyily put a semi-colon in a comment and perl just treats it as a comment.
Now extend this to the command line...
This works as expected:
perl -e 'printf "hello\n"'
This too works, as the comment is at the end:
perl -e 'printf "hello\n"; ## Comment'
This will not print anything because although the statements are separated by a semi-colon, perl has no newline to tell it where the comment ends:
perl -e '## Comment; printf "hello\n"'
This is how I entered a comment on the command line and got it to work as expected:
perl -e '## Comment; [RETURN WAS PRESSED HERE]
printf "Hello\n"'
== fx, Infinity Is Colourless | [reply] [d/l] [select] |
Re: Comments trouble
by castaway (Parson) on Dec 28, 2003 at 19:44 UTC
|
Please give short examples of a script that runs, and a script that doesn't, together with how you are running the script (as executable, calling it as a parameter to the perl binary, somehow else? Using a self-build perl or one that came with the system?
C. | [reply] |
|
|
I am writing the code in BBEDIT, a text editor, and then running it from the command line as an argument to the perl executable. All of my scripts run fine if I don't have any comments before, or in, the code.
an example:
%sushi = (
inari => "tofu",
maguro => "tuna",
ika => "squid",
tamago => "omelet",
sake => "salmon");
@english = values(%sushi);
@japanese = keys(%sushi);
foreach (@english) {
($japanese,$english) = each(%sushi);
print ("$english => $japanese\n");
}
runs just fine, but if I add the line:
# script to swap keys and values in a hash
to the beginning, no go. The first thing I tried, thinking it was an end of line problem, was adding a semicolon to the end of my comment lines, but still no change. If I put that same comment line, no semicolon, at the end of the script, it runs just fine.
I am using Perl 5.6.0, which is what this machine came with.
| [reply] [d/l] |
|
|
Sorry, not much experience on MAC OSX, I cant see how this could be happening..
Just wanted to tell you you can reverse a hash quite easily using 'reverse' (since a hash in list context is just a list containing key,value,key,value etc.. so:
%reversedsushi = reverse(%sushi);
;)
C. | [reply] [d/l] |
|
|
|
|
BTW - when I posted the original query I was signed in, but it posted as anonymous. I'm actually CTP, not anonymous :)
| [reply] |
Re: Comments trouble
by waswas-fng (Curate) on Dec 29, 2003 at 03:45 UTC
|
first let me congratulate you on your choice of OS. =) On OS X 10.2 -> 10.3 perl is installed when you install the devel toolkit from apple or when you install it with fink. It is no longer MacPerl. You will notice that while you learn perl that it runs the same as any other unix version of perl. You may want to look into getting some great books such as the camel, the lama etc. these books will help you jump in and learn perl. As for your original posting you may want to show the code that is behaving poorly. A few tips I can pass your way off the top of my head is to make sure you save as a unix file in bbedit while editing perl scripts. also you may want to get in the habit of using strict diagnostics and warrnings on your code. I would bet they will point you to the right direction with this issue. | [reply] |
|
|
I do have the Camel book, and the Llama book, as well as several others. I had taken a couple courses a while back and bought a few, then got rusty, so now jumping back in.
I will try what you say about the BBedit saves...been using BBedit forever, but hadn't ever needed to do that so didn't even know about it.
I am reading ahead about diagnostics and 'strict' and such, and plan to write a script to ferret out the issue. I wrote here first just to make sure there wasn't one of those answers like, "Oh yea, in OSX you gotta have the comments bit set to one...sheesh!" Know what I mean? :-)
Many thanks!
| [reply] |
|
|
ARRRRRGGGHHHHH.
No, not pirate like...Charlie Brown like. Changed BBedit to save as UNIX files. No joy :(
| [reply] |
|
|
I think you need to do a little more than just save as Unix. You need to convert the line endings that are already there to the Unix line endings.
I haven't had BBEdit installed since I installed Panther (OS X 10.3) but I seem to remember this was pretty easy to do.
update: Check out Line Ending Converter for a conversation on line endings (as well as some scripts to convert files in place).
| [reply] |
Re: Comments trouble
by ysth (Canon) on Dec 28, 2003 at 22:22 UTC
|
I don't have any experience with Mac perl, but is it possible that your script has carriage returns between lines and the # comment is only ending at line feeds? | [reply] |
|
|
Just to note, this isn't MacPerl any more, it's just ... Perl running on MacOSX. OSX is UNIX-like.
And I don't think the original poster was asking about Perl programs written in the command line, only about programs run from the command line.
It might be a line-ending problem of another kind, but I second the "please provide examples" post above.
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
| [reply] [d/l] |
|
|
BEGIN { use Data::Dumper; $Data::Dumper::Useqq=1; open FOO, $0 or die
+"nope: $!"
; print Dumper $_ while <FOO>; exit }
at the beginning of the source and run it and show the output.
I don't remember if the Data::Dumper in 5.6.0 had the Useqq option, though.
| [reply] [d/l] |
Re: Comments trouble
by TomDLux (Vicar) on Dec 29, 2003 at 07:04 UTC
|
What happens when you run the program in the perl debugger?
Are you using strict and warnings?
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |
Re: Comments trouble
by sgifford (Prior) on Dec 29, 2003 at 08:47 UTC
|
#!/usr/bin/perl
A comment as the very first line of the script is sometimes used by your OS and/or by Perl to determine what interpreter to run. Normally it has to start with #!, but it's possible something weird is going on. And besides, starting off your script with a proper "shebang" line is good form. :-)
If that doesn't work, show us the output, inside <code> tags, of cat -e yourscript, or whatever the equivalent is on MacOS X. That should show any nonprinting characters, and mark the ends of lines.
| [reply] [d/l] [select] |
Re: Comments trouble
by jasky (Scribe) on Dec 29, 2003 at 11:46 UTC
|
I also am using perl on OS X and just did an experiment and was able to duplicate your problem.
If I saved the file in the old Mac format with ASCII CR's s as line separator, any comments in file would extend from the comment to EOF. But if I saved the file using ASCII LF's (\0x10) as line separator, everything worked as it should.
I believe you still have not succeeded in saving your file in unix format. Use hexdump to see exactly what line separator you have.
| [reply] |
|
|
Not (yet) knowing how to UPDATE a comment, I reply to my own. Correction: ASCII LF is \0x0a and ASCII CR is \0x0d.
| [reply] |
Re: Comments trouble
by nimdokk (Vicar) on Dec 29, 2003 at 21:56 UTC
|
Not to beat a dead horse or anything but I was able to recreate your problem using Mac OSX 10.2.8 using BBEdit_Lite 6.1. However, the same script when created with vi ran just fine. I opened up the one created in BBEdit with vi and saw the ^M characters had munged the whole works. Also, I turned on warnings and got the following message:
bash-2.05a$ comment.pl
use: bad interpreter: No such file or directoryerl
So the problem is the line endings. There are a couple of ways to clean it up. One (unfortunately not available with OSX version of *nix) is the "unix2dos" command. The other method would be to do some sort of Perl (or sed) script to clean up the control characters. I think if you switch \015 (control-M or Carriage Return) for say \012 (control-L or Line Feed), you should be ok. Something along the lines of say:
#!/usr/bin/perl
use warnings;
use strict;
my $in=$ARGV[0];
my $out=$ARGV[1];
open IN, "$in" or die "Can't do it man. $!";
open OUT, ">>$out" or die "Man, I can't do that. $!";
foreach (<IN>) {
s/\015/\012/g;
print OUT $_;
}
close IN;
close OUT;
exit;
I tried this out and it should do the trick. HTH
"Ex libris un peut de tout"
| [reply] [d/l] |
|
|
So as we all suspected it was an end of line issue. What confuses me tho is why the semicolon didn't tell the interpreter that that was the end of the line...every other line is "improperly" ended, but the interpreter doesn't seem to care. I guess not all semicolons are created equal? UPDATE - I just re-read some of the previous posts, and I guess a semicolon in a comment is not created equal as it is rendered irrelevant by being in a comment...makes sense.
What happens when you run the program in the perl debugger?
Don't know how to do that yet
Are you using strict and warnings?
Not yet, but will start. Tried warnings a couple times, but got none, so moved on. Had planned on using the shebang as well, but had to stop until I got this worked out. That's actually what tipped me off to the problem.
If that doesn't work, show us the output, inside <code> tags, of cat -e yourscript, or whatever the equivalent is on MacOS X. That should show any nonprinting...
I ran the cat -e, and it is in fact the ^M that seems to be in there munging it up.
However, the same script when created with vi ran just fine.
yea - I thought of this last night. I started doing my homework in BBedit just because it's a tool I am very used to for other work. I'll get out my little vi pocket ref and dig in...all the vi I ever knew will probably come back to me after a few scripts.
I do plan on trying to make it work the other way too tho. Many thanks to all who came to my rescue!
CTP
| [reply] |
|
|
As for the Perl debugger, perl -d <scriptname> should get you started. It's quite useful, I've gotten my non-Perl savvy co-workers into the habit of running it whenever they set up one of our Perl scripts. I just got into the habit of using vi because on one system, that was the only editor I had available, so I learned it (the other telling point was when the fstab on my Linux box got messed up and I had to reboot into standalone mode and edit the thing by hand and at that point, vi is all you've got :-). I played a bit with BBEdit and found that if you do a "Save As" and select "Simple Text" and "ASCII" under the options (or advanced options), the file will get saved the way you want it and run just right (discovered that after I posted the script to clean up the CR/LFs. Anyhow, good luck.
"Ex libris un peut de tout"
| [reply] [d/l] |