Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid

Variable becomes undef on my web host but not locally.

by Lady_Aleena (Chaplain)
on Apr 22, 2013 at 19:41 UTC ( #1029964=perlquestion: print w/ replies, xml ) Need Help??
Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

Hello. If you would be so kind, scroll down to the sub named html, then to the first div in body and tell me why $root_path and $root_user would be undefined there while $root_path is still defined in head above it? The weird thing is, $link{mail} still works in the second div in body. The problem only happens on my web host, not locally. I was unaware of the problem until yesterday when I re-uploaded my site. I pulled out the the list with the get_menu and the first link_list leaving only the second link_list, ran it, and got a segmentation fault. I am really confused.

I put the value of $root_path directly into get_menu, which appears to work, but I would like to know why the variable because undefined in the first place.

package Base::HTML; use strict; use warnings FATAL => qw( all ); use base 'Exporter'; our @EXPORT_OK = qw(html story link_list alpha_links); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Cwd qw(cwd realpath); use File::Basename; use HTML::Entities qw(encode_entities); use List::Util qw(first); use Base::Roots; use Base::Menu qw(get_menu); use Base::Data qw(data_file get_hash); use Base::HTML::Element qw(head title links script body div heading pa +ragraph list anchor); use Util::Convert qw(idify textify); use Base::Sorts qw(article_sort); use Base::Nifty qw(line); my $full_path = realpath($0); my $root_path = get_root('path'); my $root_link = get_root('link'); my $root_name = get_root('name'); my $root_user = get_root('user'); my $root_mail = get_root('mail'); my %link = ( mail => anchor($root_user, { href => "mailto:$root_mail" }), name => anchor($root_name, { href => $root_link }), ); my %other_sites = get_hash( file => data_file('Base','other_sites.txt'), headings => [qw(site link)], ); my %reciprocated_links = get_hash( file => data_file('Base','reciprocated_links.txt'), headings => [qw(site link)], ); my $relative_path = $full_path; $relative_path =~ s/$root_path\///; $relative_path =~ s/\.\w+$//; my @relative_path_split = split(/\//,$relative_path); my @styles = (get_root('css').'/main.css'); sub get_styles { my ($style_dir) = @_; while (@relative_path_split) { my $var = shift @relative_path_split; $var =~ s/(\.\w{2,4})$//; next if $var eq 'working'; if (-f ("$style_dir/$var.css")) { my $new_style = "$style_dir/$var.css"; $new_style =~ s/$root_path/$root_link/; push @styles, $new_style; } get_styles("$style_dir/$var"); } return @styles; } sub link_list { my ($tab,$heading,%hash) = @_; my $heading_id = idify($heading); my @items; for my $key (sort {lc $a->{site} cmp lc $b->{site}} values %hash) { my $link = $key->{link}; my $site = $key->{site}; push @items, anchor($site, { href => "http://$link", target => '_b +lank' }); } heading($tab,2,$heading, { id => $heading_id }); paragraph($tab + 1,'Links will open in a new tab.', { class => 'no_i +ndent' }); list($tab + 1,'u',\@items); } sub html { my (%opt) = @_; my $heading = textify(basename($0)) !~ /index/ ? textify(basename($0 +)) : 'My '.lc((split(/\//,cwd))[-1]); my $title = textify(join(' - ',($root_name,map(ucfirst,split(/\//,$r +elative_path))))); $title .= textify(" - $opt{heading}") if $opt{heading}; my $page_heading = $opt{heading} ? $opt{heading} : $heading; my $page_heading_id = idify($page_heading); $page_heading =~ s/_/ /g; print "content-type: text/html \n\n"; line(0,q(<!DOCTYPE html>)); line(0,q(<html>)); head(0, sub { links(1, [map {{ rel => 'stylesheet', type => 'text/css', href => +$_ }} get_styles($root_path.'/files/css')]); script(1,{ type => 'text/javascript', src => "$root_link/files/jav +ascript/list.js" }); }, { title => $title }); body(0, sub { div(1, sub { heading(2,1,'Site menu', { id => 'Site_menu' }); list(3,'u',get_menu( directory => $root_path, tab => 2, color => + 0, full => 0 ), { onclick => 'list_onclick(event)' }); link_list(3,qq($root_user off-site),%other_sites); link_list(3,qq(Reciprocated links),%reciprocated_links); }, { class => 'left' }); div(1, sub { heading(2,1,$page_heading, { id => $page_heading_id }); &{$opt{code}}; paragraph(3,"Contact $link{mail}!", { class => 'address' }); }, { class => 'right' }); }); line(0,q(</html>)); } 1;

Update: I forgot to use readmore tags. :S

Have a cookie and a very nice day!
Lady Aleena

Comment on Variable becomes undef on my web host but not locally.
Select or Download Code
Re: Variable becomes undef on my web host but not locally.
by Anonymous Monk on Apr 22, 2013 at 20:44 UTC

    Hi Lady Aleena,
    You have only shown us the module, but how is your subroutine html been called is also important.
    Of course, from the face value both your variable $root_path and $root_user should not be undefined but who knows just "maybe", they have been changed.My Advise: Pass the variable to use within a subroutine as argument to the subroutine. In other words, as much as possible, don't use global variable that can be change by other part of your code in your subroutine.
    Simply do:

    ... html(\%hash_to_pass,$root_user,$root_path); ... sub html{ my($hash, $user, $path) = @_; .... }

      Sorry, I should have thought of adding sample usages.

      Printing a story from __DATA__ (the most used, 124 pages):

      html( code => sub { story(*DATA) });

      Printing a list:

      html( code => sub { list(3,'u',\@items, { class => 'two' }) });

      Printing a definition list:

      html( code => sub { definition_list(3, { headings => ['term','definiti +on'] }) });

      Printing a story from __DATA__ with a link_list:

      html( code => sub { story(*DATA); link_list(3,'HTML Links',%HTML_links); });

      Printing a mixed bag:

      html( code => sub { story(*DATA); heading(3,2,'Royal chess pieces'); definition_list(3, { headings => [qw(piece move)] }); table(3, { class => 'royal_chess', caption => 'Royal chess board', r +ows => [['data',\@rows]] }); });

      I was hoping there was a solution which did not require me to rewrite 169 files, however if one does not exist, I will.

      Have a cookie and a very nice day!
      Lady Aleena

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-21 08:22 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (492 votes), past polls