Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Move along, nothing to see here . . .

by hesco (Deacon)
on Jan 22, 2012 at 20:52 UTC ( #949281=perlquestion: print w/replies, xml ) Need Help??

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

This started off entitled: "issues attempting to use Apache::Admin::Config->save()".

I seem to have fewer and fewer questions around here these days. Once again, stumped for too long, yet writing this out worked me through the steps to resolve this question for myself.

Apparently you can not apply a string substitution on a scalar on the same line where you assign its value to another scalar, or what gets assigned to the result of the string substitution (in my case a 1 for true). Though perhaps they could be wrapped in parens to control priority, don't know.

And I get reminded again to Keep it Simple. So thanks perlmonks for being here, where my last several questions have been aborted by working through this process in a similar way.

at a bash prompt I see:

hesco@marcus8:~/sandbox$ grep `whoami` /etc/passwd hesco:x:1000:1000:Hugh Esco,,,:/home/hesco:/bin/bash hesco@marcus8:~/sandbox$ ls -al t/ total 61 drwxr-xr-x 6 hesco hesco 736 2012-01-22 14:38 . drwxr-xr-x 11 hesco hesco 856 2011-12-24 21:29 .. hesco@marcus8:~/sandbox$ ls -al t/conf/ total 13 drwxr-xr-x 6 hesco hesco 280 2012-01-22 13:50 . drwxr-xr-x 6 hesco hesco 736 2012-01-22 14:38 .. hesco@marcus8:~/sandbox$ ls -al t/conf/etc/ total 0 drwxr-xr-x 3 hesco hesco 72 2011-07-02 00:57 . drwxr-xr-x 6 hesco hesco 280 2012-01-22 13:50 .. hesco@marcus8:~/sandbox$ ls -al t/conf/etc/apache2/ total 1 drwxr-xr-x 3 hesco hesco 80 2011-07-02 00:57 . drwxr-xr-x 3 hesco hesco 72 2011-07-02 00:57 .. hesco@marcus8:~/sandbox$ ls -al t/conf/etc/apache2/sites-available/ total 18 drwxr-xr-x 11 hesco hesco 480 2012-01-22 13:50 . drwxr-xr-x 3 hesco hesco 80 2011-07-02 00:57 .. hesco@marcus8:~/sandbox$ ls -al t/conf/etc/apache2/sites-available/tes +t_suite/ total 1 drwxr-xr-x 2 hesco hesco 48 2012-01-22 13:51 . drwxr-xr-x 11 hesco hesco 480 2012-01-22 13:50 ..

and when running my test script, as indicated, I get:

hesco@marcus8:~/sandbox$ perl t/14-ymd_provision_apache.t snip Use of uninitialized value $parent_directory in substitution (s///) at + lib/Apache.pm line 139. Use of uninitialized value $parent_directory in concatenation (.) or s +tring at lib/Apache.pm line 140. We hope we can write to: at lib/Apache.pm line 140. Use of uninitialized value $parent_directory in -w at lib/Apache.pm li +ne 141. not ok 33 - We got an anticipated result for our new apache vhost path +. # Failed test 'We got an anticipated result for our new apache vhost + path.' # at t/14-ymd_provision_apache.t line 64. # got: 'ERROR: user - 1000 is unable to create t/conf/etc/apa +che2/sites-available/test_suite/test.example.org' # expected: 't/conf/etc/apache2/sites-available/test_suite/test.ex +ample.org' # File [ERROR: user - 1000 is unable to create t/conf/etc/apache2/site +s-available/test_suite/test.example.org] does not exist

from code which looks like this:

my $new_vhost_path = "$apache_vhost_base_path/" . $site->{'vhost_ser +ver_name'}; if( -e $new_vhost_path ){ return 'ERROR: will not overwrite an existing configuration file:' + . $new_vhost_path; } else { my $parent_directory = $new_vhost_path =~ s,^(.*)/(.*),$1,; warn "We hope we can write to: $parent_directory "; if( -w $parent_directory ){ warn "We apparently believe that we can write to: $parent_direct +ory \n"; $self->{'apache'}->save( $new_vhost_path, '-reformat' ); return $new_vhost_path; } else { return 'ERROR: user - ' . $> . ' is unable to create ' . $new_vh +ost_path; } } }

FYI: $self->{'apache'}, in this case isa(Apache::Admin::Config)

And this resolves the bug . . .

- my $parent_directory = $new_vhost_path =~ s,^(.*)/(.*),$1,; + my $parent_directory = $new_vhost_path; + $parent_directory =~ s,^(.*)/.*$,$1,; warn "We hope we can write to: $parent_directory ";

. . . made apparent by informative debug messages.

if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: Move along, nothing to see here . . .
by Corion (Pope) on Jan 22, 2012 at 21:03 UTC

    If you are really bent on doing a changed-assignment in one line, you can do it like this:

    (my $parent_directory = $new_vhost_path) =~ s,^(.*)/(.*),$1,;

    The issue is one of precedence, and the parentheses make the assignment happen first, and the replacement on $parent_directory second.

Re: Move along, nothing to see here . . .
by Anonymous Monk on Jan 23, 2012 at 07:33 UTC
    If you have 5.14 (or 5.13.2 ) you could use the /r flag
    my $old = 'cat'; my $new = $old =~ s/.+/dog/r; ## $old still 'cat' ## $new is 'dog'
Re: Move along, nothing to see here . . .
by mertserger (Curate) on Jan 23, 2012 at 10:12 UTC

    "Once again, stumped for too long, yet writing this out worked me through the steps to resolve this question for myself."

    I know that feeling - the numbers of times I have started to post a question to Perl Monks asking for help, only to find the process of writing the problem down helped me see the answer.

      From "The Practice of Programming" by Brian W Kernighan & Rob Pike:

      Another effective debugging technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind. I see what's wrong. Sorry to bother you." This works remarkably well; you can even use non-programmers as listeners. One university computer centre kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counsellor.

      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

        Also known as teddy bear debugging , mirror debugging, empty room debugging, talking to yourself, Rubber Duck method of debugging, say it out loud George , sing into that hairbrush ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2020-08-12 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (64 votes). Check out past polls.

    Notices?