Re: Fw: yet another question

2009-10-07 Thread David Christensen

Slick wrote:
Got another question that I have been trying to wrap my mind around.  About @argv.  Now it says from I understand and read it access information from the command prompt.  How exactly does it work? Is it it's own database?  Or am I mistake, I know this may be a crazy question to ask.  


When you start a program, either via a system() call or via a shell, you 
can provide arguments to that program.  For example, on Windows XP:


C:\Documents and Settings\dpchristecho one two three
one two three

When I typed the first line (echo one two three) and pressed the 
Enter key, Command Prompt parsed my input, decided that echo was a 
command, decided that one, two, and three were arguments, and 
invoked a system() API call to start the requested program with the 
provided arguments.  Inside echo, the arguments were made available 
according to the conventions of the programming language that echo was 
written in.  The program was free to use the arguments as desired.  In 
this case, echo output the arguments to the console as the second line 
(one two three).



Inside a Perl script, arguments are made available as the contents of 
the @ARGV array.  Your script is free to read the size and contents of 
the @ARGV array using all the capabilities of the Perl programming 
language, but attempting to change the size and/ or contents of @ARGV 
will cause problems; @ARGV should be treated as read-only.



(The above explanations are simplified for clarity.)


HTH,

David

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Fw: yet another question

2009-10-06 Thread Slick
Thanks David.

Got another question that I have been trying to wrap my mind around.  About 
@argv.  Now it says from I understand and read it access information from the 
command prompt.  How exactly does it work? Is it it's own database?  Or am I 
mistake, I know this may be a crazy question to ask.  

 Jason H. Owens



- Forwarded Message 
From: David Christensen dpchr...@holgerdanske.com
To: Slick jho251...@yahoo.com
Cc: beginners@perl.org
Sent: Tue, October 6, 2009 11:01:08 PM
Subject: Re: yet another question

Slick wrote:
 What do you guys do to practice? Do you practice one script over and over 
 again? Do you read differnt scripts and figure out what is happening in each 
 one? Do you think of ideas to do things, then make the script for that? 

As they say:

Necessity is the mother of invention.

I write Perl scripts because I need to get something done with my computers.


Monotonous, repetitive tasks are good candidates.  For example, copying file 
trees to a backup drive, making a tar / gzip archive of some files with a date/ 
time stamp in the archive name, and shutting down a network of computers in the 
proper order.  In fact, I've written and re-written those scripts dozens of 
times (as my needs change/ expectations increase and as I learn more about 
Perl).  I then write one script to rule them all, so at the end of the day I 
can launch one script, turn off the monitor, and go to bed.  :-)


The steps are:

1.  Get an idea -- e.g. Hey, I bet I could write a Perl script to do this... 
(need).

2.  Research in exact detail what it takes to make this happen -- e.g. data, 
algorithms, sequence of steps, command syntax, reference information, etc. 
(analysis).

3.  Figure out how to accomplish this in Perl (design and implementation):

a.  One approach is to break down the overall process into smaller steps 
and solve them one at a time (procedural or structured programming).  This is 
the traditional approach, and how I learned to program.

b.  Another approach is to model the system as objects that contain both 
data and the operations that act upon that data (object-oriented programming).  
This is a newer approach, and makes it easier to solve larger and more complex 
problems.


I prefer books for learning and for reference, and found Learning Perl, 
Programming Perl, and especially The Perl Cookbook to be invaluable when it 
came time to start writing Perl code.  I still refer to the later two often.  
When you're ready for libraries (modules), object-oriented Perl, automated 
testing, etc., get Intermediate Perl.


Another aspect to #3 is learning to leverage all the free Perl scripts and 
modules that are available on the Comprehensive Perl Archive Network:

http://www.cpan.org/

Data::Dumper is one such module.  It is included in the base Perl distribution, 
so you don't have to worry about downloading/ installing it.  I find it 
invaluable for debugging (the old-fashioned way, with print statements):

2009-10-06 20:56:25 dpchr...@p43400e ~
$ cat foo
#! /usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;

my $debug = 1;

print join( , __FILE__, __LINE__,
   Data::Dumper-Dump([...@argv], [qw(*ARGV)])
) if $debug;

2009-10-06 20:56:29 dpchr...@p43400e ~
$ ./foo -x -y 'hello, world!' 123
./foo 8 @ARGV = (
  '-x',
  '-y',
  'hello, world!',
  '123'
);


HTH,

David


-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


  

Re: Fw: yet another question

2009-10-06 Thread Jim Gibson

At 9:10 PM -0700 10/6/09, Slick wrote:

Thanks David.

Got another question that I have been trying to wrap my mind around. 
About @argv.  Now it says from I understand and read it access 
information from the command prompt.  How exactly does it work? Is 
it it's own database?  Or am I mistake, I know this may be a crazy 
question to ask.


The @ARGV array (case matters) contains the command-line arguments 
with which the program was called. For example, if you have a Perl 
program myprog.pl and execute it this way from a command prompt:


perl myprog.pl command line arguments

then the @ARGV array contains three elements, just as if you had the line

@ARGV = ( 'command', 'line', 'arguments' );

at the beginning of your program. There is no database involved. 
The @ARGV array is a normal, global array in the main package and can 
be accessed in your program like any other array.


You can read about @ARGV and many other special Perl variables in the 
'perlvar' section of the built-in Perl documentation, available from 
a shell with


perldoc perlvar


--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/