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

I had hoped I could solve my own problem but have been unsucessful. Since I'm totally Perl illiterate, I seem to be fumbling around trying to fix a bug in a script that I downloaded and need to use.

The script as written, produces the path: /home/diamich/public_html/cgi-bin/linkmanage/templates//master.htm when it should produce: "/home/diamich/public_html/cgi-bin/linkmanage/templates/default/master.htm The code written that produces the faulty path is:

my($thebase) = ($file =~ /(.*)\/[^\/]+/); my($html,$html2); if (!-e $file){ $thebase =~ s/\/[^\/]+$/\/default/;
Is there something missing that is causing the code to miss the "default/" addendum to $file? Any help you could give me would be greatly appreciated. Thanks in advance.

Replies are listed 'Best First'.
Re: Trouble with path syntax
by bm (Hermit) on Aug 29, 2003 at 16:11 UTC
    use File::Basename; my $thebase = dirname($file); $thebase = dirname($thebase).'/default' unless -e $file;
    --
    bm
      That one did it!!! THANKS EVER SO MUCH!!!! :))
Re: Trouble with path syntax
by hardburn (Abbot) on Aug 29, 2003 at 13:46 UTC

    Since your s/// works at the end of a string, you could just append to it, with some application of the ? : operator to check if there is a '/' at the end.

    $thebase .= (substr($thebase, -1) eq '/') ? 'default' : '/default';

    Which is functionally equivilent to:

    if(substr($thebase, -1) eq '/') { $thebase = $thebase . 'default'; } else { $thebase = $thebase . '/default'; }

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Trouble with path syntax
by tcf22 (Priest) on Aug 29, 2003 at 13:52 UTC
    My guess is the $file exists, so it never executes.
    $thebase =~ s/\/[^\/]+$/\/default/;
    So the base is /home/diamich/public_html/cgi-bin/linkmanage/templates. Then /master.htm is appended I assume.

    Also if the file didn't exist, the regex would replace /templates with /default so you would end up with /home/diamich/public_html/cgi-bin/linkmanage/default/master.htm and not /home/diamich/public_html/cgi-bin/linkmanage/templates/default/master.htm, since your first regex strips off the file name.
      You may be onto something here. A better shot of the code is:
      sub LoadTemplate { my($file) = @_; my($thebase) = ($file =~ /(.*)\/[^\/]+/); my($html,$html2); if (!-e $file){ $thebase =~ s/\/[^\/]+$/\/default/; } if (-e "$thebase/templates.old"){ open(TEMPLATE, "$file")||&DieNice("Opening: $file -- $!"); my $html = join('',<TEMPLATE>); close(TEMPLATE); }else{ open(TEMPLATE, "$thebase/master.htm")||&DieNice("Opening $theb +ase/master.htm: $!"); $html = join('',<TEMPLATE>); close(TEMPLATE); open(TEMPLATE, "$file")||&DieNice("Opening: $file -- $!"); $html2 = join('',<TEMPLATE>); close(TEMPLATE); $html =~ s/!INSERT!/$html2/i; }
      Still trying to figure out how to fix it.
Re: Trouble with path syntax
by wirrwarr (Monk) on Aug 29, 2003 at 15:23 UTC
    Since you are a "totally Perl illiterate", you might like a description of what the code actually does.
    1: my($thebase) = ($file =~ /(.*)\/[^\/]+/); 2: my($html,$html2); 3: if (!-e $file){ 4: $thebase =~ s/\/[^\/]+$/\/default/;
    as an example, I set $file = "1/2/3/4.txt";
    1: applies a regular expression to $file; assigns the directory $file resides in to $thebase ($thebase = "1/2/3").
    3: checks whether $file does not exist.
    4: if $file does not exist, the last directory component of $thebase is replaced by "default". ($thebase = "1/2/default").

    daniel.
      Thanks for the feedback. I had kind of figured that

      3: if (!-e $file) checked to see if $file did not exist, so I changed it to

      if (-e $file){
      So that it would execute and append the /default. It failed miserably :-(

      $file does exist and = /home/digizms2/public_html/cgi-bin/linkmanage/templates/

      What I need is for the code to check if it exists and if it does, then to make

      $thebase = /home/digizms2/public_html/cgi-bin/linkmanage/templates/default

      Any tips?

Re: Trouble with path syntax
by roju (Friar) on Aug 29, 2003 at 20:10 UTC
    Just since no one else has mentioned it, this is a case where using a different delimiter for m// makes a lot of sense. Something like m{(.*)/[^/]+} gets rid of the need for escaping the slashes.
Re: Trouble with path syntax
by hmerrill (Friar) on Aug 29, 2003 at 15:58 UTC
    I haven't seen yet what the value of $file is before that code is executed. Add some prints to help debug, like:
    print "Start: \$file = $file\n"; my($thebase) = ($file =~ /(.*)\/[^\/]+/); print " \$thebase = $thebase\n"; my($html,$html2); print "Just before test to see if \$file exists\n"; if (!-e $file){ print "$file does *NOT* exist!\n"; $thebase =~ s/\/[^\/]+$/\/default/; print "\$thebase *AFTER* substituting default = $thebase\n"; ### the rest of your code ###
    Those will help figure out exactly what your code is doing. Once we see the results of that it will be easier to offer help.