Please help Perl Newbie understand this statement

Discussion in 'Perl Misc' started by jm-1@remotekontrol.com, Jun 26, 2006.

  1. Guest

    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"
     
    , Jun 26, 2006
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Jun 26, 2006
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2006
    #3
  4. Guest

    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:
    > <> 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
    > Perl programming
    > Fort Worth, Texas
     
    , Jun 26, 2006
    #4
  5. Guest

    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:
    > 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
     
    , Jun 26, 2006
    #5
  6. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2006
    #6
  7. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2006
    #7
  8. [For future reference: it is customary to shorten the quoted text to the
    relevant part and to insperse your comments at the appropriate place]

    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
     
    Jürgen Exner, Jun 27, 2006
    #8
  9. 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
     
    Jürgen Exner, Jun 27, 2006
    #9
  10. Guest

    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:
    > <> 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
    > Perl programming
    > Fort Worth, Texas
     
    , Jun 27, 2006
    #10
  11. Dr.Ruud Guest

    Jürgen Exner schreef:
    > Tad McClellan:


    >> 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 ;-)


    See my unicount.pl in
    news:
    that finds:

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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 27, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    590
    Big Brian
    Oct 14, 2003
  2. Rogue 9
    Replies:
    0
    Views:
    276
    Rogue 9
    Jul 4, 2003
  3. Moham12345
    Replies:
    5
    Views:
    293
    Ian Shef
    Apr 18, 2007
  4. ed
    Replies:
    11
    Views:
    188
    Euclid Uranium
    Nov 24, 2005
  5. walterbyrd
    Replies:
    18
    Views:
    277
    C.DeRykus
    Dec 16, 2008
Loading...

Share This Page