The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Perl script not running correctly (http://hintsforums.macworld.com/showthread.php?t=86102)

litespeed 02-17-2008 07:56 PM

Perl script not running correctly
 
Hi,
a friend had a perl script written for him, but he cannot get it to work in Leopard. Being only a little more mac savvy than he is I told him I would see if I could get it to run. I use the terminal to run the script and I don't get any errors, but nothing else happens either. Was hoping that somebody could be kind enough to point me in the right direction as to what the problem might be.

The code is as follows:
Code:

#!usr/bin/perl



for($x=0;$x<=500;$x++){print "Content-Type: text/html\n\n";



my $url = "http://media.myspace.com/services/media/mediahitcounter.ashx?i=MIGdBgorBgEEAYI3WAPYoIGOMIGLBgorBgEEAYI3WAMBoH0wewIDAgABAgJmAwICAMAECIH%2fybI3prlTBBCGjZWfM3HUnwwUAA997FvMBFARqytmu%2bmAgQcJrCSpYXUvtI8UmTld7pfXS9Y8JWvoxSWKbj8VPUZRScii2Jnoe71fp1EWwshGRapexehQREzrFfOUD0aQFr9CZJbhQEjeGQ%3d%3d";



use LWP::UserAgent;

use HTTP::Request::Common qw(GET POST);



my $agent = LWP::UserAgent->new;

my $req = GET $url, Referer => "(http://www.google.com/search?hl=en&q=this+should+work&btnG=Google+Search&meta=)";



print $agent->request($req)->as_string;}

Thanks,
Chris

hayne 02-17-2008 11:07 PM

The first thing I notice is that you omitted the first slash in the path to 'perl' - you have:
#!usr/bin/perl
when it should be:
#!/usr/bin/perl

See also the section on running scripts in this Unix FAQ
and check that you have the correct line-endings (Unix-style end-of-line characters)

litespeed 02-18-2008 02:58 PM

Quote:

Originally Posted by hayne (Post 452107)
The first thing I notice is that you omitted the first slash in the path to 'perl' - you have:
#!usr/bin/perl
when it should be:
#!/usr/bin/perl

See also the section on running scripts in this Unix FAQ
and check that you have the correct line-endings (Unix-style end-of-line characters)

Hi Hayne,
thanks for taking the time to respond!

I added the first slash and still no luck.

I then changed the line ending from
for($x=0;$x<=500;$x++){print "Content-Type: text/html\n\n";

to
for($x=0;$x<=500;$x++){print "Content-Type: text/html\r";

still didn't do anything.

Might you have any other suggestions?

Chris

hayne 02-18-2008 04:33 PM

I'm not prepared to debug the script for you.
Instead, I suggest that you start at the beginning and learn enough to be able to debug it yourself.
E.g. Start with the following small Perl script. Make sure that you can run it and it works - it should print "Hello" in the Terminal window.
Then work up from there.
Code:

#!/usr/bin/perl

print "Hello\n"


Mikey-San 02-18-2008 04:53 PM

I'm not a perl guy, but I noticed you didn't actually say what was wrong. Just that you "can't get it to work". Whenever you need help with code, you should always state the higher-level goal (what task you're trying to accomplish), the intention of the code, the expected result of the code, the actual result of the code, and the steps you took to use the code.

http://catb.org/~esr/faqs/smart-questions.html

Mikey-San 02-18-2008 04:56 PM

Also, is this script some sort of hit count increaser for some MySpace crap? If so, that's pretty sad.

Code:

my $url = "http://media.myspace.com/services/media/mediahitcounter.ashx?i=MIGdBgorBgEEAYI3WAPYoIGOMIGLBgorBgEEAYI3WAMBoH0wewIDAgABAgJmAwICAMAECIH%2fybI3prlTBBCGjZWfM3HUnwwUAA997FvMBFARqytmu%2bmAgQcJrCSpYXUvtI8UmTld7pfXS9Y8JWvoxSWKbj8VPUZRScii2Jnoe71fp1EWwshGRapexehQREzrFfOUD0aQFr9CZJbhQEjeGQ%3d%3d";
I mean, why else would you hit this link 500 times in a script?

litespeed 02-18-2008 05:36 PM

Quote:

Originally Posted by hayne (Post 452268)
I'm not prepared to debug the script for you.
Instead, I suggest that you start at the beginning and learn enough to be able to debug it yourself.
E.g. Start with the following small Perl script. Make sure that you can run it and it works - it should print "Hello" in the Terminal window.
Then work up from there.
Code:

#!/usr/bin/perl

print "Hello\n"


Thanks Hayne, I will give it a try.

litespeed 02-18-2008 05:46 PM

no luck with printing hello in the terminal. No error message either.

hayne 02-18-2008 06:02 PM

Read the section on running scripts in that FAQ I referred to above.

litespeed 02-18-2008 06:08 PM

Mikey-San
sorry, I am not a programmer so I can't tell if the code is a hit count increaser. All I know is he asked if I could get the code to run on my mac because he couldn't on his so that is what I am attempting to do.

litespeed 02-18-2008 06:47 PM

Hayne,
I read the section on running scripts. I made it executable, I checked that it was executable , ran the script and still nothing happened.

Below is what I typed in the terminal and the result:


Macintosh:~ applemac$ chmod +x ~/Scripts/hello.pl
Macintosh:~ applemac$ ls -l ~/Scripts/hello.pl
-rwxr-xr-x@ 1 applemac applemac 32 Feb 18 14:40 /Users/applemac/Scripts/hello.pl
Macintosh:~ applemac$ ~/Scripts/hello.pl
Macintosh:~ applemac$

dmacks 02-18-2008 07:19 PM

Being helpful to friends by running code you don't understand, even after admitting you aren't going to be able to help them make it work (since you don't know what it's supposed to do, etc etc) is a pretty risky way to learn. Hayne is pretty wise to suggest learning how to make the world's simplest script work first before trying to fix other's code.

Example: If I had a slightly evil streak, I'd ask my friends to try a script for me, and that script would give me root access to their machine, including complete control over downloading illegal files and randomly changing the contents of various documents the user had saved.

litespeed 02-18-2008 07:34 PM

dmacks,
understood. I am not trying to be nefarious here and obviously I can't help him with his code. But now I am just bugged because I can't figure out how to run the simple code that Hayne gave me, even after reading the section on running scripts. It's a bit frustrating, but I don't give up easily.

Mikey-San 02-18-2008 07:44 PM

Perhaps he should post here, then, so everyone can stop playing the telephone game.

trevor 02-18-2008 07:51 PM

What do you get from the following commands:

which perl
perl -v


Trevor

litespeed 02-18-2008 07:54 PM

trevor,
Macintosh:~ applemac$ perl -v

This is perl, v5.8.8 built for darwin-thread-multi-2level
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

litespeed 02-18-2008 07:56 PM

and which perl

Macintosh:~ applemac$ which perl
/usr/bin/perl

trevor 02-18-2008 08:04 PM

What text editor are you using for your script?

Trevor

litespeed 02-18-2008 08:07 PM

trevor,
I am using TextEdit

hayne 02-19-2008 12:31 AM

Quote:

Originally Posted by litespeed (Post 452311)
Hayne,
I read the section on running scripts. I made it executable, I checked that it was executable , ran the script and still nothing happened.

Did you check that the line-endings are correct (Unix-style)? That is explained in the above Unix FAQ. And I suggested that you check that way back in post #2.

ashevin 02-19-2008 12:06 PM

Quote:

Originally Posted by hayne (Post 452389)
Did you check that the line-endings are correct (Unix-style)? That is explained in the above Unix FAQ. And I suggested that you check that way back in post #2.

Just an FYI: Perl doesn't care about the line-endings in source files. Perl on any platform will happily and correctly parse source files with line-endings from any platform it supports*.

* If there's some weird platform out there that uses some exotic line-ending scheme, Perl probably won't understand it unless Perl has already been ported to it. This is not the case for Unix (Mac OS X) and Windows.

litespeed 02-19-2008 07:22 PM

Now I am confused. Do I need to worry about line endings in this script or not?

#!/usr/bin/perl

print "Hello\n"

hayne 02-19-2008 07:30 PM

Quote:

Originally Posted by ashevin (Post 452474)
Just an FYI: Perl doesn't care about the line-endings in source files. Perl on any platform will happily and correctly parse source files with line-endings from any platform it supports*.

That may well be true if you invoke 'perl' and then give it a file containing the script. But if you use the usual "shebang" method to invoke 'perl' from a script file, then it definitely matters what line-endings you use. I.e. with the wrong line endings, it is as if the whole script is on the "shebang" line. Try it!

ashevin 02-19-2008 07:32 PM

Quote:

Originally Posted by litespeed (Post 452591)
Now I am confused. Do I need to worry about line endings in this script or not?

#!/usr/bin/perl

print "Hello\n"

You shouldn't have to.

Another thing to try would be the following:

Code:

perl -e 'print "hello\n"'
It's functionally the same as the test script, but without the script. If this works, there's something particular to reading scripts. If it doesn't, the Perl installation is just plain broken.

hayne 02-19-2008 07:33 PM

Quote:

Originally Posted by litespeed (Post 452591)
Now I am confused. Do I need to worry about line endings in this script or not?

#!/usr/bin/perl

print "Hello\n"

Yes, you do.
You always need to have Unix-style line-endings in any script files you want to run as if they are executables - i.e. by just typing the name of the script file.
Just make things easier on yourself by running the one-line Perl script I supply in the Unix FAQ and you will be sure that your script file has the correct line-endings.

ashevin 02-19-2008 07:47 PM

Quote:

Originally Posted by hayne (Post 452594)
Yes, you do.
You always need to have Unix-style line-endings in any script files you want to run as if they are executables - i.e. by just typing the name of the script file.
Just make things easier on yourself by running the one-line Perl script I supply in the Unix FAQ and you will be sure that your script file has the correct line-endings.

To be more clear, Perl doesn't care, but the shell does. I created the test script in Windows, using VMware Fusion.

Here's the shell session I tested with:

Code:

Last login: Sun Feb 17 05:33:58 on ttys000
avi-shevins-computer:~ avi$ cd Desktop/
avi-shevins-computer:Desktop avi$ ./test.pl
-bash: ./test.pl: /usr/bin/perl^M: bad interpreter: No such file or directory
avi-shevins-computer:Desktop avi$ perl ./test.pl
hello
avi-shevins-computer:Desktop avi$

Running the script straight from the command-line failed, because the shell treated the DOS end-of-line character as part of the interpreter name.

When I ran perl manually, passing it the name of the script, Perl parsed and ran it correctly.

hayne 02-19-2008 07:50 PM

Quote:

Originally Posted by ashevin (Post 452597)
To be more clear, Perl doesn't care, but the shell does.

Sorry to be pedantic, but it's not the shell - it's the OS's underlying exec mechanism. See references in the Wikipedia article I linked to above re "shebang".

ashevin 02-19-2008 08:32 PM

Quote:

Originally Posted by hayne (Post 452598)
Sorry to be pedantic, but it's not the shell - it's the OS's underlying exec mechanism. See references in the Wikipedia article I linked to above re "shebang".

Technically, we're both right.

Because the exec function can't find the interpreter listed, it passes it off to the shell to try. The error message I posted is from bash, not the kernel.

To illustrate:

Code:

avi-shevins-computer:~ avi$ cat moo
#/usr/bin/cow

echo "This proves the shell is trying funny stuff!"
avi-shevins-computer:~ avi$ ls -l /usr/bin/cow
ls: /usr/bin/cow: No such file or directory
avi-shevins-computer:~ avi$ ./moo
This proves the shell is trying funny stuff!
avi-shevins-computer:~ avi$

The test script is named moo. All it does is echo a line, using the interpreter cow, which doesn't exist. However, the script still runs, using the current* shell as the interpreter.

It can also be shown that the exec function does not treat a carriage-return (the first character DOS uses to indicate end-of-line) as a whitespace character. Here is the same script I posted above, but with a parameter added to the interpreter invocation (-w turns on warning mode for Perl):

Code:

avi-shevins-computer:~ avi$ cat test.pl
#!/usr/bin/perl -w

print "hello\n"
avi-shevins-computer:~ avi$ ./test.pl
hello
avi-shevins-computer:~ avi$

* Or it uses the $SHELL environment variable or the user's default shell. I am not sure.

hayne 02-19-2008 08:37 PM

Quote:

Originally Posted by ashevin (Post 452605)
Code:

avi-shevins-computer:~ avi$ cat moo
#/usr/bin/cow

echo "This proves the shell is trying funny stuff!"
avi-shevins-computer:~ avi$ ls -l /usr/bin/cow
ls: /usr/bin/cow: No such file or directory
avi-shevins-computer:~ avi$ ./moo
This proves the shell is trying funny stuff!
avi-shevins-computer:~ avi$

The test script is named moo. All it does is echo a line, using the interpreter cow, which doesn't exist. However, the script still runs, using the current* shell as the interpreter.

Umm, you forgot the "!" in the shebang line, so it's just treated as a comment. Nothing funny there.

ashevin 02-19-2008 08:41 PM

Quote:

Originally Posted by hayne (Post 452607)
Umm, you forgot the "!" in the shebang line, so it's just treated as a comment. Nothing funny there.

Oops! Good catch! :o

hayne 02-19-2008 08:44 PM

(Offtopic)
On the subject of funny stuff, see post #17 of this old thread: http://forums.macosxhints.com/showthread.php?t=12881

Mikey-San 02-19-2008 09:03 PM

Quote:

Originally Posted by hayne (Post 452611)
(Offtopic)
On the subject of funny stuff, see post #17 of this old thread: http://forums.macosxhints.com/showthread.php?t=12881

I miss mT around here. We used to talk regularly after he left the forums, but I haven't seen him in ages.

navaho 02-19-2008 09:07 PM

What's wrong is that it was posted 07/01 and not 04/01, Hayne. :p

It also occurs to me that the original script doesn't necessarily produce output.

Make the last line

print "done";

then run it.

Thinking I should have been more descriptive in where I was going.

"I use the terminal to run the script and I don't get any errors, but nothing else happens either"

HTTP::Request::Common should echo back an error if it can't get a dns entry for the site. it should echo back if it does and connect properly. but what if it doesn't connect at all? As in gets a host name but times out trying to connect?

What if the script is doing something, but you have it in a condition where there is no output?

Stick some print statements in and see where it dies?


By the way, your script works exactly as posted (with the path to perl correction noted above) on my leopard computer, exactly as it should.

ashevin 02-21-2008 10:31 AM

Quote:

Originally Posted by hayne (Post 452611)
(Offtopic)
On the subject of funny stuff, see post #17 of this old thread: http://forums.macosxhints.com/showthread.php?t=12881

You. Are. Cruel.

:D


All times are GMT -5. The time now is 10:17 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.