Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML]

Expand Messages
  • Shawn H Corey
    ... Always `use strict;` Always `use warnings;` -- Just my 0.00000002 million dollars worth, Shawn Confusion is the first step of understanding. Programming is
    Message 1 of 20 , Sep 27, 2011
    View Source
    • 0 Attachment
      On 11-09-27 04:56 PM, Rajagopal, Jay wrote:
      > Don't use strict.

      Always `use strict;`

      Always `use warnings;`


      --
      Just my 0.00000002 million dollars worth,
      Shawn

      Confusion is the first step of understanding.

      Programming is as much about organization and communication
      as it is about coding.

      The secret to great software: Fail early & often.

      Eliminate software piracy: use only FLOSS.

      "Make something worthwhile." -- Dear Hunter
    • Shawn H Corey
      ... # use the three argument open and test for success open my $cmd_fh,
      Message 2 of 20 , Sep 27, 2011
      View Source
      • 0 Attachment
        On 11-09-27 04:46 PM, medura43 wrote:
        >
        > !/usr/bin/perl
        > use strict;
        > use warnings;
        >
        > open(CMD,"<lvmtab.txt");

        # use the three argument open and test for success
        open my $cmd_fh, '<', 'lvmtab.txt' or die "could not open lvmtab.txt: $!\n";

        > undef($/); # Slurps in the lvmtab.txt into the array
        >
        > while (<CMD>) {
        > my @device = (split/[\n]/);
        > }

        # replace the loop with:
        my @dvice = <$cmd_fh>;

        # always close your files
        close $cmd_fh or die "could not close lvmtab.txt: $!\n";

        >
        > foreach my $device (my @device) {
        > (my $agile, my $legacy) = split(/\s+/, my $device);

        # too many `my`s
        my ( $agile, $legacy ) = split /\s+/, $device;

        __END__

        From this point on, the script does not make sense.

        > my $legacy{my $agile} = my $legacy;
        > }

        1. Closing the loop means it does do anything useful.

        >
        > $legacy = (keys my %legacy);

        2. %legacy does not contain anything.

        >
        > foreach $agile (keys %legacy) {

        3. %legacy is empty, this loop is skipped.

        > print "$agile:\t$legacy{$agile}\n";
        > }
        > $|=1;

        4. Don't do this; it only slows down your program.

        > print "Which device file do you want\n "; # Test to find out why it
        > chomp(my $name = <STDIN>); # hangs
        > print "The value is $name \n";
        >
        > $hash_len = keys %legacy; # Number of lines in a hash

        5. No, the number of keys in the hash, which is zero.

        >
        > print "The following elements\n";
        > print "$agile --> $legacy{$agile} $hash_len\n";
        > close CMD;


        --
        Just my 0.00000002 million dollars worth,
        Shawn

        Confusion is the first step of understanding.

        Programming is as much about organization and communication
        as it is about coding.

        The secret to great software: Fail early & often.

        Eliminate software piracy: use only FLOSS.

        "Make something worthwhile." -- Dear Hunter
      • Rajagopal, Jay
        I have professional programming experience in PERL for 10 years. If a person does not know between a reference and a declaration, DON T USE STRICT. ... From:
        Message 3 of 20 , Sep 27, 2011
        View Source
        • 0 Attachment
          I have professional programming experience in PERL for 10 years.

          If a person does not know between a reference and a declaration, DON'T USE STRICT.

          -----Original Message-----
          From: Shawn H Corey [mailto:shawnhcorey@...]
          Sent: Tuesday, September 27, 2011 5:40 PM
          To: perl-beginner@yahoogroups.com
          Cc: Rajagopal, Jay
          Subject: Re: [PBML] <STDIN>

          On 11-09-27 04:56 PM, Rajagopal, Jay wrote:
          > Don't use strict.

          Always `use strict;`

          Always `use warnings;`


          --
          Just my 0.00000002 million dollars worth,
          Shawn

          Confusion is the first step of understanding.

          Programming is as much about organization and communication as it is about coding.

          The secret to great software: Fail early & often.

          Eliminate software piracy: use only FLOSS.

          "Make something worthwhile." -- Dear Hunter

          T. Rowe Price (including T. Rowe Price Group, Inc. and its affiliates) and its associates do not provide legal or tax advice. Any tax-related discussion contained in this e-mail, including any attachments, is not intended or written to be used, and cannot be used, for the purpose of (i) avoiding any tax penalties or (ii) promoting, marketing, or recommending to any other party any transaction or matter addressed herein. Please consult your independent legal counsel and/or professional tax advisor regarding any legal or tax issues raised in this e-mail.

          The contents of this e-mail and any attachments are intended solely for the use of the named addressee(s) and may contain confidential and/or privileged information. Any unauthorized use, copying, disclosure, or distribution of the contents of this e-mail is strictly prohibited by the sender and may be unlawful. If you are not the intended recipient, please notify the sender immediately and delete this e-mail.
        • Shawn H Corey
          ... Excellent. I like to see young people having an interest in Perl. BTW, I ve been programming in Perl since version 4. ... What does that have to do with
          Message 4 of 20 , Sep 27, 2011
          View Source
          • 0 Attachment
            On 11-09-27 05:53 PM, Rajagopal, Jay wrote:
            > I have professional programming experience in PERL for 10 years.

            Excellent. I like to see young people having an interest in Perl. BTW,
            I've been programming in Perl since version 4.

            >
            > If a person does not know between a reference and a declaration, DON'T USE STRICT.

            What does that have to do with it? There are some good reasons to use
            strict:

            1. It catches spelling mistakes of any variable.

            2. It doesn't allow symbolic references. Symbolic references can
            inadvertently overwrite variables with strange values. This is a bug
            that's hard to diagnose.

            `use strict;` and `use warnings;` are recommended Perl's best practices.

            BTW, you can `use diagnostics;` instead of `use warnings;`. It gives a
            lengthier warning message but does the same thing.
          • Amish Husain
            I completely agree on this, I have always used -w /use strict. Its always a good programming practice and this will help you write better/cleaner code.
            Message 5 of 20 , Sep 27, 2011
            View Source
            • 0 Attachment
              I completely agree on this, I have always used -w /use strict. Its always a good programming practice and this will help you write better/cleaner code.


              ________________________________
              From: Shawn H Corey <shawnhcorey@...>
              To: perl-beginner@yahoogroups.com
              Cc: "Rajagopal, Jay" <jay_rajagopal@...>
              Sent: Tuesday, September 27, 2011 5:40 PM
              Subject: Re: [PBML] <STDIN>


               
              On 11-09-27 04:56 PM, Rajagopal, Jay wrote:
              > Don't use strict.

              Always `use strict;`

              Always `use warnings;`

              --
              Just my 0.00000002 million dollars worth,
              Shawn

              Confusion is the first step of understanding.

              Programming is as much about organization and communication
              as it is about coding.

              The secret to great software: Fail early & often.

              Eliminate software piracy: use only FLOSS.

              "Make something worthwhile." -- Dear Hunter



              [Non-text portions of this message have been removed]
            • merlyn@stonehenge.com
              ... Rajagopal I have professional programming experience in PERL for 10 Rajagopal years. No you don t. If you still call it PERL , you ve *never* been a
              Message 6 of 20 , Sep 28, 2011
              View Source
              • 0 Attachment
                >>>>> "Rajagopal" == Rajagopal, Jay <jay_rajagopal@...> writes:

                Rajagopal> I have professional programming experience in PERL for 10
                Rajagopal> years.

                No you don't. If you still call it "PERL", you've *never* been a
                professional.

                Please don't lie like that.

                --
                Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                See http://methodsandmessages.posterous.com/ for Smalltalk discussion
              • medura43
                Thank your for all your help, I am going to have a close look at all your comments.
                Message 7 of 20 , Sep 28, 2011
                View Source
                • 0 Attachment
                  Thank your for all your help, I am going to have a close look at all your comments.

                  --- In perl-beginner@yahoogroups.com, merlyn@... wrote:
                  >
                  > >>>>> "Rajagopal" == Rajagopal, Jay <jay_rajagopal@...> writes:
                  >
                  > Rajagopal> I have professional programming experience in PERL for 10
                  > Rajagopal> years.
                  >
                  > No you don't. If you still call it "PERL", you've *never* been a
                  > professional.
                  >
                  > Please don't lie like that.
                  >
                  > --
                  > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                  > See http://methodsandmessages.posterous.com/ for Smalltalk discussion
                  >
                • andy_bach@wiwb.uscourts.gov
                  while ( ) { my @device = (split/[ n]/); } Unfortunatley, the my here means @device goes way outside the while loop my @device; while ( ) { @device =
                  Message 8 of 20 , Sep 28, 2011
                  View Source
                  • 0 Attachment
                    while (<CMD>) {
                    my @device = (split/[\n]/);
                    }

                    Unfortunatley, the 'my' here means @device goes way outside the while loop
                    my @device;
                    while (<CMD>) {
                    @device = (split/[\n]/);
                    }

                    same as:
                    my @device = <CMD>;

                    as the <...> splits on \n for you.

                    >> Too many "my"s ... From this point on, the script does not make sense.

                    (my $agile, my $legacy) = split(/\s+/, my $device);
                    my $legacy{my $agile} = my $legacy;

                    Yeah, you need to first declare the hash (as a hash, that is, w/ the '%')
                    and then assign to it:
                    my ($agile, $legacy) = split(/\s+/, $device);
                    my %legacy;
                    $legacy{$agile} = $legacy;

                    'my' is only needed once per variable - it marks the variable as
                    'lexically scoped' but it doesn't need to be part of any further use.
                    Both:
                    my @device;
                    @device = <CMD>;

                    and:
                    my @device = <CMD>;

                    are basically the same. After that you can just use @device (or
                    $device[1] etc) as needed. "@devices" plural, is a better name (see
                    below).

                    It is silly/bad to use the same name for different vars unless there's
                    really really a strong reason ... can't imagine one. So using legacy as a
                    hash and a scalar var name will only confuse you more, later on. I often
                    just pluralize whatever's in there (assuming 'legacy' refers to some old
                    device name?)
                    my %legacy_devices;
                    $legacy_devices{$agile} = $legacy;

                    "Perl Best Practices" suggests naming hash to reflect the fact that they
                    are generally used as single objects ( $hash_name{$key} vs %hash_name -
                    unlike arrays which are often used as collectives, e.g. @devices vs
                    $devices[2]) so use a singular noun and a preposition (examples given:
                    %title_of
                    %count_for
                    %is_available

                    ) - a great book (Damian Conway, O'Reilly, natch) but we all can't be as
                    consistenly clever as Mr. Conway, even in our dreams.

                    Seems the next part you want is to get a name from the user and see if
                    that is a key to your hash - you're close there, but:

                    print "Which device file do you want\n "; # Test to find out why it
                    chomp(my $name = <STDIN>); # hangs
                    print "The value is $name \n";

                    $hash_len = keys %legacy; # Number of lines in a hash
                    print "The following elements\n";
                    print "$agile --> $legacy{$agile} $hash_len\n";

                    I think you want:
                    print "$name --> $legacy_devices{$name} $hash_len\n";

                    so:
                    if ( exist $legacy_devices{$name} ) {
                    print "$name --> $legacy_devices{$name}\n";
                    }
                    else {
                    print "Sorry $name is not in our legacy devices list (length:
                    $hash_len)\n";
                    }

                    or something.
                    ----------------------
                    Andy Bach
                    Systems Mangler
                    Internet: andy_bach@...
                    Voice: (608) 261-5738, Cell: (608) 658-1890

                    ?One of the most striking differences between a cat and a lie is that a
                    cat has only nine lives.?
                    Mark Twain, Vice President, American Anti-Imperialist League, and
                    erstwhile writer

                    [Non-text portions of this message have been removed]
                  • Paul.G
                    Thanks again, going through your comments. The reason, I interchange the var legacy and device. Is because, I am attempting to separate legacy device names
                    Message 9 of 20 , Sep 30, 2011
                    View Source
                    • 0 Attachment
                      Thanks again, going through your comments.

                      The reason, I interchange the var legacy and device. Is because, I am attempting to separate legacy device names from agile names. Agile names become the key in a hash for legacy devices.

                      for eg:
                      /dev/disk/disk43   /dev/dsk/c4t5d6

                      c4t5d6 is the legacy device name for disk43

                      cheers



                      ________________________________
                      From: "andy_bach@..." <andy_bach@...>
                      To: perl-beginner@yahoogroups.com
                      Sent: Thursday, 29 September 2011 1:51 AM
                      Subject: [PBML] Re: <STDIN>


                       
                      while (<CMD>) {
                      my @device = (split/[\n]/);
                      }

                      Unfortunatley, the 'my' here means @device goes way outside the while loop
                      my @device;
                      while (<CMD>) {
                      @device = (split/[\n]/);
                      }

                      same as:
                      my @device = <CMD>;

                      as the <...> splits on \n for you.

                      >> Too many "my"s ... From this point on, the script does not make sense.

                      (my $agile, my $legacy) = split(/\s+/, my $device);
                      my $legacy{my $agile} = my $legacy;

                      Yeah, you need to first declare the hash (as a hash, that is, w/ the '%')
                      and then assign to it:
                      my ($agile, $legacy) = split(/\s+/, $device);
                      my %legacy;
                      $legacy{$agile} = $legacy;

                      'my' is only needed once per variable - it marks the variable as
                      'lexically scoped' but it doesn't need to be part of any further use.
                      Both:
                      my @device;
                      @device = <CMD>;

                      and:
                      my @device = <CMD>;

                      are basically the same. After that you can just use @device (or
                      $device[1] etc) as needed. "@devices" plural, is a better name (see
                      below).

                      It is silly/bad to use the same name for different vars unless there's
                      really really a strong reason ... can't imagine one. So using legacy as a
                      hash and a scalar var name will only confuse you more, later on. I often
                      just pluralize whatever's in there (assuming 'legacy' refers to some old
                      device name?)
                      my %legacy_devices;
                      $legacy_devices{$agile} = $legacy;

                      "Perl Best Practices" suggests naming hash to reflect the fact that they
                      are generally used as single objects ( $hash_name{$key} vs %hash_name -
                      unlike arrays which are often used as collectives, e.g. @devices vs
                      $devices[2]) so use a singular noun and a preposition (examples given:
                      %title_of
                      %count_for
                      %is_available

                      ) - a great book (Damian Conway, O'Reilly, natch) but we all can't be as
                      consistenly clever as Mr. Conway, even in our dreams.

                      Seems the next part you want is to get a name from the user and see if
                      that is a key to your hash - you're close there, but:

                      print "Which device file do you want\n "; # Test to find out why it
                      chomp(my $name = <STDIN>); # hangs
                      print "The value is $name \n";

                      $hash_len = keys %legacy; # Number of lines in a hash
                      print "The following elements\n";
                      print "$agile --> $legacy{$agile} $hash_len\n";

                      I think you want:
                      print "$name --> $legacy_devices{$name} $hash_len\n";

                      so:
                      if ( exist $legacy_devices{$name} ) {
                      print "$name --> $legacy_devices{$name}\n";
                      }
                      else {
                      print "Sorry $name is not in our legacy devices list (length:
                      $hash_len)\n";
                      }

                      or something.
                      ----------------------
                      Andy Bach
                      Systems Mangler
                      Internet: andy_bach@...
                      Voice: (608) 261-5738, Cell: (608) 658-1890

                      ?One of the most striking differences between a cat and a lie is that a
                      cat has only nine lives.?
                      Mark Twain, Vice President, American Anti-Imperialist League, and
                      erstwhile writer

                      [Non-text portions of this message have been removed]




                      [Non-text portions of this message have been removed]
                    Your message has been successfully submitted and would be delivered to recipients shortly.