code_junkie

Why is $1 empty in my substitution?

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. while(<TOCFILE>)
  2. {
  3. $toc_line = $_;
  4. $toc_line =~ s/<inlineFig.*?(../pics/ch09_inline99_*?.jpg)*?</inlineFig>/<img src="${1}" alt="" />/g;
  5. $new_toc_file .= $toc_line;
  6. }
  7.  
  8. <img src="../pics/ch09_inline99_00" alt="" />
  9.  
  10. <img src="" alt="" />
  11.  
  12. inline99_*?.jpg
  13. ^^^
  14.  
  15. ($PATTERN)*?
  16.  
  17. $_ = 'one two three';
  18. s/(w+s*)*/$1/;
  19. print;
  20.  
  21. while(<TOCFILE>)
  22. {
  23. $toc_line = $_;
  24. $toc_line =~
  25. s/ # replace the follwoing
  26.  
  27. <inlineFig # match this text
  28. .*? # then any characters until the next sequence matches
  29. ( # throw the match into $1
  30. ../pics/ch09_inline99_ # ..picscho9_inline99_
  31. d*?.jpg # folowed by 0 or more numbers
  32. )*? # keeping doing that until the next sequence matches
  33. </inlineFig> # match this text
  34.  
  35. / # with the follwoing
  36.  
  37.  
  38. <img src="${1}" alt="" /> # some text and the result of $1 above.
  39.  
  40. /xg; # <- the x makes it ignore whitespace and #comments
  41. $new_toc_file .= $toc_line;
  42. }
  43.  
  44. #!/usr/bin/perl
  45.  
  46. use warnings;
  47. use strict;
  48.  
  49. my $new_toc_file;
  50.  
  51. {
  52. # localizing $_ protects any existing value in the global $_
  53. # you should localize $_ even if you choose to assign it to a variable
  54.  
  55. local $_;
  56.  
  57. while(<DATA>) {
  58. # in the absence of the bind operator =~, s/// operates against $_
  59. s!<inlineFig.*?(../pics/ch09_inline99_.*?.jpg)</inlineFig>!<img src="$1" alt="" />!g;
  60. $new_toc_file .= $_;
  61. }
  62. }
  63.  
  64. print $new_toc_file, "n";
  65.  
  66. __END__
  67. <inlineFig>../pics/ch09_inline99_00.jpg</inlineFig>
Add Comment
Please, Sign In to add comment