Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I read data passed to perl script

by dtharby (Acolyte)
on Jun 07, 2005 at 11:13 UTC ( #464233=perlquestion: print w/ replies, xml ) Need Help??
dtharby has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl so please forgive if this is basic perl 101. How do you collect the data that is passed to a script ?? Example:-
test.pl "Hello All How Are You"
What do I need to do to get the information in "" so that I can act on it ?? Any help or pointers would be appreciated. Thanks

Comment on How do I read data passed to perl script
Download Code
Re: How do I read data passed to perl script
by phenom (Chaplain) on Jun 07, 2005 at 11:18 UTC
    For some things, I use @ARGV, and for others, I use the Getopt::Std and Getopt::Long modules at cpan.org. It depends on what your needs are.
    #!/usr/bin/perl use strict; use warnings; if(@ARGV) { print "You entered @ARGV\n"; }
Re: How do I read data passed to perl script
by thcsoft (Monk) on Jun 07, 2005 at 11:21 UTC
    yepp, what phenom said, is correct. for further - and maybe more unix'ish solutions take a look at Getopt::Long

    language is a virus from outer space.
Re: How do I read data passed to perl script
by marto (Bishop) on Jun 07, 2005 at 11:21 UTC
    Hi

    If you are new to Perl I suggest reading some of the many fine Tutorials the site offers.
    This answer to this question, and may more can be found within these Tutorials :D

    Hope this helps,

    Cheers

    Martin
Re: How do I read data passed to perl script
by tlm (Prior) on Jun 07, 2005 at 11:55 UTC

    For the particular example you posted

    my $arg = shift @ARGV;
    is one way to do it. There are a few billion others.

    But, as you say, this is a very basic question, which makes me wonder, why are you trying to do this without learning the basics of Perl first? There are sources aplenty to do this. Trying to solve a programming problem by asking piecemeal questions like this one, without knowing the basics first, seems to me a crazy way to go about it. Are you under the gun to conjure up with some Perl code out of thin air?

    the lowliest monk

      why are you trying to do this without learning the basics of Perl first?

      How can he learn Perl without slinging some code? I've found, for myself, that I just cannot grab a book or whatever and "absorb" a language from scratch. I have to have some concrete problem to solve, and I learn the language as I go about solving it. Now, that means of course that if I don't need CGI in my solution that I'm not going to learn CGI, but that's what works best for me. After I have a fairly strong foundation in the language, then I can start branching out to learn things I'm not actually needing to use right away.

Re: How do I read data passed to perl script
by mikeraz (Friar) on Jun 07, 2005 at 12:36 UTC

    You're right. It is Perl 101. People have provided the answer already, so I'll indulge my curiosity. Why are you asking? Comparing Perl to other languages before deciding which one(s) to learn? Checking out how monks handle questions?

    If you decide you're going to learn Perl I highly recommend spending a weekend with Learning Perl. It will get you past the crawling stage and into walking with the language quickly. After working through that book you'll be able to follow on with your own explorations. A weekend, without too many distractions, is enough time to do every problem in the book. Well, provided you have some programming background. Your question indicates that you do have that.

    Be Appropriate && Follow Your Curiosity
      Indeed, my explanation is simple. I am under some pressure to produce 'a fabulous tool' using Perl. My programming experience has been in VBScript, REXX, JCL and some other lanuages. At the moment I have 2 books, the Tutorial page on Perl Monks open so I am trying to learn using a real world problem. I am sorry for the basic question but after scanning the relevent pages that I could find, it was easier to post a simple message and hope that others could point me in the right direction. Which the Monks have done. I have already test some parts to my solution like interogating MS SQL table for info.. Thanks for everyone's input... Danny
        BTW, that's my reply above this one.... just forgot to login !!!
Re: How do I read data passed to perl script
by rlucas (Scribe) on Jun 07, 2005 at 16:27 UTC
    I highly recommend getting in the habit of making your programs filters. Happily, Perl has a great way to do this:
    while (<>) { # do stuff on $_ line by line }
    This will either take direct STDIN (run the program, it sits there, you enter lines of text until you hit ctrl-D), or take a list of filenames on the command line and process their contents as STDIN. Because this idiom doesn't use any textual identifier (like ARGV or STDIN), and because it's a difficult concept for a newbie to describe or search for (or even to conceive of), it's often something people don't pick up on early. I'm going to go on a limb and counsel a novitate to "just do it," and adapt your intentions to fit this idiom.

    It's not exactly what you described -- the others' @ARGV solutions are for that -- but if you can alter your requirement slightly, writing programs as filters has a lot of benefits. See The Art of Unix Programming for why.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://464233]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (18)
As of 2014-12-22 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls