Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Regex reset problem?

by sz (Friar)
on Jul 07, 2000 at 18:20 UTC ( #21483=perlquestion: print w/replies, xml ) Need Help??

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

**newbie warning**

In putting together a one-time tool to stuff some product descriptions into a template, I came up against the following problem. The code below works fine except that the same product description ends up in each file. I used print statments to verify that the the while loop does indeed traverse all the files in the directory. The problem seems to lie in the substitution operation which repeatedly uses the first value of $description for each product page. What am I missing?
open (TEMPLATE, "<$template_file") or die "Can't open $template_file: +$!"; $template = do { local $/; <TEMPLATE> }; close TEMPLATE; opendir (BIN, "$prod_dir") or die "Can't open $dir: $!"; while (defined ($file = readdir BIN)) { next if ($file =~/^\.\.?$/); #skip file starting with periods open (DESCR, "<$prod_dir/$file") or die "could not open $dir/$file +: $!"; $description = do { local $/; <DESCR> }; $template =~ s/%%DESCRIPTION%%/$description/; open (OUTPUT, ">$output_directory/$file.html"); print OUTPUT $template; close OUTPUT; next; } closedir (BIN);

Replies are listed 'Best First'.
Re: Regex reset problem?
by davorg (Chancellor) on Jul 07, 2000 at 18:27 UTC

    In the line

    $template =~ s/%%DESCRIPTION%%/$description/;

    you overwrite the contents of $template so that it no longer contains your %%DESCRIPTION%% tag. Therefore the the next time you try to execute this line, no substitution takes place.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
      Ouch! I knew I was missing an obvious point. Embarrassed but grateful. Thanks for you help.
Re: Regex reset problem?
by c-era (Curate) on Jul 07, 2000 at 18:29 UTC
    That is because you didn't reset your $template, so your regex didn't match %%DESCRIPTION%%. Use can use a tmp variable to reset $template.
    open (TEMPLATE, "<$template_file") or die "Can't open $template_file: +$!"; $template = do { local $/; <TEMPLATE> }; close TEMPLATE; $tmp_template = $template; #store the template opendir (BIN, "$prod_dir") or die "Can't open $dir: $!"; while (defined ($file = readdir BIN)) { next if ($file =~/^\.\.?$/); #skip file starting with period +s open (DESCR, "<$prod_dir/$file") or die "could not open $dir +/$file: $!"; $description = do { local $/; <DESCR> }; $template = $tmp_template; # restore the template $template =~ s/%%DESCRIPTION%%/$description/; open (OUTPUT, ">$output_directory/$file.html"); print OUTPUT $template; close OUTPUT; next; } closedir (BIN);
Re: Regex reset problem?
by jjhorner (Hermit) on Jul 07, 2000 at 19:10 UTC

    You should really, really start using 'warnings' and 'strict' pragmas. Really, you should. I won't start with you, but I may just start voting down all posts that don't appear to be using those two pragmas (-w switch and strict pragma, I guess).

    It really isn't good coding to go without them.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
      J.J.,

      I agree totaly. I use both -w and strict in longer code. I suppose no code is too short or temporary to benefit, however.

      Thanks...
        You two should relax. There's many a way to do things... and if they werk correctly... well can they be wrong? Maybe some hackers are too lazy to type those things or to even deal with the occassional messages that they produce. Maybe they're afraid of them or don't like them or ... have no good reason at all like me. =)

        Maybe you (and others) fundamentally object to my opinion (even on particularly informed and experiential grounds) but I feel that the level of warnings and strictness any PerlMonk would like to adhere to is a personal decision that should not be belittled. If there is a more enlightened way, please kindly explain how and why. Otherwise, your assertions seem elitist and shallow... at least to me. Please don't take this as any sort of kerosene-wielding confrontation but rather an opinion and an interested query for further explanation (as well as the hope of increased tolerance). Thanks very much. TTFN.

        -PipTigger

        p.s. the most cigarettes

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://21483]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-09-21 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (124 votes). Check out past polls.

    Notices?