Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Please help Perl Newbie understand this statement

Reply
Thread Tools

Please help Perl Newbie understand this statement

 
 
jm-1@remotekontrol.com
Guest
Posts: n/a
 
      06-26-2006
Hi

I have not programmed before and I am doing my best to get to grips
with Perl in order to decode and modify a backup script. Could anyone
please explain the following line in plain english to me? I understand
that what we are saying is "While the file handle <CONFIG> is
available, chomp the newline character of off each line as we read it"
and then I am lost. Sorry to sound so thick! I am working my through
the tutorials but I am under pressure to get this script working. I
have pasted the entire script at the bottom of this message.

while (<CONFIG>)
{
chomp;
if (m/^\w/)
{
($key, $data) = split /=/, $_;
$array{$key} = $data;
}
}

Thanks for any help

Andy

#!/usr/bin/perl

use Expect;
use IO::Socket;

#Declare the varibles

my ($hour, $min, $sec, $now, $remoteip, $port, $connected, $ping);
my (@ports, @lines, %status);

#Delete know hosts file for ssh
$command = "/bin/rm -f /root/.ssh/known_hosts";

system($command);

#Open config file.

##if (!(open(CONFIG, "pix_backup.cfg")))
#{
# die "Emergency: Error: Cannot open configuration file";
#}

#Read contents of config file into a hash

#while (<CONFIG>)
#{
#chomp;
# if (m/^\w/)
#{
# ($key, $data) = split /=/, $_;
# $array{$key} = $data;
#}
#}

#Initialise variables read from the config file

my @ports = ("22");

$outputdir = $array{outputdir};
$firewall = $array{firewall};
$username = $array{username};
$password = $array{password};

# Get name of host list file from command line

#GetOptions ('f=s' => \$config_file);

# Decrypt host config file.

$config_file = "/var/scripts/host.lst";
#$ccrypt_key = "*************************";

#$command = "ccrypt -q -K $ccrypt_key -d $config_file.cpt";

#system($command);

#Open host config file.

if (!(open(HOSTS, $config_file)))
{
die "Emergency: Error: Cannot open host file";
}

# Read contents of config file into an array

@firewalls = <HOSTS>;

# EnCrypt host config file

#$command = "ccrypt -q -K $ccrypt_key -e $config_file";

#system($command);

# Loop through all the hosts in the config file to grab all the configs

for ($i=0; $i < @firewalls; $i++)
{

$string = $firewalls[$i];

chomp($string);

# Split out host to work on

@host = split /,/, $string;

# Set all the host variables

$Hostname = $host[0];
$HostIP = $host[1];
$UserName = $host[2];
$Password = $host[3];
$Enable_Password = $host[4];


$command = "perl /var/scripts/pix_get.pl -h $Hostname";

system($command);

##print $data;

} # End For

print "\n[FINISHED]\n\n"

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have not programmed before and I am doing my best to get to grips
> with Perl in order to decode and modify a backup script. Could anyone
> please explain the following line in plain english to me? I understand
> that what we are saying is "While the file handle <CONFIG> is
> available,


Nope. That line means "While the file that is referenced by the file handle
CONFIG has not reached EndOfFile (i.e. while there are still lines
available)" loop through each line in sequence."

> chomp the newline character of off each line as we read it"
> and then I am lost.


