For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > January 2005 > passing data









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing data
motown

2005-01-08, 3:55 am

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




Tintin

2005-01-08, 3:55 am


"motown" <moton449@yahoo.com> 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];


motown

2005-01-08, 8:55 am

anywhere in the code?


"Tintin" <tintin@invalid.invalid> wrote in message
news:349ciaF456amvU1@individual.net...
>
> "motown" <moton449@yahoo.com> wrote in message
> news:3496aeF49dh48U1@individual.net...
>
> my $param = shift;
>
> or
>
> my $param = $ARGV[0];
>
>



alsantos@gmail.com

2005-01-08, 3:55 pm

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


William Goedicke

2005-01-08, 8:55 pm

Dear Motown -
[color=darkred]

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 ws say 6 months.

- The Frank Tortora Rule
Tintin

2005-01-09, 3:55 am


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


William Goedicke

2005-01-09, 3:55 pm

Dear tintin -
[color=darkred]

tintin> "William Goedicke" <goedicke@goedsole.com> wrote in
[color=darkred]

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
motown

2005-01-10, 3:57 pm

$ARGV[n]
this works thanks

<alsantos@gmail.com> 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:
> script
>



Joe Smith

2005-01-11, 8:55 am

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com