SPUG: Reading a whole file into a scalar.

Fred Morris m3047 at inwa.net
Wed Jul 20 18:28:18 PDT 2005


    local $/; # Slurp.

    # Open the error page template or get upset.

    if ($params->{group}) {

        open(TMPL, '<' . screens() . 'group-alpha.tmpl')
                                            or return "Unable to open page
template: $!";
    }
    else {
        open(TMPL, '<' . screens() . 'contacts-alpha.tmpl')
                                            or return "Unable to open page
template: $!";
    }
    # Read it in.

    (defined($text = <TMPL>))               or return "Failed read in page
template: $!";

    # Close the file.

    close( TMPL )                           or return "Error closing page
template: $!";

    # Substitute the global params into the template.

    $text =~ s/%%magic%%/$params->{magic}/gsi;

To match the newlines, you want to use either/both the m (^ or $ at
embedded newline) or s (. matches newline) qualifiers, and to match all
instances (global replace) g. So for instance:

    # Substitute the global params into the template.
    # (Not the best example of why you need m or s, since it is neither
testing a
    # BOL/EOL assertion, nor matching spaces.)

    $text =~ s/%%magic%%/$params->{magic}/gsi;

At 6:19 PM 7/20/05, Duane Blanchard wrote:
>Hi gang,
>
>I'm too tired to think straight and too tired to keep looking on the
>'Net. I want to match things like 'line\s+that' in the example file
>below.
>
><file>
>this is a line
>that is a line
></file>
>




More information about the spug-list mailing list