Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl White Magic - Special Variables and Command Line Switches

by cog (Parson)
on Feb 16, 2005 at 10:55 UTC ( [id://431511]=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
            while (<>) {
                    @words = split /\W+/;
                    $words += @words;
            }
            print "lines -> $.\nwords -> $words\n";
    
  2. or download this
            -e commandline
                may be used to enter one line of program.
    
  3. or download this
            As with all standard commands, a single-character switch
           may be clustered with the following switch, if any.
    
               #!/usr/bin/perl -spi.orig   # same as -s -p -i.orig
    
  4. or download this
            -n   causes Perl to assume the following loop around
                your program [...]
    
    ...
                    while (<>) {
                        ...             # your program goes here
                    }
    
  5. or download this
            LINE:
              while (<>) {
                      $w += split /\W+/;
                      END { print"$w $.\n" }
              }
    
  6. or download this
            #!/usr/bin/perl -n
    
            # counts words and lines in a file
    ...
            @words = split /\W+/;
            $words = @words;
            END { print "lines -> $.\nwords -> $words\n"; }
    
  7. or download this
            #!/usr/bin/perl -w
            use strict;
    
  8. or download this
            my $line;
    
            while (<>) {
    ...
              }
              print;
            }
    
  9. or download this
            #!/usr/bin/perl -wp
            use strict;
    
            #Adding a commit each 100 lines
    
            unless ( $. % 100 ) { print "commit;\n"; }
    
  10. or download this
            -p   causes Perl to assume the following loop around
                your program [...]
    
    ...
                    } continue {
                        print or die "-p destination: $!\n";
                    }
    
  11. or download this
            -i[extension]
                specifies that files processed by the "<>"
                construct are to be edited in-place.  It does
                this by renaming the input file, opening [...]
    
  12. or download this
            #!/usr/bin/perl -wlp
            use strict;
    
            # trims lines to 80 columns
    
            substr($_, 80) = "";
    
  13. or download this
            -l[octnum]
                enables automatic line-ending processing.
    
  14. or download this
            #!/usr/bin/perl -wp
            use strict;
    
    ...
            $\ = $/;
    
            substr($_, 80) = ""
    
  15. or download this
            #!/usr/bin/perl -wpi
            use strict;
    
    ...
                    chomp;
                    print "$_\n";
            }
    
  16. or download this
            -a   turns on autosplit mode when used with a -n or
                -p.  An implicit split command to the @F array
                is done as the first thing inside the implicit
    ...
                        @F = split(' ');
                        print pop(@F), "\n";
                    }
    
  17. or download this
            @F      The array @F contains the fields of each
                   line read in when autosplit mode is turned
                   on. [...]
    
  18. or download this
            #!/usr/bin/perl -wane
            use strict
    
    ...
                            print "$_ -> $commands{$_}\n"
                    }
            }
    
  19. or download this
            #!/usr/bin/perl -w
            use strict;
    
            my @a = (1, 2, 3, 4, 5);
    
            print "@a";
    
  20. or download this
            #!/usr/bin/perl -w
            use strict;
    
    ...
    
            $" = "|";
            print "@a";
    
  21. or download this
            #!/usr/bin/perl -w
            use strict;
    
            my @a = (1, 2, 3, 4, 5);
    
            print @a;
    
  22. or download this
            #!/usr/bin/perl -w
            use strict;
    
    ...
    
            $, = "-";
            print @a;
    
  23. or download this
            perl -MExtUtils::Installed -le '
                    print for ExtUtils::Installed->new()->modules'
    
  24. or download this
    perl -        le 'use ExtUtils::Installed;
                    print for ExtUtils::Installed->new()->modules'
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://431511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-24 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found