Grand_Phallus has asked for the wisdom of the Perl Monks concerning the following question:

This is kind of a newbie question:
I'm trying to pull some text from a file, replace certain flags with variables from my script, and print the whole string of text with the replaced variables to an external process. I have the variable replacement down, but when I print the string, I see all of the special characters (\n specificially) in one big long string. I'd like the text to function just as it would when doing something like:
print SMBCLIENT "Hi there/n";
I'd appreciate any pointers/suggestions!
I'm including only a snippet of my much longer script.
open (MESSAGE, "/print_quota/windows/quota_message"); while (<MESSAGE>){ s/USER-LOGIN/$login/; s/PAGES-PRINTED/$pages/; if($pagestate eq "duplex"){ s/PAGESTATE/double sided/; } elsif ($pagestate eq "simplex"){ s/PAGESTATE/single sided/; } s/PRINTER/$printer/; s/QUOTA-USED/$quota/; s/POST-QUOTA/$report[2]/; s/ORIGINAL-QUOTA/$report[1]/; s/PRINTER/$printer/; s/MULTIPLIER/$multiplier/; open (SMBCLIENT, "|/usr/bin/smbclient -U Print_Quota -M $report[0] + -I $hostip[0]"); print SMBCLIENT "$_"; close SMBCLIENT; } close MESSAGE;
The file I'm reading from looks like this:
This is a test. Print quotas have yet to be fully implemented!\n\nYou, + USER-LOGIN, have printed PAGES-PRINTED page(s) PAGESTATE on the prin +ter \"PRINTER\".\n\nYou used QUOTA-USED credits on this print job, an +d you currently have POST-QUOTA quota credits left of your original O +RIGINAL-QUOTA.\nThe printer \"PRINTER\" is worth MULTIPLIER quota cre +dit(s).
As you may have guessed, this is a print accounting script.

Replies are listed 'Best First'.
(tye)Re: A string from a file
by tye (Sage) on May 05, 2001 at 03:41 UTC

    No reason to escape your quotes as you aren't using quotes as delimiters here. If you want the '\n's turned into newlines, then do it: s/\\n/\n/g and put that "g" option on all of your substitutions or follow the other advice given in this thread.

    Oh, and if you want to be able to include '\n' in the output by putting, for example, \\n, in the file, then you have to trickier:

    my %escape; @escape{qw(a l t n e r f)}= split /(.)/s, "\a\l\t\n\e\r\f"; s/\(.)/ $escape{$1} || $1 /ges;
    to show just one way. (:

            - tye (but my friends call me "Tye")
Re: A string from a file
by Rhandom (Curate) on May 05, 2001 at 02:42 UTC
    I just tried that and it doesn't look like it changes too much from what you type in. Looking at the file that you are reading, does the text file contain the actual text "\n" (two characters) or does it contain a newline. If it contains the two separate characters, the characters won't ever get interpolated as a newline.

    Something that will lighten the load of your swaps (especially on large documents) is to first delimit your swap in variables (instead of "PRINTER" put some special characters around it such as "__printer__"), and then swap in keys from a hash as in the following:
    # set up a hash containing the values you want my %SWAPS = (); $SWAPS{printer} = $printer; $SWAPS{quota} = $quota; if($pagestate eq "duplex"){ $SWAPS{pagestate} = 'double sided'; }elsif ($pagestate eq "simplex"){ $SWAPS{pagestate} = 'single sided'; } if( 1 ){ $SWAPS{multiplier} = 'some_multiplier'; } # swap out things like __printer__ with $SWAPS{printer} $msg =~ s/__(\w+?)__/$SWAPS{$1}/g;

    This does only one parse on the string and is a little bit more controlled than doing the multiple swap thing.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      Or if you worry about the tokens not being in the hash:
      $msg =~ s/__(\w+?)__/(defined $SWAPS{$1})?$SWAPS{$1}:"__$1__"/eg
      Thanks, I appreciate your help. I'm pretty new to perl, and am trying to figure out ways to streamline my code wherever I can.
      Muchas Gracias