passing data

passing data

am 08.01.2005 05:36:09 von motown

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

Re: passing data

am 08.01.2005 07:24:51 von Tintin

"motown" wrote in message
news:3496aeF49dh48U1@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];

Re: passing data

am 08.01.2005 13:10:43 von motown

anywhere in the code?


"Tintin" wrote in message
news:349ciaF456amvU1@individual.net...
>
> "motown" wrote in message
> news:3496aeF49dh48U1@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];
>
>

Re: passing data

am 08.01.2005 15:46:56 von alsantos

You can access your parameters through the @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

Re: passing data

am 08.01.2005 23:38:57 von William Goedicke

Dear Motown -

>>>>> "motown" == motown 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@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

Re: passing data

am 09.01.2005 04:40:07 von Tintin

"William Goedicke" wrote in message
news:m33bxbwnpa.fsf@smtp.comcast.net...
> Dear Motown -
>
>>>>>> "motown" == motown 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?

Re: passing data

am 09.01.2005 20:34:47 von William Goedicke

Dear tintin -

>>>>> "tintin" == tintin writes:

tintin> "William Goedicke" 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@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

Re: passing data

am 10.01.2005 16:49:15 von motown

$ARGV[n]
this works thanks

wrote in message
news:1105195616.392457.233290@c13g2000cwb.googlegroups.com.. .
> You can access your parameters through the @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
>

Re: passing data

am 11.01.2005 10:59:07 von Joe Smith

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 @ARGV, *still* has to access
@ARGV to get plain (non-option) data from the command line.

-Joe