Please help Perl Newbie understand this statement

J

jm-1

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"
 
J

Jürgen Exner

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
 
T

Tad McClellan

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 ) {
 
J

jm-1

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 said:
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 ) {
 
J

jm-1

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
 
T

Tad McClellan

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?



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 ]
 
T

Tad McClellan

If the current line starts with a word character

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 ]
 
J

Jürgen Exner

[For future reference: it is customary to shorten the quoted text to the
relevant part and to insperse your comments at the appropriate place]

If the current line starts with a word character

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
 
J

Jürgen Exner

Tad said:
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
 
J

jm-1

Hi Tad

In response to your request

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 said:
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?



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 ]
 
D

Dr.Ruud

Jürgen Exner schreef:
Tad McClellan:

Subject to your locale, of course ;-)

See my unicount.pl in
that finds:

word
91801 /194522 = 47.193% (lower: 1380, upper: 1160)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top