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

########## perl modules ############ use TeamSite::Config; use OLG::tpl_utils; ########## perl modules ############ my $iwhome = TeamSite::Config::iwgethome(); my $iwmount = TeamSite::Config::iwgetmount(); ($iwcfg) = "$iwhome/bin/iwconfig"; my $dcrpath = iwpt_get_dcr_name(); $dcrpath =~ s#\.iwmnt#iwmnt#g; my $dcr_name = iwpt_get_dcr_name("basename"); my $prefix = getLOB($dcrpath); my $prefDir = uc($prefix); my $templatename = getDataType($dcrpath); my $contentname = $dcr_name; $dcrpath =~ m/\/([^\/]+)\/data/; my $ofile = "generated/component/".$templatename."/".$prefix."_".$cont +entname.".xml"; (my $hostname = `$iwcfg iwwebd host`) =~ s#\n$##; my $edit_path = "http://".$hostname."/iw-cc/edit?vpath=".$dcrpath; $edit_path =~ s#$iwmount##g; (my $bellca = `$iwcfg iwwebd bellca`) =~ s#\n$##; my $endtag = "]"."]>"; my $cdata = "<!"."[CDATA["; ############ Build the initial media lists my @media_ids = (); my $filelist = 'generated/filelist/'; my $media_file = $filelist.$prefix."_".$contentname."_images.txt"; my @matches = getImageList( iwpt_get_dcr_name() ); ############ Build the region array my @region_array = ("default"); my $item_region = join(',',@region_array); ############
In this code, "use CGI" is not used. So how will the progrma understand the environment variables ?

variable is defined as ($iwcfg) , why do we need bracket around variable name, what does it do ? There are my variables declred with brackets as well. So what does it do when there are brackets around variable name ?

Whats the point in declaring my variables at the top level in program. Since my variables are used for localalization of the sciope, why is it at the top ?

Whats the point in declaring "$dcrpath" and "my $dcrpath" at the same level ? Are they different from each other ?

Replies are listed 'Best First'.
Re: Perl program questions
by AnomalousMonk (Archbishop) on Oct 02, 2009 at 22:01 UTC
    In this code, "use CGI" is not used. So how will the progrma understand the environment variables ?
    Offhand, I don't see any environment variables being used. If they were, it might be done through the %ENV special variable, or through some mechanism established via the  TeamSite::Config or  OLG::tpl_utils modules.

    variable is defined as ($iwcfg) , why do we need bracket around variable name, what does it do ?
    This is not a variable definition, but an assignment to the variable  $iwcfg which has apparently been defined in some other part of the code (assuming that strictures have been enabled, as they always should be). If strictures have not been enabled and  $iwcfg has not been elsewhere defined, the package variable  $iwcfg would autovivify at this point. In any event, the parentheses do nothing in this statement.

    There are my variables declred with brackets as well. So what does it do when there are brackets around variable name ?
    The only  my (or lexical) variables I see that are defined within brackets are in statements like
        (my $hostname = `$iwcfg iwwebd host`) =~ s#\n$##;
    This is a 'substitution on assignment' statement in which variable definition has been combined with assignment. The statement could equivalently be written
        my $hostname;
        ($hostname = `$iwcfg iwwebd host`) =~ s#\n$##;
    Other than concision, there is no benefit or effect from combining the two statements as they have been here.

    Whats the point in declaring my variables at the top level in program. Since my variables are used for localalization of the sciope, why is it at the top ?
    Except for  $iwmount, the lexical variables seem to have been defined fairly close to the point at which they are used. This is good practice. The lexicals  $iwhome and  $iwmount may have been defined together at the top of the code fragment to emphasize their common origin from  TeamSite::Config module functions.
    Whats the point in declaring "$dcrpath" and "my $dcrpath" at the same level ? Are they different from each other ?
    In the statements
        my $dcrpath = iwpt_get_dcr_name();
        $dcrpath   =~ s#\.iwmnt#iwmnt#g;
    to which this question seems to refer, the lexical  $dcrpath is defined and initialized in the first statement, and then has a substitution operation performed upon it in the second statement. These two statements do different things.

    BTW,  $dcrpath also apprears in the useless (in the context of this code fragment) statement
        $dcrpath =~ m/\/([^\/]+)\/data/;
    in which a match is done and a sub-string is (potentially) captured, but no result of the match is ever used.