Jump to content

confused with perl


RogueHart

Recommended Posts

this confuses me.

here is my perl code

$woo = $ARGV[0];
print $woo;

i know its simple. im just trying to get used to args.

now with perl i could easily do this

$input = <>;
print $input;

save that as test.pl

and type in test.pl in the command line and it would wait for my input then print it after i hit enter.

BUT the first code i posted with the arg wont work if i just type test.pl. i have to type "perl test.pl"

why?

Link to comment
Share on other sites

this confuses me.

here is my perl code

$woo = $ARGV[0];
print $woo;

i know its simple. im just trying to get used to args.

now with perl i could easily do this

$input = <>;
print $input;

save that as test.pl

and type in test.pl in the command line and it would wait for my input then print it after i hit enter.

BUT the first code i posted with the arg wont work if i just type test.pl. i have to type "perl test.pl"

why?

Did you have #!/bin/perl on the first line?

If not, that's probably why. bash is other wise trying to guess what language you wrote your script in. Your first script could have been written in any number of languages.

Link to comment
Share on other sites

Did you have #!/bin/perl on the first line?

If not, that's probably why. bash is other wise trying to guess what language you wrote your script in. Your first script could have been written in any number of languages.

does it make a difference that im using dos instead of bash?

Link to comment
Share on other sites

command prompt will be doing it based on file extension (the dumb way) instead of file contents.

thats what i thought. and i was wondering if there was a way around that. because for the most part im stuck with windows. i play with knoppix from time to time but i cant use the os as my main. too many windows dependant programs. games mostly.

Link to comment
Share on other sites

In Windows, you really should be calling it with "perl test.pl" anyway instead of letting Windows decide for you... in my opinion... why it works one way and fails the other, I have no idea.

If you need help understanding the code...

<STDIN> (abbreviated as: <> ) and $ARGV[] are used two completely different ways.

In example 2, you use <STDIN> to request input from outside the program (as in from the user). When the user hits enter the value is assigned to $input.

With example 1, you're using $ARGV[0] to assign $woo the value of the first argument value passed into the program from the command line.

Starting the program like this: perl test.pl zomg wtf bbq

will automagically assign this: $ARGV[0] = zomg, $ARGV[1] = wtf, $ARGV[2] = bbq

Starting the program like this: perl test.pl means that there's nothing after "test.pl" to assign to the $ARGV[] array... so $woo ends up empty as well.

Link to comment
Share on other sites

i did try using an argument when i ran the first example. both with the perl.exe and without. i was just wondering why it didnt work if perl.exe wasn't included.

thanks for the explanation though. the way you explained it was much clearer and easier to understand than from the books and tuts.

Link to comment
Share on other sites

I tested both of these and mine run by just calling "test1.pl" or "test2.pl" straight from the command line (I'm using 5.10.0).

Do you get an error running the ARGV test, or does it just quit back to the prompt? If it's not actually able to find perl.exe in your path, it should say:

'test.pl' is not recognized as an internal or external command, operable program or batch file.

If you just type test and not test.pl it will say much the same thing.

If there is no error and test.pl simply runs and exits back to the cmd prompt, then it probably IS really running. Add in a print statement like print ' --- This is how I know it's working --- '; to the end of the .pl file.

Good luck and have fun with PERL.

Link to comment
Share on other sites

I tested both of these and mine run by just calling "test1.pl" or "test2.pl" straight from the command line (I'm using 5.10.0).

Do you get an error running the ARGV test, or does it just quit back to the prompt? If it's not actually able to find perl.exe in your path, it should say:

'test.pl' is not recognized as an internal or external command, operable program or batch file.

If you just type test and not test.pl it will say much the same thing.

If there is no error and test.pl simply runs and exits back to the cmd prompt, then it probably IS really running. Add in a print statement like print ' --- This is how I know it's working --- '; to the end of the .pl file.

Good luck and have fun with PERL.

well i know its working. and i know its running. but the issue isn't weather or not its running. its weather or not it will accept arguments. which without perl.exe it won't

Link to comment
Share on other sites

well i know its working. and i know its running. but the issue isn't weather or not its running. its weather or not it will accept arguments. which without perl.exe it won't

Ah, now it's clear what you're saying... and I'm definitely not having that problem.

I found another forum where someone had the same problem you're having and they changed their PATH to get rid of C:\Perl\Site\Bin; and just leave in C:\Perl\Bin; and their problem went away.

To do that in XP, right-click on My Computer, go to Properties, Click the Advanced Tab, then Environment Variables, in the System Variables section, select PATH, hit Edit and then good luck finding it with the TINY dialog box M$ provides... but it's usually near the beginning.

Link to comment
Share on other sites

Ah, now it's clear what you're saying... and I'm definitely not having that problem.

I found another forum where someone had the same problem you're having and they changed their PATH to get rid of C:\Perl\Site\Bin; and just leave in C:\Perl\Bin; and their problem went away.

To do that in XP, right-click on My Computer, go to Properties, Click the Advanced Tab, then Environment Variables, in the System Variables section, select PATH, hit Edit and then good luck finding it with the TINY dialog box M$ provides... but it's usually near the beginning.

thanks :) ill try that when i'm using my main comp again. someone has me using theirs and updating it with stuff while their in vegas and since its about 1.5X as powerful as mine i decided to use it till they get back. giving mine a break from running constantly.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...