> while (<CONFIG>)
> {
> chomp;
> if (m/^\w/)


If the current line starts with a word character
"\w Match a "word" character (alphanumeric plus "_")"
then

> {
> ($key, $data) = split /=/, $_;


Split the current line at any equal sign "=", store the first part in $key,
store the second part in $data, ignore any other parts.

> $array{$key} = $data;


And store $data in the hash named %array at the data point $key. BTW: using
%array for a hash is somewhat misleading.

jue


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-26-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> I have not programmed before and I am doing my best to get to grips
> with Perl in order to decode and modify a backup script.



> Could anyone
> please explain the following line in plain english to me?



Another followup already did that, so I'll address other issues.


> I am working my through
> the tutorials



Which ones?

There are more bad Perl tutorials than good ones.

If you tell us which ones, then we can tell you whether they are
worth any study or not...


> have pasted the entire script at the bottom of this message.



It appears to have been written by someone who isn't well versed
in programmeing in general, nor in Perl in particular.

Modify it if you must, but don't be learning the bad habits
it displays.


> #!/usr/bin/perl



You should ask for all the (machine) help you can get:

use warnings;
use strict;

They will find many common programming mistakes for you.

Do read up on them:

perldoc warnings

perldoc strict


> #Declare the varibles



A good programmer would not repeat in comments what is already
said in the code. Comments are for things that cannot be easily
said in the code (such as "why" rather than "how").


> my ($hour, $min, $sec, $now, $remoteip, $port, $connected, $ping);
> my (@ports, @lines, %status);



A good programmer would limit the scope of variables rather
than making them visible even where they don't need to be
visible.

A good place to learn about scoping in Perl is:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html



> $command = "/bin/rm -f /root/.ssh/known_hosts";
> system($command);



$command was never declared, so what was the point of declaring
some variables but not others? (rhetorical question because the
answer is clearly "There is no point" to doing that).


> for ($i=0; $i < @firewalls; $i++)
> {
>
> $string = $firewalls[$i];



A good Perl programmer would let perl do the indexing for him,
but replacing those 2 lines with:


foreach my $string ( @firewalls ) {


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
jm-1@remotekontrol.com
Guest
Posts: n/a
 
      06-26-2006
Thank You very much for your help. I have been reading the tutorials @
http://learn.perl.org/library/beginning_perl/. The only problem that I
find is the examples are not real world. I would love to read through
and explanation of a real world perl script that many people use "in
human language".

It's ok using make believe examples to learn the concepts of
programming but it leaves one thinking "how would I apply that in the
real world", " I wonder how people have used that example in anger" etc
etc

It is so nice to have people of your nature who freely give thier time
to help folk like me. I have always avoided programming because I find
it frustrating but I am sure that just like many other things, if a
topic is explained to me in a way that I can put it to use, then I will
grasp it very quickly.

Thank you once again

Regards

And

Tad McClellan wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > I have not programmed before and I am doing my best to get to grips
> > with Perl in order to decode and modify a backup script.

>
>
> > Could anyone
> > please explain the following line in plain english to me?

>
>
> Another followup already did that, so I'll address other issues.
>
>
> > I am working my through
> > the tutorials

>
>
> Which ones?
>
> There are more bad Perl tutorials than good ones.
>
> If you tell us which ones, then we can tell you whether they are
> worth any study or not...
>
>
> > have pasted the entire script at the bottom of this message.

>
>
> It appears to have been written by someone who isn't well versed
> in programmeing in general, nor in Perl in particular.
>
> Modify it if you must, but don't be learning the bad habits
> it displays.
>
>
> > #!/usr/bin/perl

>
>
> You should ask for all the (machine) help you can get:
>
> use warnings;
> use strict;
>
> They will find many common programming mistakes for you.
>
> Do read up on them:
>
> perldoc warnings
>
> perldoc strict
>
>
> > #Declare the varibles

>
>
> A good programmer would not repeat in comments what is already
> said in the code. Comments are for things that cannot be easily
> said in the code (such as "why" rather than "how").
>
>
> > my ($hour, $min, $sec, $now, $remoteip, $port, $connected, $ping);
> > my (@ports, @lines, %status);

>
>
> A good programmer would limit the scope of variables rather
> than making them visible even where they don't need to be
> visible.
>
> A good place to learn about scoping in Perl is:
>
> "Coping with Scoping":
>
> http://perl.plover.com/FAQs/Namespaces.html
>
>
>
> > $command = "/bin/rm -f /root/.ssh/known_hosts";
> > system($command);

>
>
> $command was never declared, so what was the point of declaring
> some variables but not others? (rhetorical question because the
> answer is clearly "There is no point" to doing that).
>
>
> > for ($i=0; $i < @firewalls; $i++)
> > {
> >
> > $string = $firewalls[$i];

>
>
> A good Perl programmer would let perl do the indexing for him,
> but replacing those 2 lines with:
>
>
> foreach my $string ( @firewalls ) {
>
>
> --
> Tad McClellan SGML consulting
> (E-Mail Removed) Perl programming
> Fort Worth, Texas


 
Reply With Quote
 
jm-1@remotekontrol.com
Guest
Posts: n/a
 
      06-26-2006
Hi Jurgen

If the current line starts with a word character
> "\w Match a "word" character (alphanumeric plus "_")"


what does the m/^ mean in " if (m/^\w/)"

I must sound very inexperienced! I have not come accroos this sytax in
the tutorials yet.

Thank you so much for your time

Regards

Andy

Jürgen Exner wrote:
> (E-Mail Removed) wrote:
> > I have not programmed before and I am doing my best to get to grips
> > with Perl in order to decode and modify a backup script. Could anyone
> > please explain the following line in plain english to me? I understand
> > that what we are saying is "While the file handle <CONFIG> is
> > available,

>
> Nope. That line means "While the file that is referenced by the file handle
> CONFIG has not reached EndOfFile (i.e. while there are still lines
> available)" loop through each line in sequence."
>
> > chomp the newline character of off each line as we read it"
> > and then I am lost.

>
> > while (<CONFIG>)
> > {
> > chomp;
> > if (m/^\w/)

>
> If the current line starts with a word character
> "\w Match a "word" character (alphanumeric plus "_")"
> then
>
> > {
> > ($key, $data) = split /=/, $_;

>
> Split the current line at any equal sign "=", store the first part in $key,
> store the second part in $data, ignore any other parts.
>
> > $array{$key} = $data;

>
> And store $data in the hash named %array at the data point $key. BTW: using
> %array for a hash is somewhat misleading.
>
> jue


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-26-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> Thank You very much for your help.



You are welcome.

I am going to ask something in return.

Please compose followups the proper way, quoting only what you are
going to comment on, trimming stuff that you are not going to comment
on, and interleaving your comments after the quoted text that the
comment applies to.

Have you seen the Posting Guidelines that are posted here frequently?


> I have been reading the tutorials @
> http://learn.perl.org/library/beginning_perl/.



Oh, allright then. You've managed to find one of the good ones.


> I have always avoided programming because I find
> it frustrating



If the frustration is something that you would prefer to not deal
with, then you can always hire an actual programmer. (like me!)

heh.



[ snip TOFU ]

--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-26-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> If the current line starts with a word character
>> "\w Match a "word" character (alphanumeric plus "_")"

>
> what does the m/^ mean in " if (m/^\w/)"



m// is the syntax for the pattern match operator (regular expressions).

You can use a delimiter other than slash if you like, eg: m## m!!

If you choose to use slash as your delimiter, then you are
allowed to leave off the "m", eg: //

A caret (^) in a regex matches the beginning of the string.

So then, m/^\w/ will match (and return true) if the 1st
character in $_ is one of the 63 "word" characters.



[ snip more TOFU ]

--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-27-2006
[For future reference: it is customary to shorten the quoted text to the
relevant part and to insperse your comments at the appropriate place]

(E-Mail Removed) wrote:
> If the current line starts with a word character
>> "\w Match a "word" character (alphanumeric plus "_")"

>
> what does the m/^ mean in " if (m/^\w/)"


The way you are asking it stands for a syntax error because the command is
not complete.

The m operator (which is explained in perldoc perlop) takes one argument
that is a regular expression (RE), commonly written as m/..../ where the
..... is to be replaced with the RE.
The ^ at the beginning of an RE indicates a match at the beginning of the
line and the \w indicates a word character. That means the whole RE matches,
if the line begins with a word character in which case m/.../ returns true.

jue


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-27-2006
Tad McClellan wrote:
> So then, m/^\w/ will match (and return true) if the 1st
> character in $_ is one of the 63 "word" characters.


Subject to your locale, of course

jue


 
Reply With Quote
 
jm-1@remotekontrol.com
Guest
Posts: n/a
 
      06-27-2006
Hi Tad

In response to your request

>> "Please compose followups the proper way, quoting only what you are
>> going to comment on, trimming stuff that you are not going to comment
>> on, and interleaving your comments after the quoted text that the
>> comment applies to."


I will be sure to clean up my act when responding to messages in the
groups. My apologies, I do not use the groups that often. I will read
the FAQ (I promise).

Thank you for help

Regards

Andy


Tad McClellan wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > Thank You very much for your help.

>
>
> You are welcome.
>
> I am going to ask something in return.
>
> Please compose followups the proper way, quoting only what you are
> going to comment on, trimming stuff that you are not going to comment
> on, and interleaving your comments after the quoted text that the
> comment applies to.
>
> Have you seen the Posting Guidelines that are posted here frequently?
>
>
> > I have been reading the tutorials @
> > http://learn.perl.org/library/beginning_perl/.

>
>
> Oh, allright then. You've managed to find one of the good ones.
>
>
> > I have always avoided programming because I find
> > it frustrating

>
>
> If the frustration is something that you would prefer to not deal
> with, then you can always hire an actual programmer. (like me!)
>
> heh.
>
>
>
> [ snip TOFU ]
>
> --
> Tad McClellan SGML consulting
> (E-Mail Removed) Perl programming
> Fort Worth, Texas


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob trying to understand simple Perl grep statement walterbyrd Perl Misc 18 12-16-2008 01:44 AM
can't understand a java statement - please help Moham12345 Java 5 04-18-2007 07:40 PM
Newbie trying to understand some perl code. ed Perl Misc 11 11-24-2005 05:22 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Import statement help required by newbie please Rogue 9 Python 0 07-04-2003 08:58 PM



Advertisments