Re: Seeding variables from command line

2005-08-09 Thread Jeff 'japhy' Pinyan

On Aug 9, [EMAIL PROTECTED] said:


my $pwd = $1;
my $seed = $2;


That's shell syntax, Derrick.  Perl's command-line arguments are stored in 
@ARGV.


  my ($pwd, $seed) = @ARGV;

or

  my $pwd = $ARGV[0];
  my $seed = $ARGV[1];


my $key = substr(crypt($pwd,$seed),2);


Those quotes around $pwd and $seed are unnecessary.

--
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Seeding variables from command line

2005-08-09 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
 Sorry to bother everyone, but i was working on this yesterday and i
 couldn't get it to work.  I guess i have the wrong syntax for passing
 variables in from the command line.
 
 Here's my script:
 
 = crypt.pl =
 #!/usr/bin/perl
 my $pwd = $1;
 my $seed = $2;
 my $key = substr(crypt($pwd,$seed),2);
 print $key;
 =
 
 For example, I want to type:
 
  crypt.pl  string1 string2
 
  and it should spit out the value of $key.
 
 Right now both variables are returning null.  Any suggestions?

You must be a shell programmer :~)

The command-line arguments in Perl are in the global @ARGV array. You can
access them directly, as in:

   my $pwd = $ARGV[0]; # first argument
   my $seed = $ARGV[1];# second argument

Or, you can shift them off the array:

   my $pwd = shift @ARGV;
   my $seed = shift @ARGV;

Since @ARGV is the default target for the shift() function when used outside
a function, you can use the idiom:

   my $pwd = shift;
   my $seed = shift;

Finally, you can assign them as a list:

   my ($pwd, $seed) = @ARGV;

HTH

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Seeding variables from command line

2005-08-09 Thread Ryan Frantz
 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, August 09, 2005 10:39 AM
 To: beginners@perl.org
 Subject: Seeding variables from command line
 
 Sorry to bother everyone, but i was working on this yesterday and i
 couldn't get it to work.  I guess i have the wrong syntax for passing
 variables in from the command line.
 
 Here's my script:
 
 = crypt.pl =
 #!/usr/bin/perl
 my $pwd = $1;
 my $seed = $2;
 my $key = substr(crypt($pwd,$seed),2);
 print $key;
 =

This is my first attempt at helping out (newbie myself) but I figure
I'll learn more if I can help provide answers.  Here goes...

First, this is Perl, not a shell; Perl uses @ARGV for command line
arguments.  $1 and $2 are not instantiated with the values of your
arguments.

Very simply, you could write the following:

#!/usr/bin/perl

use warnings;
use strict;

my $pwd = $ARGV[0];
my $seed = $ARGV[1];
my $key = substr(crypt($pwd,$seed),2);
print $key\n;

I'd suggest checking the number of elements in @ARGV first, though, to
be sure you have the proper number of arguments.

ry


 
 For example, I want to type:
 
  crypt.pl  string1 string2
 
  and it should spit out the value of $key.
 
 Right now both variables are returning null.  Any suggestions?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Seeding variables from command line

2005-08-09 Thread Derrick_Ballentine
Thank you all, worked like a charm.

Old shell habits are hard to break ;-)

Derrick Ballentine
Automation Support Specialist
District Court - Western Arkansas