passing data

I can pass data to a subroutine but how do I pass data to the whole script
lets say I want to pass the number 2 to a whatnumber.pl script

whatnumber.pl 2

how would I access the argument 2 in the script

thanks
motown [ Sa, 08 Januar 2005 05:36 ] [ ID #573844 ]

Re: passing data

"motown" <moton449 [at] yahoo.com> wrote in message
news:3496aeF49dh48U1 [at] individual.net...
>I can pass data to a subroutine but how do I pass data to the whole script
> lets say I want to pass the number 2 to a whatnumber.pl script
>
> whatnumber.pl 2
>
> how would I access the argument 2 in the script

my $param = shift;

or

my $param = $ARGV[0];
Tintin [ Sa, 08 Januar 2005 07:24 ] [ ID #574039 ]

Re: passing data

anywhere in the code?


"Tintin" <tintin [at] invalid.invalid> wrote in message
news:349ciaF456amvU1 [at] individual.net...
>
> "motown" <moton449 [at] yahoo.com> wrote in message
> news:3496aeF49dh48U1 [at] individual.net...
>>I can pass data to a subroutine but how do I pass data to the whole script
>> lets say I want to pass the number 2 to a whatnumber.pl script
>>
>> whatnumber.pl 2
>>
>> how would I access the argument 2 in the script
>
> my $param = shift;
>
> or
>
> my $param = $ARGV[0];
>
>
motown [ Sa, 08 Januar 2005 13:10 ] [ ID #574323 ]

Re: passing data

You can access your parameters through the [at] ARGV array. For example, if
you have this command line:

whatnumber.pl 2 5 9

$ARGV[0] is 2, $ARGV[1] is 5 and $ARGV[2] is 9. Got it? You can use
anywhere in your code.

Regards.

motown wrote:
> I can pass data to a subroutine but how do I pass data to the whole
script
> lets say I want to pass the number 2 to a whatnumber.pl script
>
> whatnumber.pl 2
>
> how would I access the argument 2 in the script
>
> thanks
alsantos [ Sa, 08 Januar 2005 15:46 ] [ ID #574324 ]

Re: passing data

Dear Motown -

>>>>> "motown" == motown <moton449 [at] yahoo.com> writes:

motown> I can pass data to a subroutine but how do I pass data to
motown> the whole script lets say I want to pass the number 2 to a
motown> whatnumber.pl script

motown> whatnumber.pl 2

use Getopts::simple;

- Billy

============================================================
William Goedicke goedicke [at] goedsole.com
Cell 617-510-7244 http://www.goedsole.com:8080
============================================================

Lest we forget:

To estimate how long a task will take come up with an off-the-cuff
estimate. Then multiply the quantity times 2 and up the unit of
measure by one. So if you estimate 2 hours say 4 days, if you
estimate 3 weeks say 6 months.

- The Frank Tortora Rule
William Goedicke [ Sa, 08 Januar 2005 23:38 ] [ ID #574703 ]

Re: passing data

"William Goedicke" <goedicke [at] goedsole.com> wrote in message
news:m33bxbwnpa.fsf [at] smtp.comcast.net...
> Dear Motown -
>
>>>>>> "motown" == motown <moton449 [at] yahoo.com> writes:
>
> motown> I can pass data to a subroutine but how do I pass data to
> motown> the whole script lets say I want to pass the number 2 to a
> motown> whatnumber.pl script
>
> motown> whatnumber.pl 2
>
> use Getopts::simple;

Now why on earth would you want to use Getopts to parse a single parameter?
Tintin [ So, 09 Januar 2005 04:40 ] [ ID #575048 ]

Re: passing data

Dear tintin -

>>>>> "tintin" == tintin <tintin [at] invalid.invalid> writes:

tintin> "William Goedicke" <goedicke [at] goedsole.com> wrote in

>> use Getopts::simple;

tintin> Now why on earth would you want to use Getopts to parse a
tintin> single parameter?

A fair question, one could certainly argue that it's overkill which I
assume is your point.

The reasons I would are:

o I like to find solutions to a broad class of problems and then
stick with them. That way I have a slim chance of remembering
the details (my memory stinks). Honestly that's why I use perl,
its broad applicability means I rarely have to pick up a
different, relatively unfamiliar tool.

o If I use Getopts::simple then when I realize I actually need to
do more than I originally thought it's easy to add the extra
functionality. Maybe you're better at anticipating all the
requirements than me in which case this wouldn't be that
helpful.

o Getopts::simple is really pretty easy to use. It's really not
much more code than accessing ARGV directly. Consider:

use Getopts::Std;
my %opts;
getopts('v:', \%opts);
my $val = $opts{'v'};

(Note: My earlier point about memory it's Getopt::Std not
Getopts::simple. :)

o I try to always use the base modules intended for any particular
situation. Over and over (usually in association with one of
the points above) I find that when I don't I later get bagged by
something I didn't think of. When I use the base modules things
just work.

- Billy

============================================================
William Goedicke goedicke [at] goedsole.com
Cell 617-510-7244 http://www.goedsole.com:8080
============================================================

Lest we forget:

use XML::Simple;
use DBI;
use LWP;
use Graph;

- William Goedicke
William Goedicke [ So, 09 Januar 2005 20:34 ] [ ID #575899 ]

Re: passing data

$ARGV[n]
this works thanks

<alsantos [at] gmail.com> wrote in message
news:1105195616.392457.233290 [at] c13g2000cwb.googlegroups.com.. .
> You can access your parameters through the [at] ARGV array. For example, if
> you have this command line:
>
> whatnumber.pl 2 5 9
>
> $ARGV[0] is 2, $ARGV[1] is 5 and $ARGV[2] is 9. Got it? You can use
> anywhere in your code.
>
> Regards.
>
> motown wrote:
>> I can pass data to a subroutine but how do I pass data to the whole
> script
>> lets say I want to pass the number 2 to a whatnumber.pl script
>>
>> whatnumber.pl 2
>>
>> how would I access the argument 2 in the script
>>
>> thanks
>
motown [ Mo, 10 Januar 2005 16:49 ] [ ID #577146 ]

Re: passing data

William Goedicke wrote:

> o Getopts::simple is really pretty easy to use. It's really not
> much more code than accessing ARGV directly.

I took your example, avoided touching ARGV directly, and
added a print statement to it like this:

use Getopt::Std; # Note: Getopt, not Getopts.
my %opts;
getopts('v:', \%opts);
my $val = $opts{'v'};
print "\$val = $val\n";

When I run it with a command line argument, exactly as motown specified,
it does not do anything with that argument.

perl whatever.pl 2
$val =

The newbie, who did not know about [at] ARGV, *still* has to access
[at] ARGV to get plain (non-option) data from the command line.

-Joe
Joe Smith [ Di, 11 Januar 2005 10:59 ] [ ID #578668 ]
Perl » alt.perl » passing data

Vorheriges Thema: $;
Nächstes Thema: Palm:PDB and arrays and hashes problem.