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

Re: [PBML] Re: Reading a file

Expand Messages
  • Chad Perrin
    ... use warnings; seems to be the new Right Way To Do It, but I ve yet to see an explanation for why it s preferable to -w. What are the reasons for
    Message 1 of 10 , Apr 5, 2006
    • 0 Attachment
      On Wed, Apr 05, 2006 at 07:38:45PM -0000, a_z0_9_blah wrote:
      >
      > A couple of suggestions :-)
      >
      > At the top of your program, (after the #!... line),
      > use strict;
      > use warnings;
      >
      > (the -w switch you used is better replaced by the use warnings
      > pragma instead).

      "use warnings;" seems to be the new Right Way To Do It, but I've yet to
      see an explanation for why it's preferable to -w. What are the reasons
      for preferring "use warnings;" over -w?

      --
      Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
      "A script is what you give the actors. A program
      is what you give the audience." - Larry Wall
    • Chad Perrin
      ... Fore!!! #!/usr/bin/perl -w use strict; while ( ) { chomp; print( $_ n ) if length == 8; } This version uses the diamond operator to take input when
      Message 2 of 10 , Apr 5, 2006
      • 0 Attachment
        On Wed, Apr 05, 2006 at 07:38:45PM -0000, a_z0_9_blah wrote:
        >
        > open(MYFILE, "example.txt") || die "Can't open myfile: $!";
        >
        > while (my $word = <MYFILE>) {
        > chomp $word; # remove the newline
        > print if length $word == 8;
        > }

        Fore!!!

        #!/usr/bin/perl -w
        use strict;
        while (<>) { chomp; print("$_\n") if length == 8; }

        This version uses the diamond operator to take input when calling the
        script. Thus, if you save this file as len8, and you were operating on
        a file called words.txt, you would enter the following at the command
        line, assuming you made the file executable:

        $ len8 words.txt

        additional note: Because $_ is an implicit scalar variable here, the
        chomp and length functions automatically default to using that variable
        in this context. If you want to make your script someone more readable
        to someone not familiar with Perl's quirks, of course, you could use an
        explicit variable instead.

        hope that helps

        --
        Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
        "A script is what you give the actors. A program
        is what you give the audience." - Larry Wall
      • Mr. Shawn H. Corey
        ... That s because some older operating systems do not read the shebang and the flag would be missed. Now if someone would come up with: use taint; -- __END__
        Message 3 of 10 , Apr 5, 2006
        • 0 Attachment
          On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
          > "use warnings;" seems to be the new Right Way To Do It, but I've yet to
          > see an explanation for why it's preferable to -w. What are the reasons
          > for preferring "use warnings;" over -w?
          >

          That's because some older operating systems do not read the shebang and
          the flag would be missed.

          Now if someone would come up with: use taint;


          --
          __END__

          Just my 0.00000002 million dollars worth,
          --- Shawn

          "For the things we have to learn before we can do them, we learn by doing them."
          Aristotle

          * Perl tutorials at http://perlmonks.org/?node=Tutorials
          * A searchable perldoc is at http://perldoc.perl.org/
        • Chad Perrin
          ... Ahh, that s a good point. I keep forgetting the limitations of OSes like Windows. -- Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ] Real
          Message 4 of 10 , Apr 5, 2006
          • 0 Attachment
            On Wed, Apr 05, 2006 at 06:10:11PM -0400, Mr. Shawn H. Corey wrote:
            > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
            > > "use warnings;" seems to be the new Right Way To Do It, but I've yet to
            > > see an explanation for why it's preferable to -w. What are the reasons
            > > for preferring "use warnings;" over -w?
            > >
            >
            > That's because some older operating systems do not read the shebang and
            > the flag would be missed.
            >
            > Now if someone would come up with: use taint;

            Ahh, that's a good point. I keep forgetting the limitations of OSes
            like Windows.

            --
            Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
            "Real ugliness is not harsh-looking syntax, but having to
            build programs out of the wrong concepts." - Paul Graham
          • a_z0_9_blah
            ... yet to ... reasons ... and ... see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are several issues involved here. I m not really clear on
            Message 5 of 10 , Apr 5, 2006
            • 0 Attachment
              --- In perl-beginner@yahoogroups.com, "Mr. Shawn H. Corey"
              <shawnhcorey@...> wrote:
              >
              > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
              > > "use warnings;" seems to be the new Right Way To Do It, but I've
              yet to
              > > see an explanation for why it's preferable to -w. What are the
              reasons
              > > for preferring "use warnings;" over -w?
              > >
              >
              > That's because some older operating systems do not read the shebang
              and
              > the flag would be missed.

              see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are
              several issues involved here. I'm not really clear on this, but it can
              have an undesired effect with use, do or require of other files.

              Chris
            • Chad Perrin
              ... Thanks. I ll read that. -- Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ] There comes a time in the history of any project when it becomes
              Message 6 of 10 , Apr 5, 2006
              • 0 Attachment
                On Wed, Apr 05, 2006 at 11:59:31PM -0000, a_z0_9_blah wrote:
                > --- In perl-beginner@yahoogroups.com, "Mr. Shawn H. Corey"
                > <shawnhcorey@...> wrote:
                > >
                > > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
                > > > "use warnings;" seems to be the new Right Way To Do It, but I've
                > yet to
                > > > see an explanation for why it's preferable to -w. What are the
                > reasons
                > > > for preferring "use warnings;" over -w?
                > > >
                > >
                > > That's because some older operating systems do not read the shebang
                > and
                > > the flag would be missed.
                >
                > see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are
                > several issues involved here. I'm not really clear on this, but it can
                > have an undesired effect with use, do or require of other files.

                Thanks. I'll read that.

                --
                Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
                "There comes a time in the history of any project when it becomes necessary
                to shoot the engineers and begin production." - MacUser, November 1990
              • J.E. Cripps
                ... One might change the length test thusly: #!/usr/bin/perl -w use strict; my $length = shift; while ( ) { chomp; print( $_ n ) if length == $length; } which
                Message 7 of 10 , Apr 6, 2006
                • 0 Attachment
                  Chad Perrin <perrin@...> did lately instruct us with:

                  > Fore!!!
                  > #!/usr/bin/perl -w
                  > use strict;
                  > while (<>) { chomp; print("$_\n") if length == 8; }

                  Chad: to run (having saved the code in a file "len8":

                  > $ len8 words.txt

                  One might change the length test thusly:

                  #!/usr/bin/perl -w
                  use strict;
                  my $length = shift;
                  while (<>) { chomp; print("$_\n") if length == $length; }

                  which is run:

                  lentest.pl 8

                  or 7, 6, or the integer of your choice. For "shift" see
                  perldoc perlfunc, or page 215 of the Camel Book (_Programming Perl_).

                  > Chad: This version uses the diamond operator to take input when calling
                  > the script.

                  More formally designated, at least in the Camel Book as the "line input
                  operator" and commonly also as the "angle brackets" or "angle operator"
                  It's discussed on page 53 of the Camel.

                  But -- after some looking I have failed to find a similar explication
                  in perldoc perlop. This might be oversight on my part. The < >
                  operator is discussed in numerous sites, e.g.

                  Programming:Perl User I/O - Wikibooks
                  http://en.wikibooks.org/wiki/Programming:Perl_User_I/O

                  > Chad: additional note: Because $_ is an implicit scalar variable here,
                  > the chomp and length functions automatically default to using that
                  > variable in this context. If you want to make your script someone more
                  > readable to someone not familiar with Perl's quirks, of course, you could
                  > use an explicit variable instead.

                  Some discussion of the $_ :
                  http://perldoc.perl.org/perlvar.html perlvar - perldoc.perl.org

                  The < > and the implicit $_ are quintessial Perl idioms. The
                  reason they are employed in most persons' first attempts is
                  because they provide useful solutions to common tasks.
                Your message has been successfully submitted and would be delivered to recipients shortly.