Harry Putnam wrote: : : My script has a function that I want to just print its : output in some circumstances but in others I need to : capture its output into an array for further processing: : : sub strip_to_bone { : if(/[Ss]ource/ && /[Dd]estination/){ : ($line = $line) =~ s/[Ss]ource/Src/g; : ($line = $line) =~ s/[Dd]estination/Dst/g; : $line =~ s/(^[^ ]+, )([0-9][^ ]+ [0-9][^ ]+)(.*)(Src[^ ]+ : [0-9]+)(.*)(Dst[^ ]+ [0-9]+)/$2 $4 $6/; : $line = ''; : printf " %s %s %-28s %s\n", "<$.>", "$2", "$4", "$6"; : } : }
Sorry Harry. This reply doesn't answer your question. But your subroutine raises some excellent reasons why a better defined programming style can save keystrokes and headaches later on. One approach to subroutine management is not to allow subroutines to print anything. Certainly, this is not a hard and fast rule, but if this sub routine had been written with that in mind, it my look more like the following. print strip_to_bone(); sub strip_to_bone { # process data return sprintf " %s %s %-28s %s\n", $line_number, $2, $4, $6; } With this style your solution would be: my @c = strip_to_bone(); Another good habit is to pass all data into the subroutine. Any external value should be passed. The sub shouldn't affect its outside environment. Again, this is not a hard and fast, but it makes for easier maintenance down the road. print strip_to_bone( $line, $., $_ ); sub strip_to_bone { my( $line, $line_number, $foo ) = @_; # process data return sprintf " %s %s %-28s %s\n", $line_number, $2, $4, $6; } If the subroutine changes $line, $line_number, or $foo it won't affect the rest of the program. This is a real boon when debugging. Unfortunately, your subroutine is printing something *and* manipulating the external variable $line. So we have to cheat on one of the style rules we just established. (Actually, we probably don't - but we don't access to the rest of the script.) The easiest way to do this is to send $line as a reference. print strip_to_bone( \$line, $., $_ ); sub strip_to_bone { # # This function modifies the external variable $line # my( $line, $line_number, $foo ) = @_; if ( $foo =~ /[Ss]ource/ && $foo =~ /[Dd]estination/ ) { $$line =~ s/[Ss]ource/Src/g; $$line =~ s/[Dd]estination/Dst/g; $$line =~ /^[^ ]+, (\d[^ ]+ \d[^ ]+).*(Src[^ ]+ \d+).*(Dst[^ ]+ \d+)/; $$line = ''; return sprintf " %s %s %-28s %s\n", $line_number, $1, $2, $3; } return ''; } HTH, Charles K. Clarkson -- Head Bottle Washer, Clarkson Energy Homes, Inc. Mobile Home Specialists 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]