Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Common Perl Pitfalls

by Joe_ (Beadle)
on Apr 09, 2012 at 21:52 UTC ( #964216=perlmeditation: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    while($some_data=~ m/some_regex/)
    {
        # Do some stuff
    }
    
  2. or download this
    while($some_data=~ m/some_regex/g)
    {
        # Do some stuff
    }
    
  3. or download this
    $to_replace='some_string';
    $my_string=~ s/$to_replace/$better_data/;
    
  4. or download this
    $to_replace='a caret looks like ^';
    $my_string=~ s/$to_replace/$better_data/;
    
  5. or download this
    $to_replace='a caret looks like ^';
    $my_string=~ s/\Q$to_replace\E/$better_data/;
    
  6. or download this
    $to_replace=qr/^your_regex_here$/;
    $my_string=~ s/$to_replace/$better_data/;
    
  7. or download this
    while($string=~ m/(reg)(ex)/g)
    {
        $string=~ s/$1$2/$1ister/;
    }
    
  8. or download this
    $string=~ s/(reg)ex/$1ister/g;
    
  9. or download this
    $dummy=$string;
    while($dummy=~ m/(reg)(ex)/g)
    {
        $string=~ s/$1$2/$1ister/;
    }
    
  10. or download this
    while($string=~ m/(reg)(ex)/)
    {
        $string=~ s/$1$2/$1ister/;
    }
    
  11. or download this
    for($i=0;$i<@array;$i++)
    {
    ...
          delete $array[$i];
       }
    }
    
  12. or download this
    for($i=0;$i<@array;$i++)
    {
    ...
          splice @array,$i,1;
       }
    }
    
  13. or download this
    for($i=0;$i<@array;$i++)
    {
    ...
          $i--;
       }
    }
    
  14. or download this
    @array = @array[grep {!should_delete($_)} 0..$#array];
    
  15. or download this
    $rec_sep=$/;
    undef $/;
    $slurp=<INPUTFILE>;
    # Regex matching here ...
    
  16. or download this
    $rec_sep=$/;
    undef $/;
    $slurp=<INPUTFILE>;
    print "Enter something: ";
    $something=<STDIN>;
    
  17. or download this
    $rec_sep=$/;
    undef $/;
    ...
    $/=$rec_sep;
    print "Enter something: ";
    $something=<STDIN>;
    
  18. or download this
    my $data = do {local $/; <INPUTFILE>};
    
  19. or download this
    open COMMAND,'-|','some_command';
    $input=<COMMAND>;
    ...
    #....bla bla
    wait if $pid
    #... bla bla
    
  20. or download this
    open COMMAND,'-|','some_command';
    $input=<COMMAND>;
    ...
    #....bla bla
    wait if $pid
    #... bla bla
    
  21. or download this
    open COMMAND,'-|','some_command';
    $input=<COMMAND>;
    ...
    #....bla bla
    waitpid $pid,0;
    #... bla bla
    
  22. or download this
    undef $_ for keys %SIG;
    fork while 1;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://964216]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2015-07-07 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls